Skip to content
Myr

fix(context): set headers values correctly (#1808)

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

* fix(context): set headers values correctly * denoify

authored by Yusuke Wada · committed by GitHub · parent 75dbd4d · Browse files · 3 files+16 added−16 removed
This commit

State

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

Diff stats

3 files+16 added−16 removed

  • tests:+10 added−0 removed
  • other:+6 added−16 removed

3 files+16 added−16 removed

modified:deno_dist/context.ts+3 added-8 removed
228 unmodified lines
229 headers: this.#preparedHeaders,230 })231 }232233 // Return Response immediately if arg is ResponseInit.233 if (arg && typeof arg !== 'number') {235 const res = new Response(data, arg)236 const contentType = this.#preparedHeaders?.['content-type']237 if (contentType) {238 res.headers.set('content-type', contentType)239 }240 return res234 this.res = new Response(data, arg)235 }236243 const status = arg ?? this.#status237 let status = typeof arg === 'number' ? arg : this.#status238 this.#preparedHeaders ??= {}239240 this.#headers ??= new Headers()241 for (const [k, v] of Object.entries(this.#preparedHeaders)) {
6 unmodified lines
248 })249 for (const [k, v] of Object.entries(this.#preparedHeaders)) {250 this.#headers.set(k, v)251 }252 status = this.#res.status253 }254255 headers ??= {}256 for (const [k, v] of Object.entries(headers)) {
218 unmodified lines
modified:src/context.test.ts+10 added
203 unmodified lines
204 c.res = await c.html('foo')205 const res = c.text('foo')206 expect(res.headers.get('Content-Type')).toMatch(/^text\/plain/)207 })208209 it('Should set header values if the #this.headers is set and the arg is ResponseInit', async () => {210 c.header('foo', 'bar')211 const res = c.body('foo', {212 headers: {213 'Content-Type': 'text/plain',214 },215 })216 expect(res.headers.get('foo')).toBe('bar')217 })218})219220describe('Pass a ResponseInit to respond methods', () => {221 const req = new HonoRequest(new Request('http://localhost/'))
124 unmodified lines
modified:src/context.ts+3 added-8 removed
228 unmodified lines
229 headers: this.#preparedHeaders,230 })231 }232233 // Return Response immediately if arg is ResponseInit.233 if (arg && typeof arg !== 'number') {235 const res = new Response(data, arg)236 const contentType = this.#preparedHeaders?.['content-type']237 if (contentType) {238 res.headers.set('content-type', contentType)239 }240 return res234 this.res = new Response(data, arg)235 }236243 const status = arg ?? this.#status237 let status = typeof arg === 'number' ? arg : this.#status238 this.#preparedHeaders ??= {}239240 this.#headers ??= new Headers()241 for (const [k, v] of Object.entries(this.#preparedHeaders)) {
6 unmodified lines
248 })249 for (const [k, v] of Object.entries(this.#preparedHeaders)) {250 this.#headers.set(k, v)251 }252 status = this.#res.status253 }254255 headers ??= {}256 for (const [k, v] of Object.entries(headers)) {
218 unmodified lines