From 9f80c0d083a853d078fa5bcda8fd53afd42a61c2 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 89ac1ee4ee0..63ea7bceaa5 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 b0c0fb63498..21a25045bd7 100644 --- a/test/interceptors/response-error.js +++ b/test/interceptors/response-error.js @@ -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, '') +})