Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
feat: throw when passing throwOnError
  • Loading branch information
metcoder95 committed Aug 13, 2024
commit f723cc25269b4134dc774d14c52f9a1e7663150d
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