Skip to content

Commit

Permalink
fix: copy cookies when cloning haders (nodejs#1936)
Browse files Browse the repository at this point in the history
  • Loading branch information
KhafraDev authored and crysmags committed Feb 27, 2024
1 parent 16a8d68 commit 05eb224
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 0 deletions.
1 change: 1 addition & 0 deletions lib/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class HeadersList {
if (init instanceof HeadersList) {
this[kHeadersMap] = new Map(init[kHeadersMap])
this[kHeadersSortedMap] = init[kHeadersSortedMap]
this.cookies = init.cookies
} else {
this[kHeadersMap] = new Map(init)
this[kHeadersSortedMap] = null
Expand Down
20 changes: 20 additions & 0 deletions test/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
const tap = require('tap')
const { Headers, fill } = require('../../lib/fetch/headers')
const { kGuard } = require('../../lib/fetch/symbols')
const { once } = require('events')
const { fetch } = require('../..')
const { createServer } = require('http')

tap.test('Headers initialization', t => {
t.plan(8)
Expand Down Expand Up @@ -692,5 +695,22 @@ tap.test('Headers.prototype.getSetCookie', (t) => {
t.end()
})

// https://github.com/nodejs/undici/issues/1935
t.test('When Headers are cloned, so are the cookies', async (t) => {
const server = createServer((req, res) => {
res.setHeader('Set-Cookie', 'test=onetwo')
res.end('Hello World!')
}).listen(0)

await once(server, 'listening')
t.teardown(server.close.bind(server))

const res = await fetch(`http://localhost:${server.address().port}`)
const entries = Object.fromEntries(res.headers.entries())

t.same(res.headers.getSetCookie(), ['test=onetwo'])
t.ok('set-cookie' in entries)
})

t.end()
})

0 comments on commit 05eb224

Please sign in to comment.