diff --git a/lib/client.js b/lib/client.js index e002ee364a3..cafc03a09ff 100644 --- a/lib/client.js +++ b/lib/client.js @@ -1793,7 +1793,9 @@ function writeH2 (client, session, request) { ++h2State.openStreams stream.once('response', headers => { - if (request.onHeaders(Number(headers[HTTP2_HEADER_STATUS]), headers, stream.resume.bind(stream), '') === false) { + const { [HTTP2_HEADER_STATUS]: statusCode, ...realHeaders } = headers + + if (request.onHeaders(Number(statusCode), realHeaders, stream.resume.bind(stream), '') === false) { stream.pause() } }) diff --git a/test/fetch/http2.js b/test/fetch/http2.js index 9b3c95b8f1f..9f6997f821b 100644 --- a/test/fetch/http2.js +++ b/test/fetch/http2.js @@ -9,11 +9,11 @@ const { Readable } = require('node:stream') const { test, plan } = require('tap') const pem = require('https-pem') -const { Client, fetch } = require('../..') +const { Client, fetch, Headers } = require('../..') const nodeVersion = Number(process.version.split('v')[1].split('.')[0]) -plan(6) +plan(7) test('[Fetch] Issue#2311', async t => { const expectedBody = 'hello from client!' @@ -375,3 +375,41 @@ test( t.equal(Buffer.concat(requestChunks).toString('utf-8'), expectedBody) } ) + +test('Issue#2415', async (t) => { + t.plan(1) + const server = createSecureServer(pem) + + server.on('stream', async (stream, headers) => { + stream.respond({ + ':status': 200 + }) + stream.end('test') + }) + + server.listen() + await once(server, 'listening') + + const client = new Client(`https://localhost:${server.address().port}`, { + connect: { + rejectUnauthorized: false + }, + allowH2: true + }) + + const response = await fetch( + `https://localhost:${server.address().port}/`, + // Needs to be passed to disable the reject unauthorized + { + method: 'GET', + dispatcher: client + } + ) + + await response.text() + + t.teardown(server.close.bind(server)) + t.teardown(client.close.bind(client)) + + t.doesNotThrow(() => new Headers(response.headers)) +}) diff --git a/test/http2.js b/test/http2.js index a3d24f23670..71b77493255 100644 --- a/test/http2.js +++ b/test/http2.js @@ -17,7 +17,7 @@ const isGreaterThanv20 = gte(process.version.slice(1), '20.0.0') // https://github.com/nodejs/node/pull/41735 const hasPseudoHeadersOrderFix = gte(process.version.slice(1), '16.14.1') -plan(22) +plan(23) test('Should support H2 connection', async t => { const body = [] @@ -1154,3 +1154,38 @@ test( t.equal(response.statusCode, 200) } ) + +test('The h2 pseudo-headers is not included in the headers', async t => { + const server = createSecureServer(pem) + + server.on('stream', (stream, headers) => { + stream.respond({ + ':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.plan(2) + t.teardown(server.close.bind(server)) + t.teardown(client.close.bind(client)) + + const response = await client.request({ + path: '/', + method: 'GET' + }) + + await response.body.text() + + t.equal(response.statusCode, 200) + t.equal(response.headers[':status'], undefined) +})