forked from JhaSourav07/commitpulse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.ratelimit.test.ts
More file actions
76 lines (62 loc) · 2.49 KB
/
Copy pathmiddleware.ratelimit.test.ts
File metadata and controls
76 lines (62 loc) · 2.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
import { describe, expect, it, vi } from 'vitest';
import { NextRequest } from 'next/server';
import { config, middleware } from './middleware';
import { rateLimit } from '@/lib/rate-limit';
vi.mock('@/lib/rate-limit', () => ({
rateLimit: vi.fn(),
}));
describe('middleware rate-limit behavior', () => {
it('calls rateLimit with fixed policy values (60 requests / 60000ms)', async () => {
vi.mocked(rateLimit).mockResolvedValue({
success: true,
limit: 60,
remaining: 59,
reset: 1700000000,
});
const request = new NextRequest('http://localhost:3000/api/streak?user=octocat');
await middleware(request);
expect(rateLimit).toHaveBeenCalledWith(expect.any(String), 60, 60000);
});
it('sets all X-RateLimit headers on successful requests', async () => {
vi.mocked(rateLimit).mockResolvedValue({
success: true,
limit: 60,
remaining: 58,
reset: 1700000100,
});
const request = new NextRequest('http://localhost:3000/api/streak?user=octocat');
const response = await middleware(request);
expect(response.headers.get('X-RateLimit-Limit')).toBe('60');
expect(response.headers.get('X-RateLimit-Remaining')).toBe('58');
expect(response.headers.get('X-RateLimit-Reset')).toBe('1700000100');
});
it('returns 429 with structured JSON body when throttled', async () => {
vi.mocked(rateLimit).mockResolvedValue({
success: false,
limit: 60,
remaining: 0,
reset: 1700000200,
});
const request = new NextRequest('http://localhost:3000/api/streak?user=octocat');
const response = await middleware(request);
expect(response.status).toBe(429);
await expect(response.json()).resolves.toEqual({ error: 'Too many requests' });
});
it('sets JSON and rate headers on throttled responses', async () => {
vi.mocked(rateLimit).mockResolvedValue({
success: false,
limit: 60,
remaining: 0,
reset: 1700000300,
});
const request = new NextRequest('http://localhost:3000/api/streak?user=octocat');
const response = await middleware(request);
expect(response.headers.get('Content-Type')).toBe('application/json');
expect(response.headers.get('X-RateLimit-Limit')).toBe('60');
expect(response.headers.get('X-RateLimit-Remaining')).toBe('0');
expect(response.headers.get('X-RateLimit-Reset')).toBe('1700000300');
});
it('includes compare API matcher in middleware config', () => {
expect(config.matcher).toContain('/api/compare/:path*');
});
});