import { describe, it } from 'bun:test'; import { expect } from 'chai'; import { CorsMiddleware } from './cors-middleware'; import { CorsOptions } from './cors-protocol'; async function run(options: CorsOptions | undefined, method: string, headers: Record = {}, endpoint?: string) { const middleware = new CorsMiddleware(); Object.assign(middleware, { options, endpoint }); (middleware as unknown as { init(): void }).init(); const set: Record = {}; const response = { statusCode: 200, ended: false, getHeader: (name: string) => set[name.toLowerCase()], setHeader: (name: string, value: string) => { set[name.toLowerCase()] = value; }, end() { this.ended = true; } }; let nextCalled = false; await middleware.handle({ request: { method, headers }, response } as any, async () => { nextCalled = true; }); return { headers: set, status: response.statusCode, ended: response.ended, nextCalled }; } describe('CorsMiddleware', () => { it('allows any origin by default', async () => { const result = await run(undefined, 'GET', { origin: 'https://a.com' }); expect(result.headers).to.deep.equal({ 'access-control-allow-origin': '*' }); expect(result.nextCalled).to.equal(true); }); it('answers preflight requests', async () => { const result = await run({ credentials: true, maxAge: 600 }, 'OPTIONS', { origin: 'https://a.com', 'access-control-request-headers': 'x-a' }); expect(result.headers).to.deep.equal({ 'access-control-allow-origin': '*', 'access-control-allow-credentials': 'true', 'access-control-allow-methods': 'GET,HEAD,PUT,PATCH,POST,DELETE', 'access-control-allow-headers': 'x-a', 'access-control-max-age': '600', 'content-length': '0', vary: 'Access-Control-Request-Headers' }); expect(result).to.deep.include({ status: 204, ended: true, nextCalled: false }); expect((await run({ preflightContinue: true }, 'OPTIONS', {})).nextCalled).to.equal(true); }); it('reflects allowed origins only', async () => { const options = { origin: [/\.example\.com$/, 'https://b.com'], exposedHeaders: ['x-a', 'x-b'] }; const allowed = await run(options, 'GET', { origin: 'https://app.example.com' }); expect(allowed.headers).to.deep.equal({ 'access-control-allow-origin': 'https://app.example.com', vary: 'Origin', 'access-control-expose-headers': 'x-a,x-b' }); const refused = await run(options, 'GET', { origin: 'https://c.com' }); expect(refused.headers['access-control-allow-origin']).to.equal(undefined); expect((await run({ origin: true }, 'GET', { origin: 'https://c.com' })).headers['access-control-allow-origin']).to.equal('https://c.com'); expect((await run({ origin: 'https://fixed.com' }, 'GET', { origin: 'https://c.com' })).headers['access-control-allow-origin']).to.equal('https://fixed.com'); }); it('sets nothing when origin is false, except the endpoint', async () => { expect((await run({ origin: false }, 'GET', { origin: 'https://c.com' })).headers).to.deep.equal({}); expect((await run({ origin: false }, 'GET', {}, 'http://localhost:3000')).headers).to.deep.equal({ 'access-control-allow-origin': 'http://localhost:3000' }); }); });