|
| 1 | +import { describe, expect, test, vi } from 'vitest'; |
| 2 | +import { NextRequest } from 'next/server'; |
| 3 | +import { oauthApiHandler } from './apiHandler'; |
| 4 | + |
| 5 | +vi.mock('@/lib/posthog', () => ({ |
| 6 | + captureEvent: vi.fn(), |
| 7 | +})); |
| 8 | + |
| 9 | +const makeRequest = () => new NextRequest('http://localhost/api/ee/oauth/test', { method: 'POST' }); |
| 10 | + |
| 11 | +describe('oauthApiHandler', () => { |
| 12 | + test('passes through a 200 response unchanged', async () => { |
| 13 | + const handler = oauthApiHandler(async (_req: NextRequest) => Response.json({ ok: true }, { status: 200 })); |
| 14 | + const res = await handler(makeRequest()); |
| 15 | + expect(res.status).toBe(200); |
| 16 | + }); |
| 17 | + |
| 18 | + test('passes through a 400 response unchanged', async () => { |
| 19 | + const handler = oauthApiHandler(async (_req: NextRequest) => Response.json({ error: 'bad' }, { status: 400 })); |
| 20 | + const res = await handler(makeRequest()); |
| 21 | + expect(res.status).toBe(400); |
| 22 | + }); |
| 23 | + |
| 24 | + test('passes through a 302 redirect unchanged', async () => { |
| 25 | + const handler = oauthApiHandler(async (_req: NextRequest) => |
| 26 | + new Response(null, { status: 302, headers: { Location: '/elsewhere' } }) |
| 27 | + ); |
| 28 | + const res = await handler(makeRequest()); |
| 29 | + expect(res.status).toBe(302); |
| 30 | + }); |
| 31 | + |
| 32 | + test('passes through a 303 redirect unchanged (the spec-recommended status)', async () => { |
| 33 | + const handler = oauthApiHandler(async (_req: NextRequest) => |
| 34 | + new Response(null, { status: 303, headers: { Location: '/elsewhere' } }) |
| 35 | + ); |
| 36 | + const res = await handler(makeRequest()); |
| 37 | + expect(res.status).toBe(303); |
| 38 | + }); |
| 39 | + |
| 40 | + test('throws when the inner handler returns 307', async () => { |
| 41 | + const handler = oauthApiHandler(async (_req: NextRequest) => |
| 42 | + new Response(null, { status: 307, headers: { Location: '/elsewhere' } }) |
| 43 | + ); |
| 44 | + await expect(handler(makeRequest())).rejects.toThrow(/RFC 9700/); |
| 45 | + }); |
| 46 | + |
| 47 | + test('throws when the inner handler returns 308', async () => { |
| 48 | + const handler = oauthApiHandler(async (_req: NextRequest) => |
| 49 | + new Response(null, { status: 308, headers: { Location: '/elsewhere' } }) |
| 50 | + ); |
| 51 | + await expect(handler(makeRequest())).rejects.toThrow(/RFC 9700/); |
| 52 | + }); |
| 53 | +}); |
0 commit comments