diff --git a/lib/web/fetch/headers.js b/lib/web/fetch/headers.js index 3157561b470..d726b5a5d10 100644 --- a/lib/web/fetch/headers.js +++ b/lib/web/fetch/headers.js @@ -250,7 +250,7 @@ class HeadersList { get entries () { const headers = {} - if (this[kHeadersMap].size) { + if (this[kHeadersMap].size !== 0) { for (const { name, value } of this[kHeadersMap].values()) { headers[name] = value } @@ -259,6 +259,10 @@ class HeadersList { return headers } + rawValues () { + return this[kHeadersMap].values() + } + get entriesList () { const headers = [] diff --git a/lib/web/fetch/request.js b/lib/web/fetch/request.js index 1441a58b06e..08eca21bc53 100644 --- a/lib/web/fetch/request.js +++ b/lib/web/fetch/request.js @@ -477,9 +477,8 @@ class Request { // 4. If headers is a Headers object, then for each header in its header // list, append header’s name/header’s value to this’s headers. if (headers instanceof HeadersList) { - for (const { 0: key, 1: val } of headers) { - // Note: The header names are already in lowercase. - headersList.append(key, val, true) + for (const { name, value } of headers.rawValues()) { + headersList.append(name, value, false) } // Note: Copy the `set-cookie` meta-data. headersList.cookies = headers.cookies diff --git a/test/fetch/headers-case.js b/test/fetch/headers-case.js index bd9770b755f..78c10b3d547 100644 --- a/test/fetch/headers-case.js +++ b/test/fetch/headers-case.js @@ -1,13 +1,14 @@ 'use strict' -const { fetch, Headers } = require('../..') +const { fetch, Headers, Request } = require('../..') const { createServer } = require('node:http') const { once } = require('node:events') const { test } = require('node:test') const { tspl } = require('@matteo.collina/tspl') +const { closeServerAsPromise } = require('../utils/node-http') test('Headers retain keys case-sensitive', async (t) => { - const assert = tspl(t, { plan: 3 }) + const assert = tspl(t, { plan: 4 }) const server = createServer((req, res) => { assert.ok(req.rawHeaders.includes('Content-Type')) @@ -15,16 +16,17 @@ test('Headers retain keys case-sensitive', async (t) => { res.end() }).listen(0) - t.after(() => server.close()) + t.after(closeServerAsPromise(server)) await once(server, 'listening') + const url = `http://localhost:${server.address().port}` for (const headers of [ new Headers([['Content-Type', 'text/plain']]), { 'Content-Type': 'text/plain' }, [['Content-Type', 'text/plain']] ]) { - await fetch(`http://localhost:${server.address().port}`, { - headers - }) + await fetch(url, { headers }) } + // see https://github.com/nodejs/undici/pull/3183 + await fetch(new Request(url, { headers: [['Content-Type', 'text/plain']] }), { method: 'GET' }) })