diff --git a/lib/fetch/headers.js b/lib/fetch/headers.js index cfdc8868efa..69acaaad996 100644 --- a/lib/fetch/headers.js +++ b/lib/fetch/headers.js @@ -128,7 +128,7 @@ class HeadersList { if (init instanceof HeadersList) { this[kHeadersMap] = new Map(init[kHeadersMap]) this[kHeadersSortedMap] = init[kHeadersSortedMap] - this.cookies = init.cookies + this.cookies = init.cookies === null ? null : [...init.cookies] } else { this[kHeadersMap] = new Map(init) this[kHeadersSortedMap] = null diff --git a/test/fetch/request.js b/test/fetch/request.js index acd3e6ab439..de593e8c053 100644 --- a/test/fetch/request.js +++ b/test/fetch/request.js @@ -487,4 +487,14 @@ test('request.referrer', (t) => { t.end() }) +// https://github.com/nodejs/undici/issues/2445 +test('Clone the set-cookie header when Request is passed as the first parameter and no header is passed.', (t) => { + t.plan(2) + const request = new Request('http://localhost', { headers: { 'set-cookie': 'A' } }) + const request2 = new Request(request) + request2.headers.append('set-cookie', 'B') + t.equal(request.headers.getSetCookie().join(', '), request.headers.get('set-cookie')) + t.equal(request2.headers.getSetCookie().join(', '), request2.headers.get('set-cookie')) +}) + teardown(() => process.exit())