Skip to content

Commit df97958

Browse files
authored
fix: πŸ› fix process http2 header (#2332)
* fix: πŸ› fix process header * test: βœ… add test multiple requests to http2 * test: πŸ’‘ remove comment * test: βœ… update test plan
1 parent b9d8368 commit df97958

File tree

2 files changed

+61
-2
lines changed

2 files changed

+61
-2
lines changed

β€Žlib/core/request.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -381,7 +381,8 @@ function processHeader (request, key, val, skipAppend = false) {
381381
key.toLowerCase() === 'content-type'
382382
) {
383383
request.contentType = val
384-
request.headers += processHeaderValue(key, val)
384+
if (skipAppend) request.headers[key] = processHeaderValue(key, val, skipAppend)
385+
else request.headers += processHeaderValue(key, val)
385386
} else if (
386387
key.length === 17 &&
387388
key.toLowerCase() === 'transfer-encoding'

β€Žtest/http2.js

+59-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ const isGreaterThanv20 = gte(process.version.slice(1), '20.0.0')
1717
// https://github.com/nodejs/node/pull/41735
1818
const hasPseudoHeadersOrderFix = gte(process.version.slice(1), '16.14.1')
1919

20-
plan(21)
20+
plan(22)
2121

2222
test('Should support H2 connection', async t => {
2323
const body = []
@@ -67,6 +67,64 @@ test('Should support H2 connection', async t => {
6767
t.equal(Buffer.concat(body).toString('utf8'), 'hello h2!')
6868
})
6969

70+
test('Should support H2 connection(multiple requests)', async t => {
71+
const server = createSecureServer(pem)
72+
73+
server.on('stream', async (stream, headers, _flags, rawHeaders) => {
74+
t.equal(headers['x-my-header'], 'foo')
75+
t.equal(headers[':method'], 'POST')
76+
const reqData = []
77+
stream.on('data', chunk => reqData.push(chunk.toString()))
78+
await once(stream, 'end')
79+
const reqBody = reqData.join('')
80+
t.equal(reqBody.length > 0, true)
81+
stream.respond({
82+
'content-type': 'text/plain; charset=utf-8',
83+
'x-custom-h2': 'hello',
84+
':status': 200
85+
})
86+
stream.end(`hello h2! ${reqBody}`)
87+
})
88+
89+
server.listen(0)
90+
await once(server, 'listening')
91+
92+
const client = new Client(`https://localhost:${server.address().port}`, {
93+
connect: {
94+
rejectUnauthorized: false
95+
},
96+
allowH2: true
97+
})
98+
99+
t.plan(21)
100+
t.teardown(server.close.bind(server))
101+
t.teardown(client.close.bind(client))
102+
103+
for (let i = 0; i < 3; i++) {
104+
const sendBody = `seq ${i}`
105+
const body = []
106+
const response = await client.request({
107+
path: '/',
108+
method: 'POST',
109+
headers: {
110+
'content-type': 'text/plain; charset=utf-8',
111+
'x-my-header': 'foo'
112+
},
113+
body: Readable.from(sendBody)
114+
})
115+
116+
response.body.on('data', chunk => {
117+
body.push(chunk)
118+
})
119+
120+
await once(response.body, 'end')
121+
t.equal(response.statusCode, 200)
122+
t.equal(response.headers['content-type'], 'text/plain; charset=utf-8')
123+
t.equal(response.headers['x-custom-h2'], 'hello')
124+
t.equal(Buffer.concat(body).toString('utf8'), `hello h2! ${sendBody}`)
125+
}
126+
})
127+
70128
test('Should support H2 connection (headers as array)', async t => {
71129
const body = []
72130
const server = createSecureServer(pem)

0 commit comments

Comments
Β (0)