Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
"version": "0.0.1",
"dependencies": {
"jshashes": "1.0.3",
"request": "2.72.0"
"mocha": "3.0.1",
"request": "2.72.0",
"sinon": "1.17.5"
},
"description": "Proxy for accessing ShopYourWay developer platform APIs. For more details go to http://developers.shopyourway.com",
"main": "index.js",
"devDependencies": {},
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
"test": "./node_modules/.bin/mocha test/mocha"
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"test": "mocha test" should work.
might need to remove the mocha subfolder

},
"repository": {
"type": "git",
Expand Down
15 changes: 13 additions & 2 deletions proxy.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,19 @@ exports.callEndpoint = function(appId, appSecret, endpoint, query, token, callba
json: true
};

request(options, function (err, response, body) {
request.get(options, function (err, response, body) {
if (err) {
var errMsg = err && err.message ? ' : ' + err.message : '';
callback(new Error('Error while trying to call platform endpoint ' + endpoint + errMsg));
createError(errMsg);
return;
}

if (response.statusCode != 200) {
var statusCodeMsg = ' : the response status code is ' + response.statusCode;
createError(statusCodeMsg);
return;
}

callback(null, body);
});

Expand All @@ -29,4 +36,8 @@ exports.callEndpoint = function(appId, appSecret, endpoint, query, token, callba
function generateHash(str) {
return new Hashes.SHA256().hex(str);
}

function createError(errMsg) {
callback(new Error('Error while trying to call platform endpoint ' + endpoint + errMsg));
Copy link
Collaborator

@cowchimp cowchimp Oct 18, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

need another space i think

}
};
73 changes: 73 additions & 0 deletions test/mocha/proxy.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
var sinon = require('sinon');
var proxy = require("../../proxy");
var request = require("request");

describe("Proxy", function() {

describe("callEndpoint", function() {
var appId = "dummy-appdId";
var appSecret = "dummy-appSecret";
var endpoint = "dummy-endpoint";
var query = "dummy-query";
var token = "dummy-token";

it("WhenResponseReturnWithError_ShouldCallCallbackWithError", sinon.test(function() {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

use spaces- more readable

var callback = sinon.spy();
var dummyErrorMessage = "dummy-error-message";
var expectedError = new Error("Error while trying to call platform endpoint " + endpoint + " : " + dummyErrorMessage);

SetResponseWithError(this, dummyErrorMessage);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

camelCase


proxy.callEndpoint(appId, appSecret, endpoint, query, token, callback);

sinon.assert.calledOnce(callback);
sinon.assert.calledWith(callback, sinon.match.instanceOf(Error));
sinon.assert.calledWith(callback, sinon.match(function (result) {
return areErrorMessagesEqual(result, expectedError);
}, "Error"));
}));

it("WhenResponseStatusCodeIsNot200_ShouldCallCallbackWithError", sinon.test(function() {
var callback = sinon.spy();
var expectedError = new Error("Error while trying to call platform endpoint " + endpoint + " : the response status code is 500");

SetResponseStatusCode(this, 500);

proxy.callEndpoint(appId, appSecret, endpoint, query, token, callback);

sinon.assert.calledOnce(callback);
sinon.assert.calledWith(callback, sinon.match.instanceOf(Error));
sinon.assert.calledWith(callback, sinon.match(function (result) {
return areErrorMessagesEqual(result, expectedError);
}, "Error"));
}));

it("WhenResponseIsValid_ShouldCallCallbackWithBody", sinon.test(function() {
var callback = sinon.spy();
var expectedBody = "expected-body";

SetValidResponse(this, expectedBody);

proxy.callEndpoint(appId, appSecret, endpoint, query, token, callback);

sinon.assert.calledOnce(callback);
sinon.assert.calledWith(callback, sinon.match.same(null), sinon.match(expectedBody));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

to make it clearer that sinon.match.same(null) mean that errback was not called, consider putting it in a variable with a meaningful name

}));

function areErrorMessagesEqual(result, expected) {
return result.message == expected.message;
}

function SetResponseWithError(test, message) {
test.stub(request, 'get').yields({ message: message }, null, null);
}

function SetResponseStatusCode(test, statusCode) {
test.stub(request, 'get').yields(null, { statusCode: statusCode }, null);
}

function SetValidResponse(test, body) {
test.stub(request, 'get').yields(null, { statusCode: 200 }, body);
}
});
});