Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix http2 agent #2275

Merged
merged 2 commits into from
Sep 20, 2023
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
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!')
})
Loading