Skip to content

Commit d1e867e

Browse files
authored
Fix http2 agent (#2275)
1 parent 4d7c319 commit d1e867e

2 files changed

Lines changed: 54 additions & 3 deletions

File tree

lib/pool.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ class Pool extends PoolBase {
3434
socketPath,
3535
autoSelectFamily,
3636
autoSelectFamilyAttemptTimeout,
37+
allowH2,
3738
...options
3839
} = {}) {
3940
super()
@@ -54,6 +55,7 @@ class Pool extends PoolBase {
5455
connect = buildConnector({
5556
...tls,
5657
maxCachedSessions,
58+
allowH2,
5759
socketPath,
5860
timeout: connectTimeout == null ? 10e3 : connectTimeout,
5961
...(util.nodeHasAutoSelectFamily && autoSelectFamily ? { autoSelectFamily, autoSelectFamilyAttemptTimeout } : undefined),
@@ -66,7 +68,7 @@ class Pool extends PoolBase {
6668
: []
6769
this[kConnections] = connections || null
6870
this[kUrl] = util.parseOrigin(origin)
69-
this[kOptions] = { ...util.deepClone(options), connect }
71+
this[kOptions] = { ...util.deepClone(options), connect, allowH2 }
7072
this[kOptions].interceptors = options.interceptors
7173
? { ...options.interceptors }
7274
: undefined

test/http2.js

Lines changed: 51 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,11 @@ const { Writable, pipeline, PassThrough, Readable } = require('node:stream')
99
const { test, plan } = require('tap')
1010
const pem = require('https-pem')
1111

12-
const { Client } = require('..')
12+
const { Client, Agent } = require('..')
1313

1414
const isGreaterThanv20 = Number(process.version.slice(1).split('.')[0]) >= 20
1515

16-
plan(18)
16+
plan(19)
1717

1818
test('Should support H2 connection', async t => {
1919
const body = []
@@ -947,3 +947,52 @@ test(
947947
t.equal(Buffer.concat(requestChunks).toString('utf-8'), expectedBody)
948948
}
949949
)
950+
951+
test('Agent should support H2 connection', async t => {
952+
const body = []
953+
const server = createSecureServer(pem)
954+
955+
server.on('stream', (stream, headers) => {
956+
t.equal(headers['x-my-header'], 'foo')
957+
t.equal(headers[':method'], 'GET')
958+
stream.respond({
959+
'content-type': 'text/plain; charset=utf-8',
960+
'x-custom-h2': 'hello',
961+
':status': 200
962+
})
963+
stream.end('hello h2!')
964+
})
965+
966+
server.listen(0)
967+
await once(server, 'listening')
968+
969+
const client = new Agent({
970+
connect: {
971+
rejectUnauthorized: false
972+
},
973+
allowH2: true
974+
})
975+
976+
t.plan(6)
977+
t.teardown(server.close.bind(server))
978+
t.teardown(client.close.bind(client))
979+
980+
const response = await client.request({
981+
origin: `https://localhost:${server.address().port}`,
982+
path: '/',
983+
method: 'GET',
984+
headers: {
985+
'x-my-header': 'foo'
986+
}
987+
})
988+
989+
response.body.on('data', chunk => {
990+
body.push(chunk)
991+
})
992+
993+
await once(response.body, 'end')
994+
t.equal(response.statusCode, 200)
995+
t.equal(response.headers['content-type'], 'text/plain; charset=utf-8')
996+
t.equal(response.headers['x-custom-h2'], 'hello')
997+
t.equal(Buffer.concat(body).toString('utf8'), 'hello h2!')
998+
})

0 commit comments

Comments
 (0)