|
| 1 | +import { checkRateLimit } from '../../../src/utils/rateLimit'; |
| 2 | +import { NextRequest } from 'next/server'; |
| 3 | + |
| 4 | +function makeRequest(ip: string = '127.0.0.1'): NextRequest { |
| 5 | + const req = new NextRequest('http://localhost/api/geocode?postcode=M1+1AA', { |
| 6 | + headers: { 'x-forwarded-for': ip }, |
| 7 | + }); |
| 8 | + return req; |
| 9 | +} |
| 10 | + |
| 11 | +describe('checkRateLimit', () => { |
| 12 | + const options = { maxRequests: 3, windowMs: 60_000 }; |
| 13 | + |
| 14 | + it('should allow requests within the limit', () => { |
| 15 | + const ip = '10.0.0.1'; |
| 16 | + const result1 = checkRateLimit(makeRequest(ip), options); |
| 17 | + expect(result1.allowed).toBe(true); |
| 18 | + expect(result1.remaining).toBe(2); |
| 19 | + |
| 20 | + const result2 = checkRateLimit(makeRequest(ip), options); |
| 21 | + expect(result2.allowed).toBe(true); |
| 22 | + expect(result2.remaining).toBe(1); |
| 23 | + |
| 24 | + const result3 = checkRateLimit(makeRequest(ip), options); |
| 25 | + expect(result3.allowed).toBe(true); |
| 26 | + expect(result3.remaining).toBe(0); |
| 27 | + }); |
| 28 | + |
| 29 | + it('should block requests exceeding the limit', () => { |
| 30 | + const ip = '10.0.0.2'; |
| 31 | + for (let i = 0; i < 3; i++) { |
| 32 | + checkRateLimit(makeRequest(ip), options); |
| 33 | + } |
| 34 | + |
| 35 | + const blocked = checkRateLimit(makeRequest(ip), options); |
| 36 | + expect(blocked.allowed).toBe(false); |
| 37 | + expect(blocked.remaining).toBe(0); |
| 38 | + }); |
| 39 | + |
| 40 | + it('should track different IPs independently', () => { |
| 41 | + const ipA = '10.0.0.3'; |
| 42 | + const ipB = '10.0.0.4'; |
| 43 | + |
| 44 | + for (let i = 0; i < 3; i++) { |
| 45 | + checkRateLimit(makeRequest(ipA), options); |
| 46 | + } |
| 47 | + |
| 48 | + const blockedA = checkRateLimit(makeRequest(ipA), options); |
| 49 | + expect(blockedA.allowed).toBe(false); |
| 50 | + |
| 51 | + const allowedB = checkRateLimit(makeRequest(ipB), options); |
| 52 | + expect(allowedB.allowed).toBe(true); |
| 53 | + expect(allowedB.remaining).toBe(2); |
| 54 | + }); |
| 55 | + |
| 56 | + it('should reset after the window expires', () => { |
| 57 | + const ip = '10.0.0.5'; |
| 58 | + const shortWindow = { maxRequests: 1, windowMs: 50 }; |
| 59 | + |
| 60 | + const result1 = checkRateLimit(makeRequest(ip), shortWindow); |
| 61 | + expect(result1.allowed).toBe(true); |
| 62 | + |
| 63 | + const blocked = checkRateLimit(makeRequest(ip), shortWindow); |
| 64 | + expect(blocked.allowed).toBe(false); |
| 65 | + |
| 66 | + return new Promise<void>((resolve) => { |
| 67 | + setTimeout(() => { |
| 68 | + const result2 = checkRateLimit(makeRequest(ip), shortWindow); |
| 69 | + expect(result2.allowed).toBe(true); |
| 70 | + resolve(); |
| 71 | + }, 60); |
| 72 | + }); |
| 73 | + }); |
| 74 | + |
| 75 | + it('should include a resetTime in the result', () => { |
| 76 | + const ip = '10.0.0.6'; |
| 77 | + const before = Date.now(); |
| 78 | + const result = checkRateLimit(makeRequest(ip), options); |
| 79 | + const after = Date.now(); |
| 80 | + |
| 81 | + expect(result.resetTime).toBeGreaterThanOrEqual(before + options.windowMs); |
| 82 | + expect(result.resetTime).toBeLessThanOrEqual(after + options.windowMs); |
| 83 | + }); |
| 84 | + |
| 85 | + it('should handle missing x-forwarded-for header', () => { |
| 86 | + const req = new NextRequest('http://localhost/api/geocode?postcode=M1+1AA'); |
| 87 | + const result = checkRateLimit(req, { maxRequests: 100, windowMs: 60_000 }); |
| 88 | + expect(result.allowed).toBe(true); |
| 89 | + }); |
| 90 | +}); |
0 commit comments