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: keep raw header name #3183

Merged
merged 8 commits into from
May 1, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
6 changes: 5 additions & 1 deletion lib/web/fetch/headers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand All @@ -259,6 +259,10 @@ class HeadersList {
return headers
}

rawValues () {
tsctx marked this conversation as resolved.
Show resolved Hide resolved
return this[kHeadersMap].values()
}

get entriesList () {
const headers = []

Expand Down
5 changes: 2 additions & 3 deletions lib/web/fetch/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
14 changes: 8 additions & 6 deletions test/fetch/headers-case.js
Original file line number Diff line number Diff line change
@@ -1,30 +1,32 @@
'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'))

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' })
})
Loading