|
| 1 | +import { OpenAPIHandler } from '../../../openapi/src/adapters/fetch/openapi-handler' |
| 2 | +import { os } from '../builder' |
| 3 | +import { CORSPlugin } from './cors' |
| 4 | + |
| 5 | +function assertResponse(response: Response | undefined): asserts response is Response { |
| 6 | + if (!response) { |
| 7 | + throw new Error('response is undefined') |
| 8 | + } |
| 9 | +} |
| 10 | + |
| 11 | +beforeEach(() => { |
| 12 | + vi.clearAllMocks() |
| 13 | +}) |
| 14 | + |
| 15 | +describe('corsPlugin', () => { |
| 16 | + const handlerFn = vi.fn(() => 'pong') |
| 17 | + const router = os |
| 18 | + .route({ |
| 19 | + method: 'GET', |
| 20 | + path: '/ping', |
| 21 | + }) |
| 22 | + .handler(handlerFn) |
| 23 | + |
| 24 | + it('handles OPTIONS request with default options', async () => { |
| 25 | + const handler = new OpenAPIHandler(router, { |
| 26 | + plugins: [new CORSPlugin()], |
| 27 | + }) |
| 28 | + |
| 29 | + const { response } = await handler.handle(new Request('https://example.com', { |
| 30 | + method: 'OPTIONS', |
| 31 | + headers: { |
| 32 | + origin: 'https://example.com', |
| 33 | + }, |
| 34 | + })) |
| 35 | + |
| 36 | + assertResponse(response) |
| 37 | + expect(handlerFn).toHaveBeenCalledTimes(0) |
| 38 | + expect(response.status).toBe(204) |
| 39 | + expect(response.headers.get('access-control-allow-origin')).toBe('https://example.com') |
| 40 | + expect(response.headers.get('vary')).toBe('origin') |
| 41 | + expect(response.headers.get('access-control-allow-methods')).toBe('GET,HEAD,PUT,POST,DELETE,PATCH') |
| 42 | + expect(response.headers.get('access-control-max-age')).toBeNull() |
| 43 | + }) |
| 44 | + |
| 45 | + it('handles GET request and sets CORS headers with default origin function', async () => { |
| 46 | + const handler = new OpenAPIHandler(router, { |
| 47 | + plugins: [new CORSPlugin()], |
| 48 | + }) |
| 49 | + |
| 50 | + const { response } = await handler.handle(new Request('https://example.com/ping', { |
| 51 | + headers: { |
| 52 | + origin: 'https://example.com', |
| 53 | + }, |
| 54 | + })) |
| 55 | + |
| 56 | + assertResponse(response) |
| 57 | + expect(handlerFn).toHaveBeenCalledTimes(1) |
| 58 | + expect(response.headers.get('access-control-allow-origin')).toBe('https://example.com') |
| 59 | + expect(response.headers.get('vary')).toBe('origin') |
| 60 | + }) |
| 61 | + |
| 62 | + it('applies maxAge and allowHeaders on OPTIONS requests when specified', async () => { |
| 63 | + const plugin = new CORSPlugin({ |
| 64 | + maxAge: 600, |
| 65 | + allowHeaders: ['Content-Type', 'Authorization'], |
| 66 | + }) |
| 67 | + |
| 68 | + const handler = new OpenAPIHandler(router, { |
| 69 | + plugins: [plugin], |
| 70 | + }) |
| 71 | + |
| 72 | + const { response } = await handler.handle(new Request('https://example.com/test', { |
| 73 | + method: 'OPTIONS', |
| 74 | + headers: { |
| 75 | + origin: 'https://example.com', |
| 76 | + }, |
| 77 | + })) |
| 78 | + |
| 79 | + assertResponse(response) |
| 80 | + expect(response.headers.get('access-control-max-age')).toBe('600') |
| 81 | + expect(response.headers.get('access-control-allow-methods')).toBe('GET,HEAD,PUT,POST,DELETE,PATCH') |
| 82 | + expect(response.headers.get('access-control-allow-headers')).toBe('Content-Type,Authorization') |
| 83 | + }) |
| 84 | + |
| 85 | + it('sets allowed origin only when custom origin function approves', async () => { |
| 86 | + // Custom origin function: only allow 'https://allowed.com' |
| 87 | + const customOrigin = (origin: string) => origin === 'https://allowed.com' ? origin : '' |
| 88 | + const router = os |
| 89 | + .route({ |
| 90 | + method: 'GET', |
| 91 | + path: '/custom', |
| 92 | + }) |
| 93 | + .handler(() => 'ok') |
| 94 | + |
| 95 | + const plugin = new CORSPlugin({ origin: customOrigin }) |
| 96 | + const handler = new OpenAPIHandler(router, { |
| 97 | + plugins: [plugin], |
| 98 | + }) |
| 99 | + |
| 100 | + // Request from allowed origin |
| 101 | + const { response } = await handler.handle(new Request('https://example.com/custom', { |
| 102 | + headers: { |
| 103 | + origin: 'https://allowed.com', |
| 104 | + }, |
| 105 | + })) |
| 106 | + assertResponse(response) |
| 107 | + expect(response.headers.get('access-control-allow-origin')).toBe('https://allowed.com') |
| 108 | + |
| 109 | + // Request from a disallowed origin should not get the header set |
| 110 | + const { response: response2 } = await handler.handle(new Request('https://example.com/custom', { |
| 111 | + headers: { |
| 112 | + origin: 'https://disallowed.com', |
| 113 | + }, |
| 114 | + })) |
| 115 | + assertResponse(response2) |
| 116 | + expect(response2.headers.get('access-control-allow-origin')).toBeNull() |
| 117 | + }) |
| 118 | + |
| 119 | + it('handles timingOrigin option correctly', async () => { |
| 120 | + // Custom timingOrigin: only allow 'https://timing.com' |
| 121 | + const customTimingOrigin = (origin: string) => origin === 'https://timing.com' ? origin : '' |
| 122 | + const router = os |
| 123 | + .route({ |
| 124 | + method: 'GET', |
| 125 | + path: '/timing', |
| 126 | + }) |
| 127 | + .handler(() => 'ok') |
| 128 | + |
| 129 | + const plugin = new CORSPlugin({ timingOrigin: customTimingOrigin }) |
| 130 | + const handler = new OpenAPIHandler(router, { |
| 131 | + plugins: [plugin], |
| 132 | + }) |
| 133 | + |
| 134 | + // Request with allowed timing origin |
| 135 | + const { response } = await handler.handle(new Request('https://example.com/timing', { |
| 136 | + headers: { |
| 137 | + origin: 'https://timing.com', |
| 138 | + }, |
| 139 | + })) |
| 140 | + assertResponse(response) |
| 141 | + expect(response.headers.get('timing-allow-origin')).toBe('https://timing.com') |
| 142 | + |
| 143 | + // Request with not allowed timing origin should not have the header |
| 144 | + const { response: response2 } = await handler.handle(new Request('https://example.com/timing', { |
| 145 | + headers: { |
| 146 | + origin: 'https://not-timing.com', |
| 147 | + }, |
| 148 | + })) |
| 149 | + assertResponse(response2) |
| 150 | + expect(response2.headers.get('timing-allow-origin')).toBeNull() |
| 151 | + }) |
| 152 | + |
| 153 | + it('sets credentials and exposeHeaders when specified in options', async () => { |
| 154 | + const plugin = new CORSPlugin({ |
| 155 | + credentials: true, |
| 156 | + exposeHeaders: ['X-Custom-Header', 'X-Another-Header'], |
| 157 | + }) |
| 158 | + |
| 159 | + const handler = new OpenAPIHandler(router, { |
| 160 | + plugins: [plugin], |
| 161 | + }) |
| 162 | + |
| 163 | + const { response } = await handler.handle(new Request('https://example.com/ping', { |
| 164 | + headers: { |
| 165 | + origin: 'https://example.com', |
| 166 | + }, |
| 167 | + })) |
| 168 | + assertResponse(response) |
| 169 | + expect(response.headers.get('access-control-allow-credentials')).toBe('true') |
| 170 | + expect(response.headers.get('access-control-expose-headers')).toBe('X-Custom-Header,X-Another-Header') |
| 171 | + }) |
| 172 | + |
| 173 | + it('returns "*" for access-control-allow-origin when origin function returns "*"', async () => { |
| 174 | + const plugin = new CORSPlugin({ origin: () => '*' }) |
| 175 | + const handler = new OpenAPIHandler(router, { |
| 176 | + plugins: [plugin], |
| 177 | + }) |
| 178 | + |
| 179 | + const { response } = await handler.handle(new Request('https://example.com/ping', { |
| 180 | + headers: { |
| 181 | + origin: 'https://any-origin.com', |
| 182 | + }, |
| 183 | + })) |
| 184 | + assertResponse(response) |
| 185 | + expect(response.headers.get('access-control-allow-origin')).toBe('*') |
| 186 | + expect(response.headers.get('vary')).toBeNull() |
| 187 | + }) |
| 188 | +}) |
0 commit comments