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 7, 2024
1 parent cac18e1 commit 9f80c0d
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 @@ -168,3 +168,44 @@ test('should throw error for error response, parsing JSON without charset', asyn
message: 'Bad Request'
})
})

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 9f80c0d

Please sign in to comment.