diff --git a/lib/web/fetch/headers.js b/lib/web/fetch/headers.js index a1dab1050a8..7f2f526a9cb 100644 --- a/lib/web/fetch/headers.js +++ b/lib/web/fetch/headers.js @@ -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 @@ -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') { diff --git a/test/fetch/headers.js b/test/fetch/headers.js index 00798559e90..ac1183eeded 100644 --- a/test/fetch/headers.js +++ b/test/fetch/headers.js @@ -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) => {