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
Provenance
- Commit
8f5ba57- Tree
0366435- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
ded3315
12 files changed+66 added−13 removed
modified:deno_dist/middleware/cache/index.ts+1 added-1 removed
8 unmodified lines910111213131415161716 unmodified lines8 unmodified lines options.wait = false }
const addHeader = (response: Response) => { if (options.cacheControl) response.headers.append('Cache-Control', options.cacheControl) if (options.cacheControl) response.headers.set('Cache-Control', options.cacheControl) }
return async (c, next) => { const key = c.req.url16 unmodified linesmodified:deno_dist/middleware/cors/index.ts+2 added-2 removed
31 unmodified lines3233343536363738394035 unmodified lines767778798080818283849 unmodified lines31 unmodified lines })(opts.origin)
return async (c, next) => { function set(key: string, value: string) { c.res.headers.append(key, value) c.res.headers.set(key, value) }
const allowOrigin = findAllowOrigin(c.req.headers.get('origin') || '') if (allowOrigin) {35 unmodified lines } } if (headers?.length) { set('Access-Control-Allow-Headers', headers.join(',')) set('Vary', 'Access-Control-Request-Headers') c.res.headers.append('Vary', 'Access-Control-Request-Headers') }
c.res.headers.delete('Content-Length') c.res.headers.delete('Content-Type')9 unmodified linesmodified:deno_dist/middleware/etag/index.ts+1 added-1 removed
27 unmodified lines28293031323233343527 unmodified lines }) c.res.headers.delete('Content-Length') } else { c.res = new Response(clone.body, clone) c.res.headers.append('ETag', etag) c.res.headers.set('ETag', etag) } }}modified:deno_dist/middleware/powered-by/index.ts+1 added-1 removed
123456678import type { MiddlewareHandler } from '../../types.ts'
export const poweredBy = (): MiddlewareHandler => { return async (c, next) => { await next() c.res.headers.append('X-Powered-By', 'Hono') c.res.headers.set('X-Powered-By', 'Hono') }}modified:src/middleware/cache/index.test.ts+13 added
28 unmodified lines29 app.get('/not-wait/', (c) => {30 return c.text('not cached')31 })32 33+ 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+ })38+39 const ctx = new Context()40 41 it('Should return cached response', async () => {42 await app.request('http://localhost/wait/')12 unmodified lines55 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 })59+60+ 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 lines9 options.wait = false10 }11 12 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 }15 16 return async (c, next) => {17 const key = c.req.url16 unmodified linesmodified:src/middleware/cors/index.test.ts+19 added
31 unmodified lines32 )33 34 app.use('/api5/*', cors())35 36+ 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+ )48+49 app.get('/api/abc', (c) => {50 return c.json({ success: true })51 })52 app.get('/api2/abc', (c) => {101 unmodified lines154 155 expect(res.headers.get('Access-Control-Allow-Origin')).toBe('*')156 expect(res.headers.get('Vary')).toBeNull()157 })158+159+ it('Should not return duplicate header values', async () => {160+ const res = await app.request('http://localhost/api6/abc')161+162+ 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 lines32 })(opts.origin)33 34 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 }38 39 const allowOrigin = findAllowOrigin(c.req.headers.get('origin') || '')40 if (allowOrigin) {35 unmodified lines76 }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 }82 83 c.res.headers.delete('Content-Length')84 c.res.headers.delete('Content-Type')9 unmodified linesmodified:src/middleware/etag/index.test.ts+10 added
34 unmodified lines35 app.get('/etag/ui2', (c) => {36 return c.body(new Uint8Array([1, 2, 3, 4]))37 })38 39+ app.use('/etag2/*', etag())40+ app.use('/etag2/*', etag())41+ app.get('/etag2/abc', (c) => c.text('Hono is cool'))42+43 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 lines92 expect(res.status).toBe(304)93 expect(res.headers.get('Etag')).toBe(etag)94 expect(await res.text()).toBe('')95 })96+97+ 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 lines28 })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 lines3 4 describe('Powered by Middleware', () => {5 const app = new Hono()6 7- app.use('*', poweredBy())8- app.get('/', (c) => c.text('root'))7+ app.use('/poweredBy/*', poweredBy())8+ app.get('/poweredBy', (c) => c.text('root'))9 10+ app.use('/poweredBy2/*', poweredBy())11+ app.use('/poweredBy2/*', poweredBy())12+ app.get('/poweredBy2', (c) => c.text('root'))13+14 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 })20+21+ 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 line2 3 export 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 }