-
Notifications
You must be signed in to change notification settings - Fork 1
Add tests to proxy #5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. need another space i think |
||
| } | ||
| }; | ||
| 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() { | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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)); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. to make it clearer that |
||
| })); | ||
|
|
||
| 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); | ||
| } | ||
| }); | ||
| }); | ||
There was a problem hiding this comment.
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
mochasubfolder