Skip to content
Myr

fix(middleware): don't use `headers.append()`, use `headers.set()` (#1129)

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

* fix(middleware): don't use `c.res.headers.append()`, use `set()` * denoify

authored by Yusuke Wada · committed by GitHub · parent ded3315 · Browse files · 12 files+66 added−13 removed
This commit

State

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

Diff stats

12 files+66 added−13 removed

  • tests:+56 added−3 removed
  • other:+10 added−10 removed

12 files+66 added−13 removed

modified:deno_dist/middleware/cache/index.ts+1 added-1 removed
modified:deno_dist/middleware/cors/index.ts+2 added-2 removed
modified:deno_dist/middleware/etag/index.ts+1 added-1 removed
modified:deno_dist/middleware/powered-by/index.ts+1 added-1 removed
modified:src/middleware/cache/index.test.ts+13 added
28 unmodified lines
29 app.get('/not-wait/', (c) => {30 return c.text('not cached')31 })3233 app.use('/wait2/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'max-age=10' }))34 app.use('/wait2/*', cache({ cacheName: 'my-app-v1', wait: true, cacheControl: 'max-age=10' }))35 app.get('/wait2/', (c) => {36 return c.text('cached')37 })3839 const ctx = new Context()4041 it('Should return cached response', async () => {42 await app.request('http://localhost/wait/')
12 unmodified lines
55 expect(res.status).toBe(200)56 expect(res.headers.get('cache-control')).toBe('max-age=10')57 expect(res.headers.get('cf-cache-status')).toBeNull()58 })5960 it('Should not return duplicate header values', async () => {61 const res = await app.request('/wait2/')62 expect(res).not.toBeNull()63 expect(res.status).toBe(200)64 expect(res.headers.get('cache-control')).toBe('max-age=10')65 })66})
modified:src/middleware/cache/index.ts+1 added-1 removed
8 unmodified lines
9 options.wait = false10 }1112 const addHeader = (response: Response) => {13 if (options.cacheControl) response.headers.append('Cache-Control', options.cacheControl)13 if (options.cacheControl) response.headers.set('Cache-Control', options.cacheControl)14 }1516 return async (c, next) => {17 const key = c.req.url
16 unmodified lines
modified:src/middleware/cors/index.test.ts+19 added
31 unmodified lines
32 )3334 app.use('/api5/*', cors())3536 app.use(37 '/api6/*',38 cors({39 origin: 'http://example.com',40 })41 )42 app.use(43 '/api6/*',44 cors({45 origin: 'http://example.com',46 })47 )4849 app.get('/api/abc', (c) => {50 return c.json({ success: true })51 })52 app.get('/api2/abc', (c) => {
101 unmodified lines
154155 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*')156 expect(res.headers.get('Vary')).toBeNull()157 })158159 it('Should not return duplicate header values', async () => {160 const res = await app.request('http://localhost/api6/abc')161162 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('http://example.com')163 })164})
modified:src/middleware/cors/index.ts+2 added-2 removed
31 unmodified lines
32 })(opts.origin)3334 return async (c, next) => {35 function set(key: string, value: string) {36 c.res.headers.append(key, value)36 c.res.headers.set(key, value)37 }3839 const allowOrigin = findAllowOrigin(c.req.headers.get('origin') || '')40 if (allowOrigin) {
35 unmodified lines
76 }77 }78 if (headers?.length) {79 set('Access-Control-Allow-Headers', headers.join(','))80 set('Vary', 'Access-Control-Request-Headers')80 c.res.headers.append('Vary', 'Access-Control-Request-Headers')81 }8283 c.res.headers.delete('Content-Length')84 c.res.headers.delete('Content-Type')
9 unmodified lines
modified:src/middleware/etag/index.test.ts+10 added
34 unmodified lines
35 app.get('/etag/ui2', (c) => {36 return c.body(new Uint8Array([1, 2, 3, 4]))37 })3839 app.use('/etag2/*', etag())40 app.use('/etag2/*', etag())41 app.get('/etag2/abc', (c) => c.text('Hono is cool'))4243 it('Should return etag header', async () => {44 let res = await app.request('http://localhost/etag/abc')45 expect(res.headers.get('ETag')).not.toBeFalsy()46 expect(res.headers.get('ETag')).toBe('"4e32298b1cb4edc595237405e5b696e105c2399a"')
45 unmodified lines
92 expect(res.status).toBe(304)93 expect(res.headers.get('Etag')).toBe(etag)94 expect(await res.text()).toBe('')95 })9697 it('Should not return duplicate etag header values', async () => {98 const res = await app.request('http://localhost/etag2/abc')99 expect(res.headers.get('ETag')).not.toBeFalsy()100 expect(res.headers.get('ETag')).toBe('"4e32298b1cb4edc595237405e5b696e105c2399a"')101 })102})
modified:src/middleware/etag/index.ts+1 added-1 removed
27 unmodified lines
28 })29 c.res.headers.delete('Content-Length')30 } else {31 c.res = new Response(clone.body, clone)32 c.res.headers.append('ETag', etag)32 c.res.headers.set('ETag', etag)33 }34 }35}
modified:src/middleware/powered-by/index.test.ts+14 added-3 removed
2 unmodified lines
34describe('Powered by Middleware', () => {5 const app = new Hono()67 app.use('*', poweredBy())8 app.get('/', (c) => c.text('root'))7 app.use('/poweredBy/*', poweredBy())8 app.get('/poweredBy', (c) => c.text('root'))910 app.use('/poweredBy2/*', poweredBy())11 app.use('/poweredBy2/*', poweredBy())12 app.get('/poweredBy2', (c) => c.text('root'))1314 it('Should return with X-Powered-By header', async () => {11 const res = await app.request('http://localhost/')15 const res = await app.request('http://localhost/poweredBy')16 expect(res).not.toBeNull()17 expect(res.status).toBe(200)18 expect(res.headers.get('X-Powered-By')).toBe('Hono')19 })2021 it('Should not return duplicate values', async () => {22 const res = await app.request('http://localhost/poweredBy2')23 expect(res).not.toBeNull()24 expect(res.status).toBe(200)25 expect(res.headers.get('X-Powered-By')).toBe('Hono')26 })27})
modified:src/middleware/powered-by/index.ts+1 added-1 removed
1 unmodified line
23export const poweredBy = (): MiddlewareHandler => {4 return async (c, next) => {5 await next()6 c.res.headers.append('X-Powered-By', 'Hono')6 c.res.headers.set('X-Powered-By', 'Hono')7 }8}