Skip to content
Myr

fix(cors): append Origin to Vary header on OPTIONS preflight (#5235)

Before adoption: this commit came in with the history the project adopted, so no landing records it.

authored by Nanasi · committed by GitHub · parent 546eca0 · Browse files · 2 files+29 added−1 removed
This commit

State

Before adoption: this commit came in with the history the project adopted, so no landing records it.

Diff stats

2 files+29 added−1 removed

  • tests:+28 added−0 removed
  • other:+1 added−1 removed

2 files+29 added−1 removed

modified:src/middleware/cors/index.test.ts+28 added
275 unmodified lines
276 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')277 expect(res.headers.get('Vary')).toBe('X-Custom-Vary-Value, Origin')278 })279280 it('Append "Origin" to Vary header on OPTIONS preflight, if response has some Vary header', async () => {281 const testApp = new Hono()282 testApp.use('/api/*', async (c, next) => {283 c.header('Vary', 'Accept-Encoding', { append: true })284 await next()285 })286 testApp.use(287 '/api/*',288 cors({289 origin: 'http://example.com',290 allowHeaders: ['X-Custom-Header'],291 })292 )293 testApp.all('/api/test', (c) => c.text('ok'))294295 const res = await testApp.request('http://localhost/api/test', {296 method: 'OPTIONS',297 headers: {298 Origin: 'http://example.com',299 'Access-Control-Request-Headers': 'X-Custom-Header',300 },301 })302303 expect(res.status).toBe(204)304 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')305 expect(res.headers.get('Vary')).toBe('Accept-Encoding, Origin, Access-Control-Request-Headers')306 })307308 it('Allow origins by function', async () => {309 let req = new Request('http://localhost/api4/abc', {310 headers: {311 Origin: 'http://subdomain.example.com',
221 unmodified lines
modified:src/middleware/cors/index.ts+1 added-1 removed