Skip to content
Myr

fix(context): Retain all cookies when passing `ResponseInit` to `c.body` (#2690)

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

* Add test to illustrate disappearing cookies * Append cookie headers in newResponse

authored by Jari Aarniala · committed by GitHub · parent 9540e69 · Browse files · 3 files+28 added−4 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−4 removed

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

3 files+28 added−4 removed

modified:deno_dist/context.ts+6 added-2 removed
1import type { HonoRequest } from './request.ts'2import type { Env, FetchEventLike, Input, NotFoundHandler, TypedResponse } from './types.ts'3import { HtmlEscapedCallbackPhase, resolveCallback } from './utils/html.ts'4import type { RedirectStatusCode, StatusCode } from './utils/http-status.ts'5import type { JSONValue, JSONParsed, IsAny, Simplify } from './utils/types.ts'5import type { IsAny, JSONParsed, JSONValue, Simplify } from './utils/types.ts'67type HeaderRecord = Record<string, string | string[]>8export type Data = string | ArrayBuffer | ReadableStream9
369 unmodified lines
379 const header = new Headers(arg.headers)380 if (this.#headers) {381 // If the header is set by c.header() and arg.headers, c.header() will be prioritized.382 this.#headers.forEach((v, k) => {383 if (k === 'set-cookie') {384 header.append(k, v)385 } else {383 header.set(k, v)386 header.set(k, v)387 }388 })389 }390 const headers = setHeaders(header, this.#preparedHeaders)391 return new Response(data, {
185 unmodified lines
modified:src/context.test.ts+16 added
306 unmodified lines
307 expect(res.headers.get('content-type')).toMatch(/^text\/html/)308 expect(await res.text()).toBe('<h2>Hello</h2>')309 })310311 it('c.body() should retain context cookies from context and original response', async () => {312 setCookie(c, 'context', '1')313 setCookie(c, 'context', '2')314315 const originalResponse = new Response('', {316 headers: {317 'set-cookie': 'response=1; Path=/',318 },319 })320 const res = c.body('', originalResponse)321 const cookies = res.headers.getSetCookie()322 expect(cookies.includes('context=1; Path=/')).toBe(true)323 expect(cookies.includes('context=2; Path=/')).toBe(true)324 expect(cookies.includes('response=1; Path=/')).toBe(true)325 })326327 it('c.text()', async () => {328 const originalResponse = new Response(JSON.stringify({ foo: 'bar' }))329 const res = c.text('foo', originalResponse)330 expect(res.headers.get('content-type')).toMatch(/^text\/plain/)
41 unmodified lines
modified:src/context.ts+6 added-2 removed
1import type { HonoRequest } from './request'2import type { Env, FetchEventLike, Input, NotFoundHandler, TypedResponse } from './types'3import { HtmlEscapedCallbackPhase, resolveCallback } from './utils/html'4import type { RedirectStatusCode, StatusCode } from './utils/http-status'5import type { JSONValue, JSONParsed, IsAny, Simplify } from './utils/types'5import type { IsAny, JSONParsed, JSONValue, Simplify } from './utils/types'67type HeaderRecord = Record<string, string | string[]>8export type Data = string | ArrayBuffer | ReadableStream9
369 unmodified lines
379 const header = new Headers(arg.headers)380 if (this.#headers) {381 // If the header is set by c.header() and arg.headers, c.header() will be prioritized.382 this.#headers.forEach((v, k) => {383 if (k === 'set-cookie') {384 header.append(k, v)385 } else {383 header.set(k, v)386 header.set(k, v)387 }388 })389 }390 const headers = setHeaders(header, this.#preparedHeaders)391 return new Response(data, {
185 unmodified lines