Skip to content
Myr

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

4 files+52 added−14 removed

modified:deno_dist/context.ts+7 added-7 removed
modified:src/context.test.ts+20 added
142 unmodified lines
143 expect(res.headers.get('foo')).not.toBeNull()144 expect(res.headers.get('foo')).toBe('bar')145 })146})147148describe('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 lines
94 }9596 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 lines
122 this._prettySpace = space123 }124125 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 }137138 body(data: Data | null, status: StatusCode = this._status, headers: Headers = {}): Response {139 return this.newResponse(data, status, headers)140 }141142 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 }146147 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 }154155 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 }159160 redirect(location: string, status: StatusCode = 302): Response {
8 unmodified lines
169 }170171 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 }175176 notFound(): Response | Promise<Response> {177 return this.notFoundHandler(this as any)
2 unmodified lines
modified:src/hono.test.ts+18 added
1017 unmodified lines
1018 expect(await req.parseBody()).toEqual({ message: 'hello' })1019 expect(await res.json()).toEqual({ message: 'hello' })1020 })1021})10221023describe('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})