Skip to content

Commit dd7473c

Browse files
authored
fix: handle case no content type (#3931)
1 parent 81416fc commit dd7473c

File tree

2 files changed

+42
-1
lines changed

2 files changed

+42
-1
lines changed

lib/interceptor/response-error.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ class ResponseErrorHandler extends DecoratorHandler {
1616
}
1717

1818
#checkContentType (contentType) {
19-
return this.#contentType.indexOf(contentType) === 0
19+
return (this.#contentType ?? '').indexOf(contentType) === 0
2020
}
2121

2222
onRequestStart (controller, context) {

test/interceptors/response-error.js

+41
Original file line numberDiff line numberDiff line change
@@ -193,3 +193,44 @@ test('should throw error for networking errors response', async () => {
193193

194194
assert.equal(error.code, 'ECONNREFUSED')
195195
})
196+
197+
test('should throw error for error response without content type', async () => {
198+
const server = createServer()
199+
200+
server.on('request', (req, res) => {
201+
res.writeHead(400, {})
202+
res.end()
203+
})
204+
205+
server.listen(0)
206+
207+
await once(server, 'listening')
208+
209+
const client = new Client(
210+
`http://localhost:${server.address().port}`
211+
).compose(responseError())
212+
213+
after(async () => {
214+
await client.close()
215+
server.close()
216+
217+
await once(server, 'close')
218+
})
219+
220+
let error
221+
try {
222+
await client.request({
223+
method: 'GET',
224+
path: '/',
225+
headers: {
226+
'content-type': 'text/plain'
227+
}
228+
})
229+
} catch (err) {
230+
error = err
231+
}
232+
233+
assert.equal(error.statusCode, 400)
234+
assert.equal(error.message, 'Response Error')
235+
assert.deepStrictEqual(error.body, '')
236+
})

0 commit comments

Comments
 (0)