Skip to content
Merged
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
10 changes: 10 additions & 0 deletions lib/dispatcher/client-h2.js
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,16 @@ function writeH2 (client, request) {
const key = reqHeaders[n + 0]
const val = reqHeaders[n + 1]

if (key === 'cookie') {
if (headers[key] != null) {
headers[key] = Array.isArray(headers[key]) ? (headers[key].push(val), headers[key]) : [headers[key], val]
} else {
headers[key] = val
}

continue
}

if (Array.isArray(val)) {
for (let i = 0; i < val.length; i++) {
if (headers[key]) {
Expand Down
83 changes: 83 additions & 0 deletions test/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,89 @@ test('Should support H2 connection (headers as array)', async t => {
t.strictEqual(Buffer.concat(body).toString('utf8'), 'hello h2!')
})

test('Should support multiple header values with semicolon separator', async t => {
const body = []
const body2 = []
const expectedCookieHeaders = ['a=b', 'c=d', 'e=f']
const server = createSecureServer(await pem.generate({ opts: { keySize: 2048 } }))

server.on('stream', (stream, headers) => {
t.strictEqual(headers['x-my-header'], 'foo, bar')
t.strictEqual(headers['x-my-drink'], 'coffee, tea, water')
t.strictEqual(headers['x-other'], 'value')
t.strictEqual(headers['cookie'], expectedCookieHeaders.join('; '))
t.strictEqual(headers[':method'], 'GET')
stream.respond({
'content-type': 'text/plain; charset=utf-8',
'x-custom-h2': 'hello',
':status': 200
})
stream.end('hello h2!')
})

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

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

t = tspl(t, { plan: 9 * 2 })
after(() => server.close())
after(() => client.close())

const response = await client.request({
path: '/',
method: 'GET',
headers: [
'x-my-header', 'foo',
'x-my-drink', ['coffee', 'tea'],
'x-my-drink', 'water',
'X-My-Header', 'bar',
'x-other', 'value',
'cookie', expectedCookieHeaders
]
})

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

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

const response2 = await client.request({
path: '/',
method: 'GET',
headers: [
'x-my-header', 'foo',
'x-my-drink', ['coffee', 'tea'],
'cookie', 'a=b',
'x-my-drink', 'water',
'X-My-Header', 'bar',
'cookie', 'c=d',
'x-other', 'value',
'cookie', 'e=f'
]
})

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

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

test('Should support H2 connection(POST Buffer)', async t => {
const server = createSecureServer({ ...await pem.generate({ opts: { keySize: 2048 } }), allowHTTP1: false })

Expand Down
Loading