|
| 1 | +import type { RequestHeadersPluginContext } from './request-headers' |
| 2 | +import { OpenAPIHandler } from '../../../openapi/src/adapters/fetch/openapi-handler' |
| 3 | +import { os } from '../builder' |
| 4 | +import { RequestHeadersPlugin } from './request-headers' |
| 5 | + |
| 6 | +describe('requestHeadersPlugin', () => { |
| 7 | + it('works', async () => { |
| 8 | + let capturedHeaders: Headers | undefined |
| 9 | + |
| 10 | + const router = os |
| 11 | + .$context<RequestHeadersPluginContext>() |
| 12 | + .use(({ context, next }) => { |
| 13 | + // Capture the headers from context for verification |
| 14 | + capturedHeaders = context.reqHeaders |
| 15 | + return next() |
| 16 | + }) |
| 17 | + .route({ |
| 18 | + method: 'GET', |
| 19 | + path: '/ping', |
| 20 | + }) |
| 21 | + .handler(() => 'pong') |
| 22 | + |
| 23 | + const handler = new OpenAPIHandler(router, { |
| 24 | + plugins: [ |
| 25 | + new RequestHeadersPlugin(), |
| 26 | + ], |
| 27 | + }) |
| 28 | + |
| 29 | + const request = new Request('https://example.com/ping', { |
| 30 | + headers: { |
| 31 | + 'x-custom-1': 'value1', |
| 32 | + 'x-custom-2': 'value2', |
| 33 | + 'content-type': 'application/json', |
| 34 | + 'authorization': 'Bearer token123', |
| 35 | + }, |
| 36 | + }) |
| 37 | + |
| 38 | + const { response } = await handler.handle(request) |
| 39 | + |
| 40 | + if (!response) { |
| 41 | + throw new Error('response is undefined') |
| 42 | + } |
| 43 | + |
| 44 | + expect(capturedHeaders).toBeInstanceOf(Headers) |
| 45 | + expect(capturedHeaders?.get('x-custom-1')).toBe('value1') |
| 46 | + expect(capturedHeaders?.get('x-custom-2')).toBe('value2') |
| 47 | + expect(capturedHeaders?.get('content-type')).toBe('application/json') |
| 48 | + expect(capturedHeaders?.get('authorization')).toBe('Bearer token123') |
| 49 | + }) |
| 50 | + |
| 51 | + it('should clone the context to avoid reference issues', async () => { |
| 52 | + const router = os |
| 53 | + .$context<RequestHeadersPluginContext>() |
| 54 | + .route({ |
| 55 | + method: 'GET', |
| 56 | + path: '/ping', |
| 57 | + }) |
| 58 | + .handler(() => 'ping') |
| 59 | + |
| 60 | + const interceptor = vi.fn(({ next }) => next()) |
| 61 | + |
| 62 | + const handler = new OpenAPIHandler(router, { |
| 63 | + plugins: [ |
| 64 | + new RequestHeadersPlugin(), |
| 65 | + ], |
| 66 | + interceptors: [ |
| 67 | + interceptor, |
| 68 | + ], |
| 69 | + }) |
| 70 | + |
| 71 | + const context = { a: 'value' } |
| 72 | + await handler.handle(new Request('https://example.com/ping'), { context }) |
| 73 | + |
| 74 | + expect(interceptor).toHaveBeenCalledOnce() |
| 75 | + expect(interceptor).toHaveBeenCalledWith(expect.objectContaining({ |
| 76 | + context: { ...context, reqHeaders: expect.any(Headers) }, |
| 77 | + })) |
| 78 | + |
| 79 | + expect(interceptor.mock.calls[0]![0].context).not.toBe(context) |
| 80 | + }) |
| 81 | + |
| 82 | + it('should use the provided reqHeaders when already defined', async () => { |
| 83 | + const router = os |
| 84 | + .$context<RequestHeadersPluginContext>() |
| 85 | + .route({ |
| 86 | + method: 'GET', |
| 87 | + path: '/ping', |
| 88 | + }) |
| 89 | + .handler(() => 'ping') |
| 90 | + |
| 91 | + const interceptor = vi.fn(({ next }) => next()) |
| 92 | + |
| 93 | + const handler = new OpenAPIHandler(router, { |
| 94 | + plugins: [ |
| 95 | + new RequestHeadersPlugin(), |
| 96 | + ], |
| 97 | + interceptors: [ |
| 98 | + interceptor, |
| 99 | + ], |
| 100 | + }) |
| 101 | + |
| 102 | + const existingHeaders = new Headers({ 'existing-header': 'existing-value' }) |
| 103 | + const context = { a: 'value', reqHeaders: existingHeaders } |
| 104 | + await handler.handle(new Request('https://example.com/ping'), { context }) |
| 105 | + |
| 106 | + expect(interceptor).toHaveBeenCalledOnce() |
| 107 | + expect(interceptor).toHaveBeenCalledWith(expect.objectContaining({ |
| 108 | + context, |
| 109 | + })) |
| 110 | + |
| 111 | + // The existing reqHeaders should be preserved |
| 112 | + expect(interceptor.mock.calls[0]![0].context.reqHeaders).toBe(existingHeaders) |
| 113 | + }) |
| 114 | +}) |
0 commit comments