|
| 1 | +import { describe, it, expect, vi, beforeEach } from 'vitest'; |
| 2 | +import { NextRequest } from 'next/server'; |
| 3 | +import { POST } from '../route'; |
| 4 | +import { RATE_LIMIT_TIERS } from '@/lib/ratelimit'; |
| 5 | + |
| 6 | +// Silence the logger so error reports don't pollute test output. |
| 7 | +vi.mock('@/lib/logging', () => ({ |
| 8 | + createLogger: () => ({ |
| 9 | + error: vi.fn(), |
| 10 | + warn: vi.fn(), |
| 11 | + info: vi.fn(), |
| 12 | + debug: vi.fn(), |
| 13 | + }), |
| 14 | +})); |
| 15 | + |
| 16 | +const LIMIT = RATE_LIMIT_TIERS.REPORTING.limit; |
| 17 | + |
| 18 | +let ipCounter = 0; |
| 19 | +/** Fresh IP per call keeps each test isolated from the shared in-memory store. */ |
| 20 | +function makeRequest(body: unknown, ip = `203.0.113.${ipCounter++}`): NextRequest { |
| 21 | + return new NextRequest('https://example.com/api/errors/report', { |
| 22 | + method: 'POST', |
| 23 | + headers: { |
| 24 | + 'content-type': 'application/json', |
| 25 | + 'x-forwarded-for': ip, |
| 26 | + }, |
| 27 | + body: JSON.stringify(body), |
| 28 | + }); |
| 29 | +} |
| 30 | + |
| 31 | +const sampleReport = { |
| 32 | + id: 'rep_1', |
| 33 | + sessionId: 'sess_1', |
| 34 | + userId: 'user_1', |
| 35 | + url: 'https://example.com/page', |
| 36 | + environment: 'production', |
| 37 | + errorData: { message: 'Something broke', type: 'TypeError' }, |
| 38 | +}; |
| 39 | + |
| 40 | +describe('POST /api/errors/report rate limiting', () => { |
| 41 | + beforeEach(() => { |
| 42 | + ipCounter = 0; |
| 43 | + }); |
| 44 | + |
| 45 | + it('accepts legitimate error reports within the limit', async () => { |
| 46 | + const res = await POST(makeRequest(sampleReport)); |
| 47 | + expect(res.status).toBe(200); |
| 48 | + await expect(res.json()).resolves.toEqual({ ok: true }); |
| 49 | + }); |
| 50 | + |
| 51 | + it('returns 429 once the per-IP limit is exceeded', async () => { |
| 52 | + const ip = '198.51.100.1'; |
| 53 | + |
| 54 | + // Exhaust the allowed quota for this IP. |
| 55 | + for (let i = 0; i < LIMIT; i++) { |
| 56 | + const ok = await POST(makeRequest(sampleReport, ip)); |
| 57 | + expect(ok.status).toBe(200); |
| 58 | + } |
| 59 | + |
| 60 | + // The next request from the same IP is rate limited. |
| 61 | + const blocked = await POST(makeRequest(sampleReport, ip)); |
| 62 | + expect(blocked.status).toBe(429); |
| 63 | + }); |
| 64 | + |
| 65 | + it('includes a Retry-After header on the 429 response', async () => { |
| 66 | + const ip = '198.51.100.2'; |
| 67 | + |
| 68 | + for (let i = 0; i < LIMIT; i++) { |
| 69 | + await POST(makeRequest(sampleReport, ip)); |
| 70 | + } |
| 71 | + const blocked = await POST(makeRequest(sampleReport, ip)); |
| 72 | + |
| 73 | + expect(blocked.status).toBe(429); |
| 74 | + const retryAfter = blocked.headers.get('Retry-After'); |
| 75 | + expect(retryAfter).not.toBeNull(); |
| 76 | + expect(Number(retryAfter)).toBeGreaterThan(0); |
| 77 | + }); |
| 78 | + |
| 79 | + it('does not let one IP exhaust another IP quota', async () => { |
| 80 | + const noisy = '198.51.100.3'; |
| 81 | + for (let i = 0; i < LIMIT; i++) { |
| 82 | + await POST(makeRequest(sampleReport, noisy)); |
| 83 | + } |
| 84 | + expect((await POST(makeRequest(sampleReport, noisy))).status).toBe(429); |
| 85 | + |
| 86 | + // A different IP is unaffected. |
| 87 | + const other = await POST(makeRequest(sampleReport, '198.51.100.4')); |
| 88 | + expect(other.status).toBe(200); |
| 89 | + }); |
| 90 | +}); |
0 commit comments