|
| 1 | +/** |
| 2 | + * @jest-environment node |
| 3 | + */ |
| 4 | + |
| 5 | +import { NextRequest } from "next/server"; |
| 6 | +import { withRateLimit, checkRateLimit } from "../lib/middleware/rate-limit-middleware"; |
| 7 | +import { RATE_LIMIT_CONFIG } from "../lib/rate-limit"; |
| 8 | + |
| 9 | +// Mock the rate limiter for testing |
| 10 | +jest.mock("../lib/rate-limit", () => ({ |
| 11 | + ...jest.requireActual("../lib/rate-limit"), |
| 12 | + rateLimiters: { |
| 13 | + default: { |
| 14 | + limit: jest.fn(), |
| 15 | + }, |
| 16 | + api: { |
| 17 | + limit: jest.fn(), |
| 18 | + }, |
| 19 | + templates: { |
| 20 | + limit: jest.fn(), |
| 21 | + }, |
| 22 | + snippets: { |
| 23 | + limit: jest.fn(), |
| 24 | + }, |
| 25 | + ai: { |
| 26 | + limit: jest.fn(), |
| 27 | + }, |
| 28 | + auth: { |
| 29 | + limit: jest.fn(), |
| 30 | + }, |
| 31 | + upload: { |
| 32 | + limit: jest.fn(), |
| 33 | + }, |
| 34 | + }, |
| 35 | +})); |
| 36 | + |
| 37 | +describe("Rate Limiting", () => { |
| 38 | + beforeEach(() => { |
| 39 | + jest.clearAllMocks(); |
| 40 | + }); |
| 41 | + |
| 42 | + describe("Rate Limit Configuration", () => { |
| 43 | + it("should have correct rate limit configurations", () => { |
| 44 | + expect(RATE_LIMIT_CONFIG.default).toEqual({ requests: 100, window: "1h" }); |
| 45 | + expect(RATE_LIMIT_CONFIG.api).toEqual({ requests: 60, window: "1m" }); |
| 46 | + expect(RATE_LIMIT_CONFIG.templates).toEqual({ requests: 30, window: "1m" }); |
| 47 | + expect(RATE_LIMIT_CONFIG.snippets).toEqual({ requests: 50, window: "1m" }); |
| 48 | + expect(RATE_LIMIT_CONFIG.ai).toEqual({ requests: 10, window: "1m" }); |
| 49 | + expect(RATE_LIMIT_CONFIG.auth).toEqual({ requests: 5, window: "1m" }); |
| 50 | + expect(RATE_LIMIT_CONFIG.upload).toEqual({ requests: 20, window: "1m" }); |
| 51 | + }); |
| 52 | + }); |
| 53 | + |
| 54 | + describe("Rate Limiting Middleware", () => { |
| 55 | + const mockHandler = jest.fn(); |
| 56 | + const mockRequest = new NextRequest("http://localhost:3000/api/test", { |
| 57 | + headers: { |
| 58 | + "x-forwarded-for": "192.168.1.1", |
| 59 | + }, |
| 60 | + }); |
| 61 | + |
| 62 | + beforeEach(() => { |
| 63 | + mockHandler.mockClear(); |
| 64 | + }); |
| 65 | + |
| 66 | + it("should allow requests within rate limit", async () => { |
| 67 | + const { rateLimiters } = require("../lib/rate-limit"); |
| 68 | + rateLimiters.default.limit.mockResolvedValue({ |
| 69 | + success: true, |
| 70 | + limit: 100, |
| 71 | + remaining: 99, |
| 72 | + reset: Date.now() + 3600000, |
| 73 | + }); |
| 74 | + |
| 75 | + mockHandler.mockResolvedValue(new Response("OK")); |
| 76 | + |
| 77 | + const response = await withRateLimit(mockRequest, mockHandler, "default"); |
| 78 | + |
| 79 | + expect(rateLimiters.default.limit).toHaveBeenCalledWith("192.168.1.1:default"); |
| 80 | + expect(mockHandler).toHaveBeenCalledWith(mockRequest); |
| 81 | + expect(response.status).toBe(200); |
| 82 | + }); |
| 83 | + |
| 84 | + it("should block requests exceeding rate limit", async () => { |
| 85 | + const { rateLimiters } = require("../lib/rate-limit"); |
| 86 | + rateLimiters.default.limit.mockResolvedValue({ |
| 87 | + success: false, |
| 88 | + limit: 100, |
| 89 | + remaining: 0, |
| 90 | + reset: Date.now() + 3600000, |
| 91 | + }); |
| 92 | + |
| 93 | + const response = await withRateLimit(mockRequest, mockHandler, "default"); |
| 94 | + |
| 95 | + expect(rateLimiters.default.limit).toHaveBeenCalledWith("192.168.1.1:default"); |
| 96 | + expect(mockHandler).not.toHaveBeenCalled(); |
| 97 | + expect(response.status).toBe(429); |
| 98 | + |
| 99 | + const body = await response.json(); |
| 100 | + expect(body.error).toBe("Rate limit exceeded"); |
| 101 | + }); |
| 102 | + |
| 103 | + it("should add rate limit headers to successful responses", async () => { |
| 104 | + const { rateLimiters } = require("../lib/rate-limit"); |
| 105 | + rateLimiters.api.limit.mockResolvedValue({ |
| 106 | + success: true, |
| 107 | + limit: 60, |
| 108 | + remaining: 59, |
| 109 | + reset: Date.now() + 60000, |
| 110 | + }); |
| 111 | + |
| 112 | + mockHandler.mockResolvedValue(new Response("OK")); |
| 113 | + |
| 114 | + const response = await withRateLimit(mockRequest, mockHandler, "api"); |
| 115 | + |
| 116 | + expect(response.headers.get("X-RateLimit-Limit")).toBe("60"); |
| 117 | + expect(response.headers.get("X-RateLimit-Remaining")).toBe("59"); |
| 118 | + expect(response.headers.get("X-RateLimit-Policy")).toBe("sliding-window"); |
| 119 | + }); |
| 120 | + |
| 121 | + it("should handle different rate limit types", async () => { |
| 122 | + const { rateLimiters } = require("../lib/rate-limit"); |
| 123 | + |
| 124 | + // Test AI rate limiting (strictest) |
| 125 | + rateLimiters.ai.limit.mockResolvedValue({ |
| 126 | + success: true, |
| 127 | + limit: 10, |
| 128 | + remaining: 9, |
| 129 | + reset: Date.now() + 60000, |
| 130 | + }); |
| 131 | + |
| 132 | + mockHandler.mockResolvedValue(new Response("OK")); |
| 133 | + |
| 134 | + const response = await withRateLimit(mockRequest, mockHandler, "ai"); |
| 135 | + |
| 136 | + expect(rateLimiters.ai.limit).toHaveBeenCalledWith("192.168.1.1:ai"); |
| 137 | + expect(response.headers.get("X-RateLimit-Limit")).toBe("10"); |
| 138 | + }); |
| 139 | + |
| 140 | + it("should handle rate limiting errors gracefully", async () => { |
| 141 | + const { rateLimiters } = require("../lib/rate-limit"); |
| 142 | + rateLimiters.default.limit.mockRejectedValue(new Error("Redis connection failed")); |
| 143 | + |
| 144 | + mockHandler.mockResolvedValue(new Response("OK")); |
| 145 | + |
| 146 | + const response = await withRateLimit(mockRequest, mockHandler, "default"); |
| 147 | + |
| 148 | + // Should allow request to proceed if rate limiting fails |
| 149 | + expect(mockHandler).toHaveBeenCalledWith(mockRequest); |
| 150 | + expect(response.status).toBe(200); |
| 151 | + }); |
| 152 | + }); |
| 153 | + |
| 154 | + describe("IP Address Detection", () => { |
| 155 | + it("should extract IP from X-Forwarded-For header", () => { |
| 156 | + const { getClientIP } = require("../lib/rate-limit"); |
| 157 | + const request = new NextRequest("http://localhost:3000/api/test", { |
| 158 | + headers: { |
| 159 | + "x-forwarded-for": "192.168.1.1, 10.0.0.1", |
| 160 | + }, |
| 161 | + }); |
| 162 | + |
| 163 | + const ip = getClientIP(request); |
| 164 | + expect(ip).toBe("192.168.1.1"); |
| 165 | + }); |
| 166 | + |
| 167 | + it("should extract IP from X-Real-IP header", () => { |
| 168 | + const { getClientIP } = require("../lib/rate-limit"); |
| 169 | + const request = new NextRequest("http://localhost:3000/api/test", { |
| 170 | + headers: { |
| 171 | + "x-real-ip": "192.168.1.2", |
| 172 | + }, |
| 173 | + }); |
| 174 | + |
| 175 | + const ip = getClientIP(request); |
| 176 | + expect(ip).toBe("192.168.1.2"); |
| 177 | + }); |
| 178 | + |
| 179 | + it("should fallback to default IP", () => { |
| 180 | + const { getClientIP } = require("../lib/rate-limit"); |
| 181 | + const request = new NextRequest("http://localhost:3000/api/test"); |
| 182 | + |
| 183 | + const ip = getClientIP(request); |
| 184 | + expect(ip).toBe("127.0.0.1"); |
| 185 | + }); |
| 186 | + }); |
| 187 | +}); |
| 188 | + |
| 189 | +describe("Rate Limit Error Responses", () => { |
| 190 | + it("should create proper error response format", () => { |
| 191 | + const { createRateLimitErrorResponse } = require("../lib/rate-limit"); |
| 192 | + const result = { |
| 193 | + success: false, |
| 194 | + limit: 10, |
| 195 | + remaining: 0, |
| 196 | + reset: new Date("2024-01-01T12:00:00Z").getTime(), |
| 197 | + }; |
| 198 | + |
| 199 | + const response = createRateLimitErrorResponse(result); |
| 200 | + |
| 201 | + expect(response.status).toBe(429); |
| 202 | + expect(response.headers.get("X-RateLimit-Limit")).toBe("10"); |
| 203 | + expect(response.headers.get("X-RateLimit-Remaining")).toBe("0"); |
| 204 | + expect(response.headers.get("Retry-After")).toBeTruthy(); |
| 205 | + }); |
| 206 | +}); |
0 commit comments