diff --git a/lib/pool.js b/lib/pool.js index 93b3158f21a..08509958069 100644 --- a/lib/pool.js +++ b/lib/pool.js @@ -34,6 +34,7 @@ class Pool extends PoolBase { socketPath, autoSelectFamily, autoSelectFamilyAttemptTimeout, + allowH2, ...options } = {}) { super() @@ -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), @@ -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 diff --git a/test/http2.js b/test/http2.js index 8fd9c616bc3..538255399eb 100644 --- a/test/http2.js +++ b/test/http2.js @@ -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 = [] @@ -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!') +})