fix(context): fix header values shifting (#463)
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 8879f24 · Browse files · 4 files·+52 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
4 files+52 added−14 removed
- tests:+38 added−0 removed
- other:+14 added−14 removed
Provenance
- Commit
e07147a- Tree
7260a9d- Author
Yusuke Wada <yusuke@kamawada.com>- Committer
GitHub <noreply@github.com>- Parent
8879f24
4 files changed+52 added−14 removed
modified:deno_dist/context.ts+7 added-7 removed
93 unmodified lines9495969798989910010110219 unmodified lines1221231241251261261271281291301311321331341341351361371381391401411421431431441451461471481491501511511521531541551561561571581591608 unmodified lines1691701711721731731741751761772 unmodified lines93 unmodified lines }
header(name: string, value: string): void { this._headers ||= {} this._headers[name] = value this._headers[name.toLowerCase()] = value if (this.finalized) { this.res.headers.set(name, value) } }19 unmodified lines this._prettySpace = space }
newResponse(data: Data | null, status: StatusCode, headers: Headers = {}): Response { const _headers = { ...this._headers, ...headers } const _headers = { ...this._headers } if (this._res) { this._res.headers.forEach((v, k) => { _headers[k] = v }) } return new Response(data, { status: status || this._status || 200, headers: _headers, headers: { ..._headers, ...headers }, }) }
body(data: Data | null, status: StatusCode = this._status, headers: Headers = {}): Response { return this.newResponse(data, status, headers) }
text(text: string, status: StatusCode = this._status, headers: Headers = {}): Response { headers['Content-Type'] ||= 'text/plain; charset=UTF-8' headers['content-type'] = 'text/plain; charset=UTF-8' return this.body(text, status, headers) }
json<T>(object: T, status: StatusCode = this._status, headers: Headers = {}): Response { const body = this._pretty ? JSON.stringify(object, null, this._prettySpace) : JSON.stringify(object) headers['Content-Type'] ||= 'application/json; charset=UTF-8' headers['content-type'] = 'application/json; charset=UTF-8' return this.body(body, status, headers) }
html(html: string, status: StatusCode = this._status, headers: Headers = {}): Response { headers['Content-Type'] ||= 'text/html; charset=UTF-8' headers['content-type'] = 'text/html; charset=UTF-8' return this.body(html, status, headers) }
redirect(location: string, status: StatusCode = 302): Response {8 unmodified lines }
cookie(name: string, value: string, opt?: CookieOptions): void { const cookie = serialize(name, value, opt) this.header('Set-Cookie', cookie) this.header('set-cookie', cookie) }
notFound(): Response | Promise<Response> { return this.notFoundHandler(this as any)2 unmodified linesmodified:src/context.test.ts+20 added
142 unmodified lines143 expect(res.headers.get('foo')).not.toBeNull()144 expect(res.headers.get('foo')).toBe('bar')145 })146 })147+148+describe('Context header', () => {149+ const req = new Request('http://localhost/')150+ let c: Context151+ beforeEach(() => {152+ c = new HonoContext(req)153+ })154+ it('Should return only one content-type value', async () => {155+ c.header('Content-Type', 'foo')156+ c.header('content-type', 'foo')157+ const res = c.html('foo')158+ expect(res.headers.get('Content-Type')).toBe('text/html; charset=UTF-8')159+ expect(res.headers.get('content-type')).toBe('text/html; charset=UTF-8')160+ })161+ it('Should rewrite header values correctly', async () => {162+ c.res = c.html('foo')163+ const res = c.text('foo')164+ expect(res.headers.get('Content-Type')).toBe('text/plain; charset=UTF-8')165+ })166+})modified:src/context.ts+7 added-7 removed
93 unmodified lines94 }95 96 header(name: string, value: string): void {97 this._headers ||= {}98- this._headers[name] = value98+ this._headers[name.toLowerCase()] = value99 if (this.finalized) {100 this.res.headers.set(name, value)101 }102 }19 unmodified lines122 this._prettySpace = space123 }124 125 newResponse(data: Data | null, status: StatusCode, headers: Headers = {}): Response {126- const _headers = { ...this._headers, ...headers }126+ const _headers = { ...this._headers }127 if (this._res) {128 this._res.headers.forEach((v, k) => {129 _headers[k] = v130 })131 }132 return new Response(data, {133 status: status || this._status || 200,134- headers: _headers,134+ headers: { ..._headers, ...headers },135 })136 }137 138 body(data: Data | null, status: StatusCode = this._status, headers: Headers = {}): Response {139 return this.newResponse(data, status, headers)140 }141 142 text(text: string, status: StatusCode = this._status, headers: Headers = {}): Response {143- headers['Content-Type'] ||= 'text/plain; charset=UTF-8'143+ headers['content-type'] = 'text/plain; charset=UTF-8'144 return this.body(text, status, headers)145 }146 147 json<T>(object: T, status: StatusCode = this._status, headers: Headers = {}): Response {148 const body = this._pretty149 ? JSON.stringify(object, null, this._prettySpace)150 : JSON.stringify(object)151- headers['Content-Type'] ||= 'application/json; charset=UTF-8'151+ headers['content-type'] = 'application/json; charset=UTF-8'152 return this.body(body, status, headers)153 }154 155 html(html: string, status: StatusCode = this._status, headers: Headers = {}): Response {156- headers['Content-Type'] ||= 'text/html; charset=UTF-8'156+ headers['content-type'] = 'text/html; charset=UTF-8'157 return this.body(html, status, headers)158 }159 160 redirect(location: string, status: StatusCode = 302): Response {8 unmodified lines169 }170 171 cookie(name: string, value: string, opt?: CookieOptions): void {172 const cookie = serialize(name, value, opt)173- this.header('Set-Cookie', cookie)173+ this.header('set-cookie', cookie)174 }175 176 notFound(): Response | Promise<Response> {177 return this.notFoundHandler(this as any)2 unmodified linesmodified:src/hono.test.ts+18 added
1017 unmodified lines1018 expect(await req.parseBody()).toEqual({ message: 'hello' })1019 expect(await res.json()).toEqual({ message: 'hello' })1020 })1021 })1022+1023+describe('Both two middleware returning response', () => {1024+ it('Should return correct Content-Type`', async () => {1025+ const app = new Hono()1026+ app.use('*', async (c, next) => {1027+ await next()1028+ return c.html('Foo')1029+ })1030+ app.get('/', (c) => {1031+ return c.text('Bar')1032+ })1033+ const res = await app.request('http://localhost/')1034+ expect(res).not.toBeNull()1035+ expect(res.status).toBe(200)1036+ expect(await res.text()).toBe('Foo')1037+ expect(res.headers.get('content-type')).toBe('text/html; charset=UTF-8')1038+ })1039+})