Skip to content

Commit d2b332a

Browse files
authored
support array of headers in WrapHandler (#3941)
* support array of headers in WrapHandler * fixup
1 parent dd7473c commit d2b332a

File tree

3 files changed

+37
-8
lines changed

3 files changed

+37
-8
lines changed

lib/handler/wrap-handler.js

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,7 @@ module.exports = class WrapHandler {
5353
onRequestUpgrade (controller, statusCode, headers, socket) {
5454
const rawHeaders = []
5555
for (const [key, val] of Object.entries(headers)) {
56-
// TODO (fix): What if val is Array
57-
rawHeaders.push(Buffer.from(key), Buffer.from(val))
56+
rawHeaders.push(Buffer.from(key), Array.isArray(val) ? val.map(v => Buffer.from(v)) : Buffer.from(val))
5857
}
5958

6059
this.#handler.onUpgrade?.(statusCode, rawHeaders, socket)
@@ -63,8 +62,7 @@ module.exports = class WrapHandler {
6362
onResponseStart (controller, statusCode, headers, statusMessage) {
6463
const rawHeaders = []
6564
for (const [key, val] of Object.entries(headers)) {
66-
// TODO (fix): What if val is Array
67-
rawHeaders.push(Buffer.from(key), Buffer.from(val))
65+
rawHeaders.push(Buffer.from(key), Array.isArray(val) ? val.map(v => Buffer.from(v)) : Buffer.from(val))
6866
}
6967

7068
if (this.#handler.onHeaders?.(statusCode, rawHeaders, () => controller.resume(), statusMessage) === false) {
@@ -81,8 +79,7 @@ module.exports = class WrapHandler {
8179
onResponseEnd (controller, trailers) {
8280
const rawTrailers = []
8381
for (const [key, val] of Object.entries(trailers)) {
84-
// TODO (fix): What if val is Array
85-
rawTrailers.push(Buffer.from(key), Buffer.from(val))
82+
rawTrailers.push(Buffer.from(key), Array.isArray(val) ? val.map(v => Buffer.from(v)) : Buffer.from(val))
8683
}
8784

8885
this.#handler.onComplete?.(rawTrailers)

test/client-node-max-header-size.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ describe("Node.js' --max-http-header-size cli option", () => {
2525

2626
test("respect Node.js' --max-http-header-size", async (t) => {
2727
t = tspl(t, { plan: 6 })
28-
const command = 'node -e "require(\'.\').request(\'http://localhost:' + server.address().port + '\')"'
28+
const command = 'node --disable-warning=ExperimentalWarning -e "require(\'.\').request(\'http://localhost:' + server.address().port + '\')"'
2929

3030
exec(`${command} --max-http-header-size=1`, { stdio: 'pipe' }, (err, stdout, stderr) => {
3131
t.strictEqual(err.code, 1)
@@ -44,7 +44,7 @@ describe("Node.js' --max-http-header-size cli option", () => {
4444

4545
test('--max-http-header-size with Client API', async (t) => {
4646
t = tspl(t, { plan: 6 })
47-
const command = 'node -e "new (require(\'.\').Client)(new URL(\'http://localhost:200\'))"'
47+
const command = 'node --disable-warning=ExperimentalWarning -e "new (require(\'.\').Client)(new URL(\'http://localhost:200\'))"'
4848

4949
exec(`${command} --max-http-header-size=0`, { stdio: 'pipe' }, (err, stdout, stderr) => {
5050
t.strictEqual(err.code, 1)

test/issue-3934.js

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
'use strict'
2+
3+
const { test } = require('node:test')
4+
const { createServer } = require('node:http')
5+
const { once } = require('node:events')
6+
const assert = require('node:assert')
7+
const { Agent, RetryAgent, request } = require('..')
8+
9+
// https://github.com/nodejs/undici/issues/3934
10+
test('WrapHandler works with multiple header values', async (t) => {
11+
const server = createServer(async (_req, res) => {
12+
const headers = [
13+
['set-cookie', 'a'],
14+
['set-cookie', 'b'],
15+
['set-cookie', 'c']
16+
]
17+
res.writeHead(200, headers)
18+
res.end()
19+
}).listen(0)
20+
21+
await once(server, 'listening')
22+
t.after(() => server.close())
23+
24+
const agent = new Agent()
25+
const retryAgent = new RetryAgent(agent)
26+
27+
const {
28+
headers
29+
} = await request(`http://localhost:${server.address().port}`, { dispatcher: retryAgent })
30+
31+
assert.deepStrictEqual(headers['set-cookie'], ['a', 'b', 'c'])
32+
})

0 commit comments

Comments
 (0)