diff --git a/package.json b/package.json index 94eba13..8e9d30d 100644 --- a/package.json +++ b/package.json @@ -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" }, "repository": { "type": "git", diff --git a/proxy.js b/proxy.js index 81abcd8..b84469f 100644 --- a/proxy.js +++ b/proxy.js @@ -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); }); @@ -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)); + } }; \ No newline at end of file diff --git a/test/mocha/proxy.js b/test/mocha/proxy.js new file mode 100644 index 0000000..bb1cd5f --- /dev/null +++ b/test/mocha/proxy.js @@ -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() { + 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); + + 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)); + })); + + 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); + } + }); +});