Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 8 additions & 11 deletions middleware.accessibility.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { describe, it, expect, vi, beforeEach } from 'vitest';
import { NextRequest, NextResponse } from 'next/server';
import { middleware } from './middleware';
import { proxy as middleware } from './proxy';
import { rateLimit } from './lib/rate-limit';

vi.mock('./lib/rate-limit', () => ({
Expand Down Expand Up @@ -28,10 +28,10 @@ describe('proxy.accessibility - Middleware Responsibilities (JSON responses, rat
expect(body).toEqual({ error: 'Too many requests' });
});

it('provides a structured JSON error response when the refresh rate limit is exceeded', async () => {
it('provides a structured JSON error response when rate limit is exceeded on any route', async () => {
vi.mocked(rateLimit).mockResolvedValue({
success: false,
limit: 5,
limit: 60,
remaining: 0,
reset: 123456789,
});
Expand All @@ -41,9 +41,7 @@ describe('proxy.accessibility - Middleware Responsibilities (JSON responses, rat

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.',
});
expect(body).toEqual({ error: 'Too many requests' });
});

it('exposes rate limit information transparently via headers on successful requests', async () => {
Expand All @@ -62,20 +60,19 @@ describe('proxy.accessibility - Middleware Responsibilities (JSON responses, rat
expect(response.headers.get('X-RateLimit-Reset')).toBe('123456789');
});

it('exposes correct rate limit policy headers on refresh requests', async () => {
it('exposes correct rate limit headers on rate limited responses', async () => {
vi.mocked(rateLimit).mockResolvedValue({
success: false,
limit: 5,
limit: 60,
remaining: 0,
reset: 123456789,
});

const request = new NextRequest('http://localhost:3000/api/streak?bypassCache=true');
const request = new NextRequest('http://localhost:3000/api/streak');
const response = await middleware(request);

expect(response.headers.get('X-RateLimit-Limit')).toBe('5');
expect(response.headers.get('X-RateLimit-Limit')).toBe('60');
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 () => {
Expand Down
Loading
Loading