Skip to content
Myr

feat(cors): Enable to check origin header by a function. (#533)

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

authored by Taku Amano · committed by GitHub · parent bbb9558 · Browse files · 3 files+60 added−14 removed
This commit

State

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

Diff stats

3 files+60 added−14 removed

  • tests:+32 added−0 removed
  • other:+28 added−14 removed

3 files+60 added−14 removed

modified:deno_dist/middleware/cors/index.ts+14 added-7 removed
modified:src/middleware/cors/index.test.ts+32 added
22 unmodified lines
23 origin: ['http://example.com', 'http://example.org', 'http://example.dev'],24 })25 )2627 app.use(28 '/api4/*',29 cors({30 origin: (origin) => (origin.endsWith('.example.com') ? origin : 'http://example.com'),31 })32 )3334 app.all('/api/abc', (c) => {35 return c.json({ success: true })36 })37 app.all('/api2/abc', (c) => {
1 unmodified line
39 })40 app.all('/api3/abc', (c) => {41 return c.json({ success: true })42 })43 app.all('/api4/abc', (c) => {44 return c.json({ success: true })45 })4647 it('GET default', async () => {48 const res = await app.request('http://localhost/api/abc')49
57 unmodified lines
107 })108 res = await app.request(req)109 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')110 })111112 it('Allow origins by function', async () => {113 let req = new Request('http://localhost/api4/abc', {114 headers: {115 Origin: 'http://subdomain.example.com',116 },117 })118 let res = await app.request(req)119 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://subdomain.example.com')120121 req = new Request('http://localhost/api4/abc')122 res = await app.request(req)123 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')124125 req = new Request('http://localhost/api4/abc', {126 headers: {127 Referer: 'http://evil-example.com/',128 },129 })130 res = await app.request(req)131 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')132 })133})
modified:src/middleware/cors/index.ts+14 added-7 removed