Skip to content

Commit

Permalink
Merge pull request #28 from peter-vasarhelyi/parse-json
Browse files Browse the repository at this point in the history
feat(wrapper): parse response if content-type contains application/json
  • Loading branch information
sonicoder86 committed Jan 17, 2017
2 parents ad5138a + 8cc4d8b commit a254c04
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 1 deletion.
6 changes: 5 additions & 1 deletion wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ RequestWrapper.prototype = {
return reject(new SuiteRequestError('Empty http response', 500, response.statusMessage));
}

if (response.headers['content-type'] === 'application/json') {
if (this._isJsonResponse(response)) {
try {
response.body = JSON.parse(response.body);
} catch (ex) {
Expand Down Expand Up @@ -91,6 +91,10 @@ RequestWrapper.prototype = {

},

_isJsonResponse(response) {
return response.headers['content-type'] &&
response.headers['content-type'].indexOf('application/json') !== -1;
},

_getLogParameters: function(extraParametersToLog) {
var requestParametersToLog = _.pick(this.requestOptions, ['method', 'host', 'url']);
Expand Down
10 changes: 10 additions & 0 deletions wrapper.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,16 @@ describe('Wrapper', function() {
}
throw new Error('Error should have been thrown');
});

it('should parse JSON if content-type header contains charset too', function *() {
const testJson = { text: 'Test JSON text' };
apiResponse.headers['content-type'] = 'application/json; charset=utf-8';
apiResponse.body = JSON.stringify(testJson);

const response = yield wrapper.send();

expect(response.body).to.eql(testJson);
});
});


Expand Down

0 comments on commit a254c04

Please sign in to comment.