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 7 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 { key, value } of headers.rawValues()) {
headersList.append(key, value, false)
tsctx marked this conversation as resolved.
Show resolved Hide resolved
}
// 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'

Check failure on line 1 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / test (21, ubuntu-latest) / Test with Node.js 21 on ubuntu-latest

test/fetch/headers-case.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

Check failure on line 1 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / test (18, ubuntu-latest) / Test with Node.js 18 on ubuntu-latest

test/fetch/headers-case.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms', code: 'ERR_TEST_FAILURE' }

Check failure on line 1 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest) / Test with Node.js 20 on ubuntu-latest

test/fetch/headers-case.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

Check failure on line 1 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / test (20, macos-latest) / Test with Node.js 20 on macos-latest

test/fetch/headers-case.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

Check failure on line 1 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / test (21, macos-latest) / Test with Node.js 21 on macos-latest

test/fetch/headers-case.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

Check failure on line 1 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / test (18, macos-latest) / Test with Node.js 18 on macos-latest

test/fetch/headers-case.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms', code: 'ERR_TEST_FAILURE' }

Check failure on line 1 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 20 compiled --without-intl

test/fetch/headers-case.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

Check failure on line 1 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 21 compiled --without-intl

test/fetch/headers-case.js

[Error [ERR_TEST_FAILURE]: test timed out after 30000ms] { code: 'ERR_TEST_FAILURE', failureType: 'testTimeoutFailure', cause: 'test timed out after 30000ms' }

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

Check failure on line 31 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / test (21, ubuntu-latest) / Test with Node.js 21 on ubuntu-latest

Headers retain keys case-sensitive

[Error [ERR_TEST_FAILURE]: Cannot read properties of undefined (reading 'toLowerCase')] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: TypeError [Error]: Cannot read properties of undefined (reading 'toLowerCase') at fetch (/home/runner/work/undici/undici/index.js:111:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async TestContext.<anonymous> (/home/runner/work/undici/undici/test/fetch/headers-case.js:31:3) at async Test.run (node:internal/test_runner/test:640:9) at async startSubtest (node:internal/test_runner/harness:218:3) }

Check failure on line 31 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / test (18, ubuntu-latest) / Test with Node.js 18 on ubuntu-latest

Headers retain keys case-sensitive

[Error [ERR_TEST_FAILURE]: Cannot read properties of undefined (reading 'toLowerCase')] { failureType: 'testCodeFailure', cause: TypeError [Error]: Cannot read properties of undefined (reading 'toLowerCase') at fetch (/home/runner/work/undici/undici/index.js:111:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async TestContext.<anonymous> (/home/runner/work/undici/undici/test/fetch/headers-case.js:31:3) at async Test.run (node:internal/test_runner/test:632:9) at async startSubtest (node:internal/test_runner/harness:214:3), code: 'ERR_TEST_FAILURE' }

Check failure on line 31 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / test (20, ubuntu-latest) / Test with Node.js 20 on ubuntu-latest

Headers retain keys case-sensitive

[Error [ERR_TEST_FAILURE]: Cannot read properties of undefined (reading 'toLowerCase')] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: TypeError [Error]: Cannot read properties of undefined (reading 'toLowerCase') at fetch (/home/runner/work/undici/undici/index.js:111:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async TestContext.<anonymous> (/home/runner/work/undici/undici/test/fetch/headers-case.js:31:3) at async Test.run (node:internal/test_runner/test:640:9) at async startSubtest (node:internal/test_runner/harness:218:3) }

Check failure on line 31 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / test (20, macos-latest) / Test with Node.js 20 on macos-latest

Headers retain keys case-sensitive

[Error [ERR_TEST_FAILURE]: Cannot read properties of undefined (reading 'toLowerCase')] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: TypeError [Error]: Cannot read properties of undefined (reading 'toLowerCase') at fetch (/Users/runner/work/undici/undici/index.js:111:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async TestContext.<anonymous> (/Users/runner/work/undici/undici/test/fetch/headers-case.js:31:3) at async Test.run (node:internal/test_runner/test:640:9) at async startSubtest (node:internal/test_runner/harness:218:3) }

Check failure on line 31 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / test (21, macos-latest) / Test with Node.js 21 on macos-latest

Headers retain keys case-sensitive

[Error [ERR_TEST_FAILURE]: Cannot read properties of undefined (reading 'toLowerCase')] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: TypeError [Error]: Cannot read properties of undefined (reading 'toLowerCase') at fetch (/Users/runner/work/undici/undici/index.js:111:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async TestContext.<anonymous> (/Users/runner/work/undici/undici/test/fetch/headers-case.js:31:3) at async Test.run (node:internal/test_runner/test:640:9) at async startSubtest (node:internal/test_runner/harness:218:3) }

Check failure on line 31 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / test (18, macos-latest) / Test with Node.js 18 on macos-latest

Headers retain keys case-sensitive

[Error [ERR_TEST_FAILURE]: Cannot read properties of undefined (reading 'toLowerCase')] { failureType: 'testCodeFailure', cause: TypeError [Error]: Cannot read properties of undefined (reading 'toLowerCase') at fetch (/Users/runner/work/undici/undici/index.js:111:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async TestContext.<anonymous> (/Users/runner/work/undici/undici/test/fetch/headers-case.js:31:3) at async Test.run (node:internal/test_runner/test:632:9) at async startSubtest (node:internal/test_runner/harness:214:3), code: 'ERR_TEST_FAILURE' }

Check failure on line 31 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 20 compiled --without-intl

Headers retain keys case-sensitive

[Error [ERR_TEST_FAILURE]: Cannot read properties of undefined (reading 'toLowerCase')] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: TypeError [Error]: Cannot read properties of undefined (reading 'toLowerCase') at fetch (/home/runner/work/undici/undici/index.js:111:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async TestContext.<anonymous> (/home/runner/work/undici/undici/test/fetch/headers-case.js:31:3) at async Test.run (node:internal/test_runner/test:640:9) at async startSubtest (node:internal/test_runner/harness:218:3) }

Check failure on line 31 in test/fetch/headers-case.js

View workflow job for this annotation

GitHub Actions / Test with Node.js 21 compiled --without-intl

Headers retain keys case-sensitive

[Error [ERR_TEST_FAILURE]: Cannot read properties of undefined (reading 'toLowerCase')] { code: 'ERR_TEST_FAILURE', failureType: 'testCodeFailure', cause: TypeError [Error]: Cannot read properties of undefined (reading 'toLowerCase') at fetch (/home/runner/work/undici/undici/index.js:111:13) at process.processTicksAndRejections (node:internal/process/task_queues:95:5) at async TestContext.<anonymous> (/home/runner/work/undici/undici/test/fetch/headers-case.js:31:3) at async Test.run (node:internal/test_runner/test:640:9) at async startSubtest (node:internal/test_runner/harness:218:3) }
})
Loading