From f723cc25269b4134dc774d14c52f9a1e7663150d Mon Sep 17 00:00:00 2001 From: Carlos Fuentes Date: Tue, 13 Aug 2024 11:04:20 +0200 Subject: [PATCH] feat: throw when passing throwOnError --- lib/core/request.js | 7 ++++- test/client.js | 66 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 71 insertions(+), 2 deletions(-) diff --git a/lib/core/request.js b/lib/core/request.js index a97dadc0a09..688777e6135 100644 --- a/lib/core/request.js +++ b/lib/core/request.js @@ -41,7 +41,8 @@ class Request { bodyTimeout, reset, expectContinue, - servername + servername, + throwOnError }, handler) { if (typeof path !== 'string') { throw new InvalidArgumentError('path must be a string') @@ -81,6 +82,10 @@ class Request { throw new InvalidArgumentError('invalid expectContinue') } + if (throwOnError != null) { + throw new InvalidArgumentError('invalid throwOnError') + } + this.headersTimeout = headersTimeout this.bodyTimeout = bodyTimeout diff --git a/test/client.js b/test/client.js index 9cbda899f8c..62f2c10dc7a 100644 --- a/test/client.js +++ b/test/client.js @@ -3,7 +3,7 @@ const { tspl } = require('@matteo.collina/tspl') const { readFileSync, createReadStream } = require('node:fs') const { createServer } = require('node:http') -const { Readable } = require('node:stream') +const { Readable, PassThrough } = require('node:stream') const { test, after } = require('node:test') const { Client, errors } = require('..') const { kSocket } = require('../lib/core/symbols') @@ -320,6 +320,70 @@ test('basic get with query params partially in path', async (t) => { await t.completed }) +test('using throwOnError should throw (request)', async (t) => { + t = tspl(t, { plan: 2 }) + + const server = createServer((req, res) => { + res.statusCode = 400 + res.end('hello') + }) + after(() => server.close()) + + server.listen(0, () => { + const client = new Client(`http://localhost:${server.address().port}`, { + keepAliveTimeout: 300e3 + }) + after(() => client.close()) + + const signal = new EE() + client.request({ + signal, + path: '/', + method: 'GET', + throwOnError: true + }, (err) => { + t.strictEqual(err.message, 'invalid throwOnError') + t.strictEqual(err.code, 'UND_ERR_INVALID_ARG') + }) + }) + + await t.completed +}) + +test('using throwOnError should throw (stream)', async (t) => { + t = tspl(t, { plan: 2 }) + + const server = createServer((req, res) => { + res.statusCode = 400 + res.end('hello') + }) + after(() => server.close()) + + server.listen(0, () => { + const client = new Client(`http://localhost:${server.address().port}`, { + keepAliveTimeout: 300e3 + }) + after(() => client.close()) + + client.stream({ + path: '/', + method: 'GET', + throwOnError: true, + opaque: new PassThrough() + }, ({ opaque: pt }) => { + pt.on('data', () => { + t.fail() + }) + return pt + }, err => { + t.strictEqual(err.message, 'invalid throwOnError') + t.strictEqual(err.code, 'UND_ERR_INVALID_ARG') + }) + }) + + await t.completed +}) + test('basic head', async (t) => { t = tspl(t, { plan: 14 })