Skip to content

Commit

Permalink
feat: throw when passing throwOnError
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Aug 13, 2024
1 parent 10cefe4 commit f723cc2
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 2 deletions.
7 changes: 6 additions & 1 deletion lib/core/request.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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
Expand Down
66 changes: 65 additions & 1 deletion test/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
Expand Down Expand Up @@ -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 })

Expand Down

0 comments on commit f723cc2

Please sign in to comment.