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
Provenance
- Commit
ee9ccf8- Tree
6855f3d- Author
Jari Aarniala <codeflows@users.noreply.github.com>- Committer
GitHub <noreply@github.com>- Parent
9540e69
3 files changed+28 added−4 removed
modified:deno_dist/context.ts+6 added-2 removed
1 import type { HonoRequest } from './request.ts'2 import type { Env, FetchEventLike, Input, NotFoundHandler, TypedResponse } from './types.ts'3 import { HtmlEscapedCallbackPhase, resolveCallback } from './utils/html.ts'4 import type { RedirectStatusCode, StatusCode } from './utils/http-status.ts'5-import type { JSONValue, JSONParsed, IsAny, Simplify } from './utils/types.ts'5+import type { IsAny, JSONParsed, JSONValue, Simplify } from './utils/types.ts'6 7 type HeaderRecord = Record<string, string | string[]>8 export type Data = string | ArrayBuffer | ReadableStream9 369 unmodified lines379 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 linesmodified:src/context.test.ts+16 added
306 unmodified lines307 expect(res.headers.get('content-type')).toMatch(/^text\/html/)308 expect(await res.text()).toBe('<h2>Hello</h2>')309 })310 311+ it('c.body() should retain context cookies from context and original response', async () => {312+ setCookie(c, 'context', '1')313+ setCookie(c, 'context', '2')314+315+ 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+ })326+327 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 linesmodified:src/context.ts+6 added-2 removed
1 import type { HonoRequest } from './request'2 import type { Env, FetchEventLike, Input, NotFoundHandler, TypedResponse } from './types'3 import { HtmlEscapedCallbackPhase, resolveCallback } from './utils/html'4 import type { RedirectStatusCode, StatusCode } from './utils/http-status'5-import type { JSONValue, JSONParsed, IsAny, Simplify } from './utils/types'5+import type { IsAny, JSONParsed, JSONValue, Simplify } from './utils/types'6 7 type HeaderRecord = Record<string, string | string[]>8 export type Data = string | ArrayBuffer | ReadableStream9 369 unmodified lines379 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