Skip to content

Commit

Permalink
Fix http2 agent (#2275)
Browse files Browse the repository at this point in the history
  • Loading branch information
mcollina authored Sep 20, 2023
1 parent 4d7c319 commit d1e867e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/pool.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ class Pool extends PoolBase {
socketPath,
autoSelectFamily,
autoSelectFamilyAttemptTimeout,
allowH2,
...options
} = {}) {
super()
Expand All @@ -54,6 +55,7 @@ class Pool extends PoolBase {
connect = buildConnector({
...tls,
maxCachedSessions,
allowH2,
socketPath,
timeout: connectTimeout == null ? 10e3 : connectTimeout,
...(util.nodeHasAutoSelectFamily && autoSelectFamily ? { autoSelectFamily, autoSelectFamilyAttemptTimeout } : undefined),
Expand All @@ -66,7 +68,7 @@ class Pool extends PoolBase {
: []
this[kConnections] = connections || null
this[kUrl] = util.parseOrigin(origin)
this[kOptions] = { ...util.deepClone(options), connect }
this[kOptions] = { ...util.deepClone(options), connect, allowH2 }
this[kOptions].interceptors = options.interceptors
? { ...options.interceptors }
: undefined
Expand Down
53 changes: 51 additions & 2 deletions test/http2.js
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,11 @@ const { Writable, pipeline, PassThrough, Readable } = require('node:stream')
const { test, plan } = require('tap')
const pem = require('https-pem')

const { Client } = require('..')
const { Client, Agent } = require('..')

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

plan(18)
plan(19)

test('Should support H2 connection', async t => {
const body = []
Expand Down Expand Up @@ -947,3 +947,52 @@ test(
t.equal(Buffer.concat(requestChunks).toString('utf-8'), expectedBody)
}
)

test('Agent should support H2 connection', async t => {
const body = []
const server = createSecureServer(pem)

server.on('stream', (stream, headers) => {
t.equal(headers['x-my-header'], 'foo')
t.equal(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 Agent({
connect: {
rejectUnauthorized: false
},
allowH2: true
})

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

const response = await client.request({
origin: `https://localhost:${server.address().port}`,
path: '/',
method: 'GET',
headers: {
'x-my-header': 'foo'
}
})

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!')
})

0 comments on commit d1e867e

Please sign in to comment.