forked from JhaSourav07/commitpulse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmiddleware.accessibility.test.ts
More file actions
96 lines (78 loc) · 3.04 KB
/
Copy pathmiddleware.accessibility.test.ts
File metadata and controls
96 lines (78 loc) · 3.04 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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { NextRequest, NextResponse } from 'next/server';
import { middleware } from './middleware';
import { rateLimit } from './lib/rate-limit';
vi.mock('./lib/rate-limit', () => ({
rateLimit: vi.fn(),
}));
describe('proxy.accessibility - Middleware Responsibilities (JSON responses, rate limits, headers)', () => {
beforeEach(() => {
vi.clearAllMocks();
});
it('provides a structured JSON error response when the general rate limit is exceeded', async () => {
vi.mocked(rateLimit).mockResolvedValue({
success: false,
limit: 60,
remaining: 0,
reset: 123456789,
});
const request = new NextRequest('http://localhost:3000/api/streak');
const response = await middleware(request);
expect(response.status).toBe(429);
const body = await response.json();
expect(body).toEqual({ error: 'Too many requests' });
});
it('provides a structured JSON error response when the refresh rate limit is exceeded', async () => {
vi.mocked(rateLimit).mockResolvedValue({
success: false,
limit: 5,
remaining: 0,
reset: 123456789,
});
const request = new NextRequest('http://localhost:3000/api/streak?refresh=true');
const response = await middleware(request);
expect(response.status).toBe(429);
const body = await response.json();
expect(body).toEqual({
error: 'Too many refresh requests. Please wait before bypassing the cache again.',
});
});
it('exposes rate limit information transparently via headers on successful requests', async () => {
vi.mocked(rateLimit).mockResolvedValue({
success: true,
limit: 60,
remaining: 59,
reset: 123456789,
});
const request = new NextRequest('http://localhost:3000/api/streak');
const response = await middleware(request);
expect(response.headers.get('X-RateLimit-Limit')).toBe('60');
expect(response.headers.get('X-RateLimit-Remaining')).toBe('59');
expect(response.headers.get('X-RateLimit-Reset')).toBe('123456789');
});
it('exposes correct rate limit policy headers on refresh requests', async () => {
vi.mocked(rateLimit).mockResolvedValue({
success: false,
limit: 5,
remaining: 0,
reset: 123456789,
});
const request = new NextRequest('http://localhost:3000/api/streak?bypassCache=true');
const response = await middleware(request);
expect(response.headers.get('X-RateLimit-Limit')).toBe('5');
expect(response.headers.get('X-RateLimit-Remaining')).toBe('0');
expect(response.headers.get('X-RateLimit-Policy')).toBe('refresh');
});
it('allows the request to proceed when within acceptable rate limits', async () => {
vi.mocked(rateLimit).mockResolvedValue({
success: true,
limit: 60,
remaining: 45,
reset: 123456789,
});
const nextSpy = vi.spyOn(NextResponse, 'next');
const request = new NextRequest('http://localhost:3000/api/streak');
await middleware(request);
expect(nextSpy).toHaveBeenCalled();
});
});