-
Notifications
You must be signed in to change notification settings - Fork 553
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: response error interceptor broken (#3805)
* fix: response error interceptor broken Expose the interceptor in the same manner as the others. Remove `throwOnError` as it is considered an invalid argument. * cleanup * docs --------- Co-authored-by: Matteo Collina <[email protected]>
- Loading branch information
Showing
4 changed files
with
88 additions
and
242 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,84 @@ | ||
'use strict' | ||
|
||
const assert = require('assert') | ||
const { test } = require('node:test') | ||
const createResponseErrorInterceptor = require('../../lib/interceptor/response-error') | ||
|
||
test('should not error if request is not meant to throw error', async (t) => { | ||
const opts = { throwOnError: false } | ||
const handler = { | ||
onError: () => {}, | ||
onData: () => {}, | ||
onComplete: () => {} | ||
} | ||
const assert = require('node:assert') | ||
const { once } = require('node:events') | ||
const { createServer } = require('node:http') | ||
const { test, after } = require('node:test') | ||
const { interceptors, Client } = require('../..') | ||
const { responseError } = interceptors | ||
|
||
const interceptor = createResponseErrorInterceptor((opts, handler) => handler.onComplete()) | ||
test('should throw error for error response', async () => { | ||
const server = createServer() | ||
|
||
assert.doesNotThrow(() => interceptor(opts, handler)) | ||
}) | ||
server.on('request', (req, res) => { | ||
res.writeHead(400, { 'content-type': 'text/plain' }) | ||
res.end('Bad Request') | ||
}) | ||
|
||
server.listen(0) | ||
|
||
await once(server, 'listening') | ||
|
||
const client = new Client( | ||
`http://localhost:${server.address().port}` | ||
).compose(responseError()) | ||
|
||
after(async () => { | ||
await client.close() | ||
server.close() | ||
|
||
await once(server, 'close') | ||
}) | ||
|
||
test('should error if request status code is in the specified error codes', async (t) => { | ||
const opts = { throwOnError: true, statusCodes: [500] } | ||
const response = { statusCode: 500 } | ||
let capturedError | ||
const handler = { | ||
onError: (err) => { | ||
capturedError = err | ||
}, | ||
onData: () => {}, | ||
onComplete: () => {} | ||
let error | ||
try { | ||
await client.request({ | ||
method: 'GET', | ||
path: '/', | ||
headers: { | ||
'content-type': 'text/plain' | ||
} | ||
}) | ||
} catch (err) { | ||
error = err | ||
} | ||
|
||
const interceptor = createResponseErrorInterceptor((opts, handler) => { | ||
if (opts.throwOnError && opts.statusCodes.includes(response.statusCode)) { | ||
handler.onError(new Error('Response Error')) | ||
} else { | ||
handler.onComplete() | ||
} | ||
assert.equal(error.statusCode, 400) | ||
assert.equal(error.message, 'Response Error') | ||
assert.equal(error.data, 'Bad Request') | ||
}) | ||
|
||
test('should not throw error for ok response', async () => { | ||
const server = createServer() | ||
|
||
server.on('request', (req, res) => { | ||
res.writeHead(200, { 'content-type': 'text/plain' }) | ||
res.end('hello') | ||
}) | ||
|
||
interceptor({ ...opts, response }, handler) | ||
server.listen(0) | ||
|
||
await new Promise(resolve => setImmediate(resolve)) | ||
await once(server, 'listening') | ||
|
||
assert(capturedError, 'Expected error to be captured but it was not.') | ||
assert.strictEqual(capturedError.message, 'Response Error') | ||
assert.strictEqual(response.statusCode, 500) | ||
}) | ||
const client = new Client( | ||
`http://localhost:${server.address().port}` | ||
).compose(responseError()) | ||
|
||
test('should not error if request status code is not in the specified error codes', async (t) => { | ||
const opts = { throwOnError: true, statusCodes: [500] } | ||
const response = { statusCode: 404 } | ||
const handler = { | ||
onError: () => {}, | ||
onData: () => {}, | ||
onComplete: () => {} | ||
} | ||
after(async () => { | ||
await client.close() | ||
server.close() | ||
|
||
await once(server, 'close') | ||
}) | ||
|
||
const interceptor = createResponseErrorInterceptor((opts, handler) => { | ||
if (opts.throwOnError && opts.statusCodes.includes(response.statusCode)) { | ||
handler.onError(new Error('Response Error')) | ||
} else { | ||
handler.onComplete() | ||
const response = await client.request({ | ||
method: 'GET', | ||
path: '/', | ||
headers: { | ||
'content-type': 'text/plain' | ||
} | ||
}) | ||
|
||
assert.doesNotThrow(() => interceptor({ ...opts, response }, handler)) | ||
assert.equal(response.statusCode, 200) | ||
assert.equal(await response.body.text(), 'hello') | ||
}) |