fix(context): fixed bug raw `Response` content-type is overwritten (#836)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Yusuke Wada · committed by GitHub · parent 1908be8 · Browse files · 4 files·+33 added−3 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
4 files+33 added−3 removed
- tests:+31 added−3 removed
- other:+2 added−0 removed
Provenance
- Commit
4f9d7f2- Tree
cc43fff- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
1908be8
4 files changed+33 added−3 removed
modified:deno_dist/context.ts+1 added
64 unmodified lines65 }66 67 set res(_res: Response) {68 if (this._res) {69+ this._res.headers.delete('content-type')70 this._res.headers.forEach((v, k) => {71 _res.headers.set(k, v)72 })73 }177 unmodified linesmodified:src/context.test.ts+6 added-3 removed
121 unmodified linesExpand all12212312412512612612712812913013113213313413513613713847 unmodified lines1861871881891851901911881921931941953 unmodified lines121 unmodified linesExpand all })
it('Should append the previous headers to new Response', () => { c.res.headers.set('x-Custom1', 'Message1') const res2 = new Response('foo2') const res2 = new Response('foo2', { headers: { 'Content-Type': 'application/json', }, }) res2.headers.set('x-Custom2', 'Message2') c.res = res2 expect(c.res.headers.get('x-Custom1')).toBe('Message1') expect(c.res.headers.get('Content-Type')).toBe('application/json') })
it('Should return 200 response', async () => { const res = c.text('Text')47 unmodified lines c = new Context(req) }) it('Should return only one content-type value', async () => { c.header('Content-Type', 'foo') c.header('content-type', 'foo') const res = c.html('foo') expect(res.headers.get('Content-Type')).toBe('text/html; charset=UTF-8') expect(res.headers.get('content-type')).toBe('text/html; charset=UTF-8') }) it('Should rewrite header values correctly', async () => { c.res = c.html('foo') const res = c.text('foo')3 unmodified linesmodified:src/context.ts+1 added
64 unmodified lines65 }66 67 set res(_res: Response) {68 if (this._res) {69+ this._res.headers.delete('content-type')70 this._res.headers.forEach((v, k) => {71 _res.headers.set(k, v)72 })73 }177 unmodified linesmodified:src/hono.test.ts+25 added
433 unmodified lines434 app.get('/hello', (c) => {435 return c.text('hello')436 })437 438+ app.use('/json/*', async (c, next) => {439+ c.res.headers.append('foo', 'bar')440+ await next()441+ })442+443+ app.get('/json', (c) => {444+ // With a raw response445+ return new Response(446+ JSON.stringify({447+ message: 'hello',448+ }),449+ {450+ headers: {451+ 'content-type': 'application/json',452+ },453+ }454+ )455+ })456+457 app.get('/hello/:message', (c) => {458 const message = c.req.param('message')459 return c.text(`${message}`)460 })22 unmodified lines483 expect(res.headers.get('x-custom')).toBe('root')484 expect(res.headers.get('x-message-2')).toBe('custom-header-2')485 })486 487+ it('should return correct the content-type header', async () => {488+ const res = await app.request('http://localhost/json')489+ expect(res.status).toBe(200)490+ expect(res.headers.get('content-type')).toMatch(/^application\/json/)491+ })492+493 it('not found', async () => {494 const res = await app.request('http://localhost/foo')495 expect(res.status).toBe(404)496 expect(await res.text()).toBe('Not Found Foo')949 unmodified lines