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
93 lines (75 loc) · 2.89 KB
/
Copy pathmiddleware.accessibility.test.ts
File metadata and controls
93 lines (75 loc) · 2.89 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
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { NextRequest, NextResponse } from 'next/server';
import { proxy as middleware } from './proxy';
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 rate limit is exceeded on any route', async () => {
vi.mocked(rateLimit).mockResolvedValue({
success: false,
limit: 60,
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 requests' });
});
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 headers on rate limited responses', 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.headers.get('X-RateLimit-Limit')).toBe('60');
expect(response.headers.get('X-RateLimit-Remaining')).toBe('0');
});
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();
});
});