From 957a9d101d8bb6dfee59900ccff9bdc2a0107e35 Mon Sep 17 00:00:00 2001 From: Aras Abbasi Date: Sun, 6 Oct 2024 18:47:27 +0200 Subject: [PATCH] test: make fetch test independent from internet connection --- test/fetch/client-node-max-header-size.js | 48 ++++++++++++++++------- 1 file changed, 34 insertions(+), 14 deletions(-) diff --git a/test/fetch/client-node-max-header-size.js b/test/fetch/client-node-max-header-size.js index 9906c7e8b81..65d9251dc03 100644 --- a/test/fetch/client-node-max-header-size.js +++ b/test/fetch/client-node-max-header-size.js @@ -2,24 +2,44 @@ const { tspl } = require('@matteo.collina/tspl') const { exec } = require('node:child_process') -const { test } = require('node:test') +const { once } = require('node:events') +const { createServer } = require('node:http') +const { test, describe, before, after } = require('node:test') -const command = 'node -e "require(\'./undici-fetch.js\').fetch(\'https://httpbin.org/get\')"' +describe('fetch respects --max-http-header-size', () => { + let server -test("respect Node.js' --max-http-header-size", async (t) => { - t = tspl(t, { plan: 6 }) + before(async () => { + server = createServer((req, res) => { + res.writeHead(200, 'OK', { + 'Content-Length': 2 + }) + res.write('OK') + res.end() + }).listen(0) - exec(`${command} --max-http-header-size=1`, { stdio: 'pipe' }, (err, stdout, stderr) => { - t.strictEqual(err.code, 1) - t.strictEqual(stdout, '') - t.match(stderr, /UND_ERR_HEADERS_OVERFLOW/, '--max-http-header-size=1 should throw') + await once(server, 'listening') }) - exec(command, { stdio: 'pipe' }, (err, stdout, stderr) => { - t.ifError(err) - t.strictEqual(stdout, '') - t.strictEqual(stderr, '', 'default max-http-header-size should not throw') - }) + after(() => server.close()) + + test("respect Node.js' --max-http-header-size", async (t) => { + t = tspl(t, { plan: 6 }) + + const command = 'node -e "require(\'./undici-fetch.js\').fetch(\'http://localhost:' + server.address().port + '\')"' - await t.completed + exec(`${command} --max-http-header-size=1`, { stdio: 'pipe' }, (err, stdout, stderr) => { + t.strictEqual(err.code, 1) + t.strictEqual(stdout, '') + t.match(stderr, /UND_ERR_HEADERS_OVERFLOW/, '--max-http-header-size=1 should throw') + }) + + exec(command, { stdio: 'pipe' }, (err, stdout, stderr) => { + t.ifError(err) + t.strictEqual(stdout, '') + t.strictEqual(stderr, '', 'default max-http-header-size should not throw') + }) + + await t.completed + }) })