From df7635e6e285df8e254fee8ce538299894484e77 Mon Sep 17 00:00:00 2001 From: Gianluigi Oliva Date: Sat, 7 Dec 2024 21:14:44 +0100 Subject: [PATCH] fix: handle case no content type --- lib/interceptor/response-error.js | 2 +- test/interceptors/response-error.js | 41 +++++++++++++++++++++++++++++ 2 files changed, 42 insertions(+), 1 deletion(-) diff --git a/lib/interceptor/response-error.js b/lib/interceptor/response-error.js index 70f0c3f67f2..a8105aa1437 100644 --- a/lib/interceptor/response-error.js +++ b/lib/interceptor/response-error.js @@ -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) { diff --git a/test/interceptors/response-error.js b/test/interceptors/response-error.js index 996ff546098..157d5130b2f 100644 --- a/test/interceptors/response-error.js +++ b/test/interceptors/response-error.js @@ -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, '') +})