From fe3bc2ee668142610ac2672f8d9a1efbadf79dbf Mon Sep 17 00:00:00 2001 From: Dror Lapidot Date: Thu, 28 Jul 2016 23:24:53 +0300 Subject: [PATCH 1/3] Check that the status code of the response is 200 --- proxy.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/proxy.js b/proxy.js index 81abcd8..9e8fed1 100644 --- a/proxy.js +++ b/proxy.js @@ -14,7 +14,7 @@ exports.callEndpoint = function(appId, appSecret, endpoint, query, token, callba }; request(options, function (err, response, body) { - if (err) { + if (err || response.statusCode != 200) { var errMsg = err && err.message ? ' : ' + err.message : ''; callback(new Error('Error while trying to call platform endpoint ' + endpoint + errMsg)); return; From 2b52730c44499c6a2da63da77badb5726ff39dbf Mon Sep 17 00:00:00 2001 From: Dror Lapidot Date: Sat, 30 Jul 2016 23:42:46 +0300 Subject: [PATCH 2/3] Separate the error check and response check --- proxy.js | 15 +++++++++++++-- 1 file changed, 13 insertions(+), 2 deletions(-) diff --git a/proxy.js b/proxy.js index 9e8fed1..05f84d0 100644 --- a/proxy.js +++ b/proxy.js @@ -14,11 +14,18 @@ exports.callEndpoint = function(appId, appSecret, endpoint, query, token, callba }; request(options, function (err, response, body) { - if (err || response.statusCode != 200) { + 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 From ae86a3b258acda5ee978b0290c7a344ad49db049 Mon Sep 17 00:00:00 2001 From: Dror Lapidot Date: Mon, 8 Aug 2016 22:27:51 +0300 Subject: [PATCH 3/3] Add tests to proxy --- package.json | 6 ++-- proxy.js | 4 +-- test/mocha/proxy.js | 73 +++++++++++++++++++++++++++++++++++++++++++++ 3 files changed, 79 insertions(+), 4 deletions(-) create mode 100644 test/mocha/proxy.js 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 05f84d0..b84469f 100644 --- a/proxy.js +++ b/proxy.js @@ -13,7 +13,7 @@ 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 : ''; createError(errMsg); @@ -21,7 +21,7 @@ exports.callEndpoint = function(appId, appSecret, endpoint, query, token, callba } if (response.statusCode != 200) { - var statusCodeMsg = 'the response status code is' + response.statusCode; + var statusCodeMsg = ' : the response status code is ' + response.statusCode; createError(statusCodeMsg); return; } 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); + } + }); +});