Skip to content

Commit

Permalink
Merge pull request #35 from blacksonic/master
Browse files Browse the repository at this point in the history
Change underlying library from request to axios
  • Loading branch information
sonicoder86 authored Jan 5, 2018
2 parents f1c2edd + bec604b commit adc9fcc
Show file tree
Hide file tree
Showing 8 changed files with 315 additions and 243 deletions.
140 changes: 55 additions & 85 deletions lib/request.spec.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
'use strict';

const SuiteRequest = require('./request');
const request = require('request');
const axios = require('axios');
const Escher = require('escher-auth');

describe('SuiteRequest', function() {
Expand All @@ -17,134 +17,104 @@ describe('SuiteRequest', function() {
const createDummyResponse = function() {
return {
headers: {},
body: 'response body dummy'
data: 'response body dummy'
};
};

let requestOptions;
let requestStub;
let suiteRequest;

beforeEach(function() {
requestOptions = new SuiteRequest.Options(serviceConfig.host, serviceConfig);
requestStub = this.sandbox.stub(axios, 'request').resolves(createDummyResponse());
suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions);
});

it('should sign headers of GET request', function() {
const suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions);
it('should sign headers of GET request', function*() {
yield suiteRequest.get('/path');

this.sandbox.stub(request, 'get').callsFake(function(options, callback) {
expect(options.headers['x-ems-auth'])
.to.have.string('SignedHeaders=content-type;host;x-ems-date,');
callback(null, createDummyResponse());
});

suiteRequest.get('/path');
const requestArgument = requestStub.args[0][0];
expect(requestArgument.headers['x-ems-auth']).to.have.string('SignedHeaders=content-type;host;x-ems-date,');
});

it('should sign headers of POST request', function() {
const suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions);
it('should sign headers of POST request', function*() {
yield suiteRequest.post('/path', { name: 'Almanach' });

this.sandbox.stub(request, 'post').callsFake(function(options, callback) {
expect(options.headers['x-ems-auth'])
.to.have.string('SignedHeaders=content-type;host;x-ems-date,');
callback(null, createDummyResponse());
});

suiteRequest.post('/path', { name: 'Almanach' });
const requestArgument = requestStub.args[0][0];
expect(requestArgument.headers['x-ems-auth']).to.have.string('SignedHeaders=content-type;host;x-ems-date,');
});

it('should sign headers of DELETE request', function() {
const suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions);
it('should sign headers of DELETE request', function*() {
yield suiteRequest.delete('/path');

this.sandbox.stub(request, 'delete').callsFake(function(options, callback) {
expect(options.headers['x-ems-auth'])
.to.have.string('SignedHeaders=content-type;host;x-ems-date,');
callback(null, createDummyResponse());
});

suiteRequest.delete('/path');
const requestArgument = requestStub.args[0][0];
expect(requestArgument.headers['x-ems-auth']).to.have.string('SignedHeaders=content-type;host;x-ems-date,');
});

it('should sign headers with non string values', function() {
const suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions);
it('should sign headers with non string values', function*() {
requestOptions.setHeader(['x-customer-id', 15]);

this.sandbox.stub(request, 'post').callsFake(function(options, callback) {
expect(options.headers['x-ems-auth'])
.to.have.string('content-type;host;x-customer-id;x-ems-date,');
callback(null, createDummyResponse());
});
yield suiteRequest.post('/path', { name: 'Almanach' });

suiteRequest.post('/path', { name: 'Almanach' });
const requestArgument = requestStub.args[0][0];
expect(requestArgument.headers['x-ems-auth']).to.have.string('content-type;host;x-customer-id;x-ems-date,');
});

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

return suiteRequest.post('/path', { name: 'Almanach' });
const requestArgument = requestStub.args[0][0];
expect(requestArgument.data).to.eql('{"name":"Almanach"}');
});

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

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

return suiteRequest.post('/path', { name: 'Almanach' });
const requestArgument = requestStub.args[0][0];
expect(requestArgument.data).to.eql('{"name":"Almanach"}');
});

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

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

return suiteRequest.post('/path', 'header1;header2');
const requestArgument = requestStub.args[0][0];
expect(requestArgument.data).to.eql('header1;header2');
});

it('signs extra headers too', function() {
it('signs extra headers too', function*() {
requestOptions.setHeader(['extra-header', 'header-value']);
const suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions);

this.sandbox.stub(request, 'get').callsFake(function(options, callback) {
expect(options.headers['x-ems-auth'])
.to.have.string('SignedHeaders=content-type;extra-header;host;x-ems-date,');
callback(null, createDummyResponse());
});
yield suiteRequest.get('/path');

suiteRequest.get('/path');
const requestArgument = requestStub.args[0][0];
expect(requestArgument.headers['x-ems-auth'])
.to.have.string('SignedHeaders=content-type;extra-header;host;x-ems-date,');
});

it('should sign the payload of POST request', function() {
const suiteRequest = SuiteRequest.create('key-id', 'secret', requestOptions);
const payload = { name: 'Test' };
this.sandbox.spy(Escher.prototype, 'signRequest');
it('should pass down parameters to request call from request options', function*() {
yield suiteRequest.post('/path', { name: 'Almanach' });

const requestArgument = requestStub.args[0][0];

this.sandbox.stub(request, 'post').callsFake(function(options, callback) {
callback(null, createDummyResponse());
expect(requestArgument).to.contain({
method: 'post',
url: 'https://localhost:1234/api/path',
data: '{"name":"Almanach"}',
timeout: 15000,
maxContentLength: 10485760
});
});

it('should sign the payload of POST request', function*() {
const payload = { name: 'Test' };
this.sandbox.spy(Escher.prototype, 'signRequest');

suiteRequest.post('/path', payload);
yield suiteRequest.post('/path', payload);

expect(Escher.prototype.signRequest.callCount).to.eql(1);
const firstCall = Escher.prototype.signRequest.getCall(0);
Expand Down
6 changes: 5 additions & 1 deletion lib/requestOption.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
'use strict';

const MEGA_BYTE = 1024 * 1024;

class SuiteRequestOption {

static createForInternalApi(environment, rejectUnauthorized) {
Expand Down Expand Up @@ -33,6 +35,7 @@ class SuiteRequestOption {
this.prefix = '';
this.timeout = 'timeout' in options ? options.timeout : 15000;
this.allowEmptyResponse = false;
this.maxContentLength = options.maxContentLength || 10 * MEGA_BYTE;

if (!options) {
options = {};
Expand Down Expand Up @@ -88,7 +91,8 @@ class SuiteRequestOption {
host: this.host,
headers: this.headers,
prefix: this.prefix,
timeout: this.timeout
timeout: this.timeout,
maxContentLength: this.maxContentLength
};

if (!this.rejectUnauthorized) {
Expand Down
3 changes: 2 additions & 1 deletion lib/requestOption.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -125,7 +125,8 @@ describe('SuiteRequestOption', function() {
port: 1234,
prefix: '/api',
rejectUnauthorized: false,
timeout: 15000
timeout: 15000,
maxContentLength: 10485760
});
expect(requestOptions.toHash()).to.not.have.property('allowEmptyResponse');
});
Expand Down
22 changes: 6 additions & 16 deletions lib/testSetup.spec.js
Original file line number Diff line number Diff line change
@@ -1,31 +1,21 @@
'use strict';

var sinon = require('sinon');
const sinon = require('sinon');
const chai = require('chai');
const chaiSubset = require('chai-subset');
const chaiSinon = require('sinon-chai');

before(function() {
var chai = require('chai');
var sinonChai = require('sinon-chai');

global.expect = chai.expect;

chai.use(sinonChai);

sinon.stub.returnsWithResolve = function(data) {
return this.returns(Promise.resolve(data));
};

sinon.stub.returnsWithReject = function(error) {
return this.returns(Promise.reject(error));
};

chai.use(chaiSinon);
chai.use(chaiSubset);
});


beforeEach(function() {
this.sandbox = sinon.sandbox.create();
});


afterEach(function() {
this.sandbox.restore();
});
Loading

0 comments on commit adc9fcc

Please sign in to comment.