From ead3029c0ebaf64dbfa67a256f38a5670f5b1ba6 Mon Sep 17 00:00:00 2001 From: Salil Agrawal Date: Tue, 9 Oct 2018 12:57:51 -0700 Subject: [PATCH] [fix] content-type check. --- src/fetchUtils.js | 7 ++++--- tests/fetchUtils.spec.js | 3 +++ 2 files changed, 7 insertions(+), 3 deletions(-) diff --git a/src/fetchUtils.js b/src/fetchUtils.js index 87ddff4..1acb4c5 100644 --- a/src/fetchUtils.js +++ b/src/fetchUtils.js @@ -24,12 +24,13 @@ handlers.decode = function (response) { if (contentType.includes('application/json')) { return response.json() .then(data => handlers.finish(response, data)); - } else if (contentType.includes('text/html') || contentType.includes('text/plain')) { + } + try { return response.text() .then(data => handlers.finish(response, data)); + } catch (err) { + return Promise.reject(`Content-type ${contentType} not supported`); } - - return Promise.reject(`Content-type ${contentType} not supported`); }; /** diff --git a/tests/fetchUtils.spec.js b/tests/fetchUtils.spec.js index e2963ec..99976f4 100644 --- a/tests/fetchUtils.spec.js +++ b/tests/fetchUtils.spec.js @@ -138,6 +138,9 @@ describe('fetchUtils', () => { }); it('rejects for unhandled content types', (done) => { + mockResponse.text = jest.fn().mockImplementation(() => { + throw new Error('`text` not implemented'); + }); mockResponse.headers.set('content-type', 'some/type'); handlers.decode(mockResponse).catch(data => { expect(data).toContain('not supported');