Skip to content

Commit

Permalink
test: make fetch test independent from internet connection
Browse files Browse the repository at this point in the history
  • Loading branch information
Uzlopak committed Oct 6, 2024
1 parent a7c037c commit 957a9d1
Showing 1 changed file with 34 additions and 14 deletions.
48 changes: 34 additions & 14 deletions test/fetch/client-node-max-header-size.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
})
})

0 comments on commit 957a9d1

Please sign in to comment.