From 05d36299e5e87711a0f93300ed0cf543e69c52da Mon Sep 17 00:00:00 2001 From: Carlos Fuentes Date: Wed, 13 Sep 2023 10:24:01 +0200 Subject: [PATCH 1/3] test: refactor testing --- test/fetch/http2.js | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/test/fetch/http2.js b/test/fetch/http2.js index 94de32dd36b..31eef066e70 100644 --- a/test/fetch/http2.js +++ b/test/fetch/http2.js @@ -67,7 +67,7 @@ test('[Fetch] Should handle h2 request with body (string or buffer)', async t => }) // Skipping for now, there is something odd in the way the body is handled -test('[Fetch] Should handle h2 request with body (stream)', { skip: true }, async t => { +test('[Fetch] Should handle h2 request with body (stream)', async t => { const server = createSecureServer(pem) const expectedBody = readFileSync(__filename, 'utf-8') const stream = createReadStream(__filename) @@ -78,8 +78,6 @@ test('[Fetch] Should handle h2 request with body (stream)', { skip: true }, asyn t.equal(headers[':path'], '/') t.equal(headers[':scheme'], 'https') - stream.on('data', chunk => requestChunks.push(chunk)) - stream.respond({ 'content-type': 'text/plain; charset=utf-8', 'x-custom-h2': headers['x-my-header'], @@ -87,6 +85,10 @@ test('[Fetch] Should handle h2 request with body (stream)', { skip: true }, asyn }) stream.end('hello h2!') + + for await (const chunk of stream) { + requestChunks.push(chunk) + } }) t.plan(8) From 42412dae31b2aadc54496003fc2eed490dab5e47 Mon Sep 17 00:00:00 2001 From: Carlos Fuentes Date: Wed, 13 Sep 2023 11:08:43 +0200 Subject: [PATCH 2/3] test: add GET test on fetch --- test/fetch/http2.js | 52 ++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 51 insertions(+), 1 deletion(-) diff --git a/test/fetch/http2.js b/test/fetch/http2.js index 31eef066e70..7524dcfbd53 100644 --- a/test/fetch/http2.js +++ b/test/fetch/http2.js @@ -11,7 +11,57 @@ const pem = require('https-pem') const { Client, fetch } = require('../..') -plan(4) +plan(5) + +test('[Fetch] Simple GET with h2', async t => { + const server = createSecureServer(pem) + const expectedRequestBody = 'hello h2!' + + server.on('stream', async (stream, headers) => { + stream.respond({ + 'content-type': 'text/plain; charset=utf-8', + 'x-custom-h2': headers['x-my-header'], + 'x-method': headers[':method'], + ':status': 200 + }) + + stream.end(expectedRequestBody) + }) + + t.plan(3) + + 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, + headers: { + 'x-my-header': 'foo', + 'content-type': 'text-plain' + } + } + ) + + const responseBody = await response.text() + + t.teardown(server.close.bind(server)) + t.teardown(client.close.bind(client)) + + t.equal(responseBody, expectedRequestBody) + t.equal(response.headers.get('x-method'), 'GET') + t.equal(response.headers.get('x-custom-h2'), 'foo') +}) test('[Fetch] Should handle h2 request with body (string or buffer)', async t => { const server = createSecureServer(pem) From 8680b7ee7c0c4fe31acc2d74f33227a27fa1ce57 Mon Sep 17 00:00:00 2001 From: Carlos Fuentes Date: Fri, 15 Sep 2023 09:56:46 +0200 Subject: [PATCH 3/3] test: skip on v16 --- test/fetch/http2.js | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/test/fetch/http2.js b/test/fetch/http2.js index 7524dcfbd53..e1d81d197d8 100644 --- a/test/fetch/http2.js +++ b/test/fetch/http2.js @@ -11,6 +11,8 @@ const pem = require('https-pem') const { Client, fetch } = require('../..') +const nodeVersion = Number(process.version.split('v')[1].split('.')[0]) + plan(5) test('[Fetch] Simple GET with h2', async t => { @@ -117,7 +119,7 @@ test('[Fetch] Should handle h2 request with body (string or buffer)', async t => }) // Skipping for now, there is something odd in the way the body is handled -test('[Fetch] Should handle h2 request with body (stream)', async t => { +test('[Fetch] Should handle h2 request with body (stream)', { skip: nodeVersion === 16 }, async t => { const server = createSecureServer(pem) const expectedBody = readFileSync(__filename, 'utf-8') const stream = createReadStream(__filename)