Skip to content
Myr

fix(context): set headers correctly if it has `this.#headers` (#2364)

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

* fix(context): set headers correctly if it has `this.#headers` * denoify

authored by Yusuke Wada · committed by GitHub · parent dd7561a · Browse files · 3 files+28 added−2 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+28 added−2 removed

  • tests:+12 added−0 removed
  • other:+16 added−2 removed

3 files+28 added−2 removed

modified:deno_dist/context.ts+8 added-1 removed
358 unmodified lines
359 })360 }361362 if (arg && typeof arg !== 'number') {363 const headers = setHeaders(new Headers(arg.headers), this.#preparedHeaders)363 const header = new Headers(arg.headers)364 if (this.#headers) {365 // If the header is set by c.header() and arg.headers, c.header() will be prioritized.366 this.#headers.forEach((v, k) => {367 header.set(k, v)368 })369 }370 const headers = setHeaders(header, this.#preparedHeaders)371 return new Response(data, {372 headers,373 status: arg.status ?? this.#status,374 })
183 unmodified lines
modified:src/context.test.ts+12 added
64 unmodified lines
65 const foo = res.headers.get('X-Foo')66 expect(foo).toBe('Bar, Buzz')67 })6869 it('Should set headers if already this.#headers is created by `c.header()`', async () => {70 c.header('X-Foo', 'Bar')71 c.header('X-Foo', 'Buzz', { append: true })72 const res = c.body('Hi', {73 headers: {74 'X-Message': 'Hi',75 },76 })77 expect(res.headers.get('X-Foo')).toBe('Bar, Buzz')78 expect(res.headers.get('X-Message')).toBe('Hi')79 })8081 it('c.header() - append, c.html()', async () => {82 c.header('X-Foo', 'Bar', { append: true })83 const res = await c.html('<h1>This rendered fine</h1>')84 expect(res.headers.get('content-type')).toMatch(/^text\/html/)
249 unmodified lines
modified:src/context.ts+8 added-1 removed
358 unmodified lines
359 })360 }361362 if (arg && typeof arg !== 'number') {363 const headers = setHeaders(new Headers(arg.headers), this.#preparedHeaders)363 const header = new Headers(arg.headers)364 if (this.#headers) {365 // If the header is set by c.header() and arg.headers, c.header() will be prioritized.366 this.#headers.forEach((v, k) => {367 header.set(k, v)368 })369 }370 const headers = setHeaders(header, this.#preparedHeaders)371 return new Response(data, {372 headers,373 status: arg.status ?? this.#status,374 })
183 unmodified lines