Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix header cloning bug #3162

Merged
merged 4 commits into from
Apr 24, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/web/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -259,6 +259,24 @@ class HeadersList {
return headers
}

get entriesList () {
const headers = []

if (this[kHeadersMap].size !== 0) {
for (const { 0: lowerName, 1: { name, value } } of this[kHeadersMap]) {
if (lowerName === 'set-cookie') {
for (const cookie of this.cookies) {
headers.push(['set-cookie', cookie])
tsctx marked this conversation as resolved.
Show resolved Hide resolved
}
} else {
headers.push([name, value])
}
}
}

return headers
}

// https://fetch.spec.whatwg.org/#convert-header-names-to-a-sorted-lowercase-set
toSortedArray () {
const size = this[kHeadersMap].size
Expand Down Expand Up @@ -605,7 +623,7 @@ webidl.converters.HeadersInit = function (V) {
// A work-around to ensure we send the properly-cased Headers when V is a Headers object.
// Read https://github.com/nodejs/undici/pull/3159#issuecomment-2075537226 before touching, please.
if (!util.types.isProxy(V) && kHeadersList in V && iterator === Headers.prototype.entries) { // Headers object
return V[kHeadersList].entries
return V[kHeadersList].entriesList
}

if (typeof iterator === 'function') {
Expand Down
6 changes: 6 additions & 0 deletions test/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -725,6 +725,12 @@ test('Headers.prototype.getSetCookie', async (t) => {
assert.deepStrictEqual(res.headers.getSetCookie(), ['test=onetwo', 'test=onetwothree'])
assert.ok('set-cookie' in entries)
})

await t.test('When Headers are cloned, so are the cookies (Headers constructor)', () => {
const headers = new Headers([['set-cookie', 'a'], ['set-cookie', 'b']])

assert.deepStrictEqual([...headers], [...new Headers(headers)])
})
})

test('When the value is updated, update the cache', (t) => {
Expand Down
Loading