Skip to content

Commit

Permalink
Merge pull request #10 from blacksonic/master
Browse files Browse the repository at this point in the history
feat(request): skip json encoding on non json
  • Loading branch information
sonicoder86 committed Feb 29, 2016
2 parents 094acdd + d83502b commit f2cfd79
Show file tree
Hide file tree
Showing 6 changed files with 75 additions and 21 deletions.
2 changes: 2 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
Escher Suite Api js
==================
[![Dependency Status](https://david-dm.org/emartech/escher-suiteapi-js.svg)](https://david-dm.org/emartech/escher-suiteapi-js)
[![devDependency Status](https://david-dm.org/emartech/escher-suiteapi-js/dev-status.svg)](https://david-dm.org/emartech/escher-suiteapi-js#info=devDependencies)

Usage
---------
Expand Down
12 changes: 6 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,24 +30,24 @@
},
"homepage": "https://github.com/emartech/escher-suiteapi-js",
"dependencies": {
"bluebird": "3.3.1",
"bluebird": "3.3.3",
"escher-auth": "0.2.8",
"lodash": "3.10.1",
"lodash": "4.5.1",
"logentries-logformat": "0.1.4",
"request": "2.69.0",
"timer-machine": "1.0.1"
},
"devDependencies": {
"chai": "3.0.0",
"chai": "3.5.0",
"co-mocha": "1.1.2",
"eslint": "2.2.0",
"eslint-config-emarsys": "3.0.0",
"eslint-plugin-security": "1.2.0",
"mocha": "2.2.5",
"mocha": "2.4.5",
"nyc": "5.6.0",
"pre-commit": "1.0.10",
"pre-commit": "1.1.2",
"semantic-release": "4.3.5",
"sinon": "1.15.4",
"sinon": "1.17.3",
"sinon-chai": "2.8.0"
}
}
13 changes: 10 additions & 3 deletions request.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ SuiteRequest.prototype = {

post: function(path, data) {
var options = this._getOptionsFor('POST', path);
var payload = JSON.stringify(data);
var payload = this._getPayload(data);
var signedOptions = this._signRequest(options, payload);

logger.log('send', this._getLogParameters(options));
Expand All @@ -42,7 +42,7 @@ SuiteRequest.prototype = {

put: function(path, data) {
var options = this._getOptionsFor('PUT', path);
var payload = JSON.stringify(data);
var payload = this._getPayload(data);
var signedOptions = this._signRequest(options, payload);

logger.log('send', this._getLogParameters(options));
Expand Down Expand Up @@ -89,8 +89,15 @@ SuiteRequest.prototype = {

_getLogParameters: function(options) {
return _.pick(options, ['method', 'host', 'url']);
}
},

_getPayload: function(data) {
if (this._options.getHeader('content-type').indexOf('application/json') === -1) {
return data;
}

return JSON.stringify(data);
}
};

SuiteRequest.EscherConstants = {
Expand Down
46 changes: 46 additions & 0 deletions request.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,52 @@ describe('SuiteRequest', function() {
suiteRequest.post('/path', { name: 'Almanach' });
});

it('should encode payload when content type is json', function() {
var suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions);
this.sandbox.stub(request, 'post', function(options, callback) {
try {
expect(options.body).to.eql('{"name":"Almanach"}');
callback(null, createDummyResponse());
} catch (e) {
callback(e, createDummyResponse());
}
});

return suiteRequest.post('/path', { name: 'Almanach' });
});

it('should encode payload when content type is utf8 json', function() {
var suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions);
requestOptions.setHeader(['content-type', 'application/json;charset=utf-8']);

this.sandbox.stub(request, 'post', function(options, callback) {
try {
expect(options.body).to.eql('{"name":"Almanach"}');
callback(null, createDummyResponse());
} catch (e) {
callback(e, createDummyResponse());
}
});

return suiteRequest.post('/path', { name: 'Almanach' });
});

it('should skip encoding of payload when content type is not json', function() {
var suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions);
requestOptions.setHeader(['content-type', 'text/csv']);

this.sandbox.stub(request, 'post', function(options, callback) {
try {
expect(options.body).to.eql('header1;header2');
callback(null, createDummyResponse());
} catch (e) {
callback(e, createDummyResponse());
}
});

return suiteRequest.post('/path', 'header1;header2');
});

it('signs extra headers too', function() {
requestOptions.setHeader(['extra-header', 'header-value']);
var suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions);
Expand Down
9 changes: 8 additions & 1 deletion requestOption.js
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,14 @@ SuiteRequestOption.prototype = {
.concat([headerToSet]);
},

getHeader: function(name) {
var result = _.find(this.headers, function(header) {
return header[0].toLowerCase() === name.toLowerCase();
});

return result ? result[1] : null;
},

toHash: function() {
var hash = {
port: this.port,
Expand All @@ -63,7 +71,6 @@ SuiteRequestOption.prototype = {
return existingHeader[0] !== headerKeyToSkip;
};
}

};

SuiteRequestOption.createForInternalApi = function(environment, rejectUnauthorized) {
Expand Down
14 changes: 3 additions & 11 deletions requestOption.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,13 @@ describe('SuiteRequestOption', function() {

requestOptions.setHeader(dummyHeader);

expect(requestOptions.toHash().headers).to.include(dummyHeader);
expect(requestOptions.getHeader('header-name')).to.eql('header-value');
});

it('should add default content type', function() {
var requestOptions = new SuiteRequestOption(dummyServiceConfig.host, dummyServiceConfig);

var contentTypeHeader = requestOptions.toHash().headers.filter(function(header) {
return header[0] === 'content-type';
});

expect(contentTypeHeader).to.eql([['content-type', 'application/json']]);
expect(requestOptions.getHeader('content-type')).to.eql('application/json');
});

it('should not duplicate headers with same name', function() {
Expand All @@ -38,10 +34,6 @@ describe('SuiteRequestOption', function() {

requestOptions.setHeader(expectedContentTypeHeader);

var contentTypeHeader = requestOptions.toHash().headers.filter(function(header) {
return header[0] === 'content-type';
});

expect(contentTypeHeader).to.eql([expectedContentTypeHeader]);
expect(requestOptions.getHeader('content-type')).to.eql('text/csv');
});
});

0 comments on commit f2cfd79

Please sign in to comment.