Skip to content

Commit

Permalink
fix: handle case no content type
Browse files Browse the repository at this point in the history
  • Loading branch information
Gigioliva committed Dec 8, 2024
1 parent 503ad76 commit df7635e
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/interceptor/response-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ class ResponseErrorHandler extends DecoratorHandler {
}

#checkContentType (contentType) {
return this.#contentType.indexOf(contentType) === 0
return (this.#contentType ?? '').indexOf(contentType) === 0
}

onRequestStart (controller, context) {
Expand Down
41 changes: 41 additions & 0 deletions test/interceptors/response-error.js
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,44 @@ test('should throw error for networking errors response', async () => {

assert.equal(error.code, 'ECONNREFUSED')
})

test('should throw error for error response without content type', async () => {
const server = createServer()

server.on('request', (req, res) => {
res.writeHead(400, {})
res.end()
})

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')
})

let error
try {
await client.request({
method: 'GET',
path: '/',
headers: {
'content-type': 'text/plain'
}
})
} catch (err) {
error = err
}

assert.equal(error.statusCode, 400)
assert.equal(error.message, 'Response Error')
assert.deepStrictEqual(error.body, '')
})

0 comments on commit df7635e

Please sign in to comment.