From 48c516d21f7da62aa4c652670a93a21892a5e560 Mon Sep 17 00:00:00 2001 From: Pablo Palacios Date: Mon, 27 Dec 2021 19:25:57 +0100 Subject: [PATCH] refactor: move parseResponse to http module --- libs/fetcher.client.js | 13 +------------ libs/util/http.client.js | 13 ++++++++++++- tests/unit/libs/util/http.client.js | 10 ++++------ 3 files changed, 17 insertions(+), 19 deletions(-) diff --git a/libs/fetcher.client.js b/libs/fetcher.client.js index 8e1fbf0b..b680e9f5 100644 --- a/libs/fetcher.client.js +++ b/libs/fetcher.client.js @@ -19,17 +19,6 @@ var DEFAULT_TIMEOUT = 3000; var MAX_URI_LEN = 2048; var OP_READ = 'read'; -function parseResponse(response) { - if (response && response.responseText) { - try { - return JSON.parse(response.responseText); - } catch (e) { - return null; - } - } - return null; -} - /** * A RequestClient instance represents a single fetcher request. * The constructor requires `operation` (CRUD) and `resource`. @@ -188,7 +177,7 @@ function executeRequest(request, resolve, reject) { if (err) { return reject(err); } - resolve(parseResponse(response)); + resolve(response); }; var config = Object.assign( diff --git a/libs/util/http.client.js b/libs/util/http.client.js index 5f117b70..3ad6827c 100644 --- a/libs/util/http.client.js +++ b/libs/util/http.client.js @@ -26,6 +26,17 @@ var DEFAULT_CONFIG = { var INITIAL_ATTEMPT = 0; +function parseResponse(response) { + if (response && response.responseText) { + try { + return JSON.parse(response.responseText); + } catch (e) { + return null; + } + } + return null; +} + function normalizeHeaders(rawHeaders, method, isCors) { var headers = Object.assign({}, rawHeaders); @@ -113,7 +124,7 @@ function doRequest(method, url, headers, data, config, attempt, callback) { withCredentials: config.withCredentials, on: { success: function (err, response) { - callback(null, response); + callback(null, parseResponse(response)); }, failure: function (err, response) { if (!shouldRetry(method, config, response.status, attempt)) { diff --git a/tests/unit/libs/util/http.client.js b/tests/unit/libs/util/http.client.js index d04a869b..e6460037 100644 --- a/tests/unit/libs/util/http.client.js +++ b/tests/unit/libs/util/http.client.js @@ -26,7 +26,7 @@ describe('Client HTTP', function () { describe('#Successful requests', function () { beforeEach(function () { responseStatus = 200; - mockBody = 'BODY'; + mockBody = { data: 'BODY' }; }); it('GET', function (done) { @@ -45,8 +45,7 @@ describe('Client HTTP', function () { expect(options.headers.get('X-Foo')).to.equal('foo'); expect(options.method).to.equal('GET'); expect(err).to.equal(null); - expect(response.statusCode).to.equal(200); - expect(response.responseText).to.equal('BODY'); + expect(response).to.deep.equal(mockBody); done(); }); }); @@ -81,7 +80,7 @@ describe('Client HTTP', function () { describe('#Successful CORS requests', function () { beforeEach(function () { responseStatus = 200; - mockBody = 'BODY'; + mockBody = { data: 'BODY' }; sinon.spy(global, 'Request'); }); @@ -109,8 +108,7 @@ describe('Client HTTP', function () { expect(options.headers.get('X-Foo')).to.equal('foo'); expect(options.method).to.equal('GET'); expect(err).to.equal(null); - expect(response.statusCode).to.equal(200); - expect(response.responseText).to.equal('BODY'); + expect(response).to.deep.equal(mockBody); sinon.assert.calledWith( Request,