Skip to content
Closed
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
24 changes: 12 additions & 12 deletions middleware.test.ts β†’ proxy.test.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import { beforeEach, describe, expect, it, vi } from 'vitest';
import { NextRequest, NextResponse } from 'next/server';
import { middleware } from './middleware';
import { proxy } from './proxy';
import { rateLimit } from '@/lib/rate-limit';

vi.mock('@/lib/rate-limit', () => ({
rateLimit: vi.fn(),
}));

describe('middleware', () => {
describe('proxy', () => {
beforeEach(() => {
vi.clearAllMocks();
});
Expand All @@ -23,7 +23,7 @@ describe('middleware', () => {
const nextSpy = vi.spyOn(NextResponse, 'next');

const request = new NextRequest('http://localhost:3000/api/streak?user=octocat');
await middleware(request);
await proxy(request);

expect(nextSpy).toHaveBeenCalled();
});
Expand All @@ -37,7 +37,7 @@ describe('middleware', () => {
});

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

expect(response.status).toBe(429);
});
Expand All @@ -51,7 +51,7 @@ describe('middleware', () => {
});

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

await expect(response.json()).resolves.toEqual({
error: 'Too many requests',
Expand All @@ -67,7 +67,7 @@ describe('middleware', () => {
});

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

expect(response.headers.get('X-RateLimit-Limit')).toBe('60');
});
Expand All @@ -81,7 +81,7 @@ describe('middleware', () => {
});

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

expect(response.headers.get('X-RateLimit-Remaining')).toBe('59');
});
Expand All @@ -100,7 +100,7 @@ describe('middleware', () => {
},
});

await middleware(request);
await proxy(request);

expect(rateLimit).toHaveBeenCalledWith('1.2.3.4', 60, 60000);
});
Expand All @@ -119,7 +119,7 @@ describe('middleware', () => {
},
});

await middleware(request);
await proxy(request);

expect(rateLimit).toHaveBeenCalledWith('9.9.9.9', 60, 60000);
});
Expand All @@ -134,7 +134,7 @@ describe('middleware', () => {

const request = new NextRequest('http://localhost:3000/api/streak?user=octocat');

await middleware(request);
await proxy(request);

expect(rateLimit).toHaveBeenCalledWith('127.0.0.1', 60, 60000);
});
Expand All @@ -154,7 +154,7 @@ describe('middleware', () => {
},
});

await middleware(request);
await proxy(request);

expect(rateLimit).toHaveBeenCalledWith('1.2.3.4', 60, 60000);
});
Expand All @@ -173,7 +173,7 @@ describe('middleware', () => {
},
});

await middleware(request);
await proxy(request);

expect(rateLimit).toHaveBeenCalledWith('1.2.3.4', 60, 60000);
});
Expand Down
2 changes: 1 addition & 1 deletion middleware.ts β†’ proxy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import { rateLimit } from './lib/rate-limit';
*
* Limit: 60 requests per minute per IP.
*/
export async function middleware(request: NextRequest) {
export async function proxy(request: NextRequest) {
// Use Vercel's ip property if available, fallback to headers, then localhost
const ip =
request.headers.get('x-forwarded-for')?.split(',')[0] ??
Expand Down
Loading