|
2 | 2 | * @vitest-environment node |
3 | 3 | */ |
4 | 4 | import { getRequestContext } from '@sim/logger' |
| 5 | +import { NextResponse } from 'next/server' |
5 | 6 | import { beforeEach, describe, expect, it, vi } from 'vitest' |
6 | | -import { withRequestId } from '@/lib/api/server/routes/request-id' |
| 7 | +import { responseWithRequestId, withRequestId } from '@/lib/api/server/routes/request-id' |
7 | 8 |
|
8 | 9 | const mockGetRequestContext = vi.mocked(getRequestContext) |
9 | 10 |
|
@@ -43,3 +44,63 @@ describe('withRequestId', () => { |
43 | 44 | expect(withRequestId([{ error: 'a' }])).toEqual([{ error: 'a' }]) |
44 | 45 | }) |
45 | 46 | }) |
| 47 | + |
| 48 | +describe('responseWithRequestId', () => { |
| 49 | + beforeEach(() => { |
| 50 | + vi.clearAllMocks() |
| 51 | + mockGetRequestContext.mockReturnValue(undefined) |
| 52 | + }) |
| 53 | + |
| 54 | + it('stamps the request id into an already-built JSON error response', async () => { |
| 55 | + mockGetRequestContext.mockReturnValue({ requestId: 'req-123' }) |
| 56 | + |
| 57 | + const stamped = await responseWithRequestId( |
| 58 | + NextResponse.json({ error: 'Validation error', details: [] }, { status: 400 }) |
| 59 | + ) |
| 60 | + |
| 61 | + expect(stamped.status).toBe(400) |
| 62 | + await expect(stamped.json()).resolves.toEqual({ |
| 63 | + error: 'Validation error', |
| 64 | + details: [], |
| 65 | + requestId: 'req-123', |
| 66 | + }) |
| 67 | + }) |
| 68 | + |
| 69 | + it('preserves headers other than a stale content-length', async () => { |
| 70 | + mockGetRequestContext.mockReturnValue({ requestId: 'req-123' }) |
| 71 | + |
| 72 | + const original = NextResponse.json( |
| 73 | + { error: 'Validation error' }, |
| 74 | + { status: 400, headers: { 'x-custom': 'kept', 'content-length': '29' } } |
| 75 | + ) |
| 76 | + const stamped = await responseWithRequestId(original) |
| 77 | + |
| 78 | + expect(stamped.headers.get('x-custom')).toBe('kept') |
| 79 | + expect(stamped.headers.get('content-length')).toBeNull() |
| 80 | + }) |
| 81 | + |
| 82 | + it('returns the original response when there is no active request scope', async () => { |
| 83 | + const original = NextResponse.json({ error: 'Validation error' }, { status: 400 }) |
| 84 | + |
| 85 | + expect(await responseWithRequestId(original)).toBe(original) |
| 86 | + }) |
| 87 | + |
| 88 | + it('returns the original response when the body is not JSON', async () => { |
| 89 | + mockGetRequestContext.mockReturnValue({ requestId: 'req-123' }) |
| 90 | + |
| 91 | + const original = new NextResponse('plain text', { |
| 92 | + status: 400, |
| 93 | + headers: { 'content-type': 'text/plain' }, |
| 94 | + }) |
| 95 | + |
| 96 | + expect(await responseWithRequestId(original)).toBe(original) |
| 97 | + }) |
| 98 | + |
| 99 | + it('leaves a response that already carries a requestId untouched', async () => { |
| 100 | + mockGetRequestContext.mockReturnValue({ requestId: 'req-123' }) |
| 101 | + |
| 102 | + const original = NextResponse.json({ error: 'boom', requestId: 'explicit' }, { status: 400 }) |
| 103 | + |
| 104 | + expect(await responseWithRequestId(original)).toBe(original) |
| 105 | + }) |
| 106 | +}) |
0 commit comments