Skip to content

Commit

Permalink
fix header cloning bug (#3162)
Browse files Browse the repository at this point in the history
* fix header cloning bug

* Apply suggestions from code review

* Apply suggestions from code review

* Update headers.js
  • Loading branch information
tsctx committed Apr 24, 2024
1 parent dfacde8 commit f6f0787
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
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([name, cookie])
}
} 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

0 comments on commit f6f0787

Please sign in to comment.