Skip to content
Closed
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
7 changes: 6 additions & 1 deletion lib/dispatcher/client-h2.js
Original file line number Diff line number Diff line change
Expand Up @@ -546,7 +546,7 @@ function buildRequestHeaders (reqHeaders) {
const headers = {}

for (let n = 0; n < reqHeaders.length; n += 2) {
const key = reqHeaders[n + 0]
const key = reqHeaders[n + 0].toLowerCase()
const val = reqHeaders[n + 1]
const current = headers[key]

Expand All @@ -560,6 +560,11 @@ function buildRequestHeaders (reqHeaders) {
continue
}

if (key === 'content-type') {
headers[key] = Array.isArray(val) ? val[val.length - 1] : val
continue
}

if (typeof val === 'string') {
headers[key] = current ? `${current}, ${val}` : val
continue
Expand Down
41 changes: 41 additions & 0 deletions test/http2-body.js
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,47 @@ test('Should handle h2 request without body', async t => {
await t.completed
})

test('Should tolerate duplicated single-value request headers with h2', async t => {
const assert = tspl(t, { plan: 3 })
const server = createSecureServer(await pem.generate({ opts: { keySize: 2048 } }))

server.on('stream', (stream, headers) => {
assert.strictEqual(headers['content-type'], 'application/json')

stream.respond({ ':status': 200 })
stream.end('ok')
})

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

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

t.after(async () => {
server.close()
await client.close()
})

const response = await client.request({
path: '/',
method: 'POST',
body: '{}',
headers: {
'Content-Type': 'application/json',
'content-type': 'application/json'
}
})

assert.strictEqual(response.statusCode, 200)
assert.strictEqual(await response.body.text(), 'ok')

await assert.completed
})

test('Should send content-length: 0 for empty h2 requests with payload-expecting methods', async t => {
const assert = tspl(t, { plan: 18 })
const methods = ['PUT', 'POST', 'PATCH', 'QUERY', 'PROPFIND', 'PROPPATCH']
Expand Down