Skip to content

Commit

Permalink
fix: πŸ› fix process http2 header (#2332)
Browse files Browse the repository at this point in the history
* fix: πŸ› fix process header

* test: βœ… add test multiple requests to http2

* test: πŸ’‘ remove comment

* test: βœ… update test plan
  • Loading branch information
binsee authored Oct 11, 2023
1 parent b9d8368 commit df97958
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 2 deletions.
3 changes: 2 additions & 1 deletion lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -381,7 +381,8 @@ function processHeader (request, key, val, skipAppend = false) {
key.toLowerCase() === 'content-type'
) {
request.contentType = val
request.headers += processHeaderValue(key, val)
if (skipAppend) request.headers[key] = processHeaderValue(key, val, skipAppend)
else request.headers += processHeaderValue(key, val)
} else if (
key.length === 17 &&
key.toLowerCase() === 'transfer-encoding'
Expand Down
60 changes: 59 additions & 1 deletion test/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const isGreaterThanv20 = gte(process.version.slice(1), '20.0.0')
// https://github.com/nodejs/node/pull/41735
const hasPseudoHeadersOrderFix = gte(process.version.slice(1), '16.14.1')

plan(21)
plan(22)

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

test('Should support H2 connection(multiple requests)', async t => {
const server = createSecureServer(pem)

server.on('stream', async (stream, headers, _flags, rawHeaders) => {
t.equal(headers['x-my-header'], 'foo')
t.equal(headers[':method'], 'POST')
const reqData = []
stream.on('data', chunk => reqData.push(chunk.toString()))
await once(stream, 'end')
const reqBody = reqData.join('')
t.equal(reqBody.length > 0, true)
stream.respond({
'content-type': 'text/plain; charset=utf-8',
'x-custom-h2': 'hello',
':status': 200
})
stream.end(`hello h2! ${reqBody}`)
})

server.listen(0)
await once(server, 'listening')

const client = new Client(`https://localhost:${server.address().port}`, {
connect: {
rejectUnauthorized: false
},
allowH2: true
})

t.plan(21)
t.teardown(server.close.bind(server))
t.teardown(client.close.bind(client))

for (let i = 0; i < 3; i++) {
const sendBody = `seq ${i}`
const body = []
const response = await client.request({
path: '/',
method: 'POST',
headers: {
'content-type': 'text/plain; charset=utf-8',
'x-my-header': 'foo'
},
body: Readable.from(sendBody)
})

response.body.on('data', chunk => {
body.push(chunk)
})

await once(response.body, 'end')
t.equal(response.statusCode, 200)
t.equal(response.headers['content-type'], 'text/plain; charset=utf-8')
t.equal(response.headers['x-custom-h2'], 'hello')
t.equal(Buffer.concat(body).toString('utf8'), `hello h2! ${sendBody}`)
}
})

test('Should support H2 connection (headers as array)', async t => {
const body = []
const server = createSecureServer(pem)
Expand Down

0 comments on commit df97958

Please sign in to comment.