New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
feat: Option to throw on error status codes #1453
Changes from 6 commits
b27804b
01cbde5
8f57b26
428fe87
e5f7c2b
2b4d087
b36f767
d3d6aa0
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -315,6 +315,72 @@ test('basic get with query params partially in path', (t) => { | |
}) | ||
}) | ||
|
||
test('basic get returns 400 when configured to throw on errors (callback)', (t) => { | ||
t.plan(6) | ||
|
||
const server = createServer((req, res) => { | ||
res.statusCode = 400 | ||
res.end('hello') | ||
}) | ||
t.teardown(server.close.bind(server)) | ||
|
||
server.listen(0, () => { | ||
const client = new Client(`http://localhost:${server.address().port}`, { | ||
keepAliveTimeout: 300e3 | ||
}) | ||
t.teardown(client.close.bind(client)) | ||
|
||
const signal = new EE() | ||
client.request({ | ||
signal, | ||
path: '/', | ||
method: 'GET', | ||
throwOnError: true | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We need a better name for the option, in this example is not throwing at all. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Any ideas? What aspects would you like emphasized? Axios uses There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @ronag any preference on the name? |
||
}, (err) => { | ||
t.equal(err.message, 'Response status code 400: Bad Request') | ||
t.equal(err.status, 400) | ||
t.equal(err.statusCode, 400) | ||
t.equal(err.headers.connection, 'keep-alive') | ||
t.equal(err.headers['content-length'], '5') | ||
}) | ||
t.equal(signal.listenerCount('abort'), 1) | ||
}) | ||
}) | ||
|
||
test('basic get returns 400 when configured to throw on errors (promise)', (t) => { | ||
t.plan(5) | ||
|
||
const server = createServer((req, res) => { | ||
res.writeHead(400, 'Invalid params', { 'content-type': 'text/plain' }) | ||
res.end('Invalid params') | ||
}) | ||
t.teardown(server.close.bind(server)) | ||
|
||
server.listen(0, async () => { | ||
const client = new Client(`http://localhost:${server.address().port}`, { | ||
keepAliveTimeout: 300e3 | ||
}) | ||
t.teardown(client.close.bind(client)) | ||
|
||
const signal = new EE() | ||
try { | ||
await client.request({ | ||
signal, | ||
path: '/', | ||
method: 'GET', | ||
throwOnError: true | ||
}) | ||
t.fail('Should throw an error') | ||
} catch (err) { | ||
t.equal(err.message, 'Response status code 400: Invalid params') | ||
t.equal(err.status, 400) | ||
t.equal(err.statusCode, 400) | ||
t.equal(err.headers.connection, 'keep-alive') | ||
t.equal(err.headers['content-type'], 'text/plain') | ||
} | ||
}) | ||
}) | ||
|
||
test('basic head', (t) => { | ||
t.plan(14) | ||
|
||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
this was always passed, but neither used nor documented