Permalink
Browse files

Make closures out of server tests

  • Loading branch information...
1 parent 8049fd0 commit ec7bcbb3820165b3c2ee448faca78f6123f5b2ba @tedeh committed Oct 7, 2013
Showing with 196 additions and 102 deletions.
  1. +196 −102 test/server.test.js
View
@@ -73,91 +73,143 @@ describe('jayson server instance', function() {
describe('event handlers', function() {
- var requestEventRequest = utils.request('add', [9, 2], 'test_request_event_id');
- it('should emit "request" upon a request', reqShouldEmit(requestEventRequest, 'request', function(req) {
- should.exist(req);
- req.id.should.equal(requestEventRequest.id);
- }));
+ (function() {
- var responseEventRequest = utils.request('add', [5, 2], 'test_response_event_id');
- it('should emit "response" upon a response', reqShouldEmit(responseEventRequest, 'response', function(req, res) {
- should.exist(req);
- should.exist(res);
- req.id.should.equal(responseEventRequest.id);
- res.result.should.equal(5 + 2);
- }));
+ var request = utils.request('add', [9, 2], 'test_request_event_id');
+ it('should emit "request" upon a request', reqShouldEmit(request, 'request', function(req) {
+ should.exist(req);
+ req.id.should.equal(request.id);
+ }));
- var batchEventRequests = [utils.request('add', [5, 2], 'test_batch_event_id')];
- it('should emit "batch" upon a batch request', reqShouldEmit(batchEventRequests, 'batch', function(batch) {
- should.exist(batch);
- batch.should.be.instanceof(Array).and.have.length(1);
- batch[0].id.should.equal(batchEventRequests[0].id);
- }));
+ }());
+
+ (function() {
+
+ var request = utils.request('add', [5, 2], 'test_response_event_id');
+ it('should emit "response" upon a response', reqShouldEmit(request, 'response', function(req, res) {
+ should.exist(req);
+ should.exist(res);
+ req.id.should.equal(request.id);
+ res.result.should.equal(5 + 2);
+ }));
+
+ }());
+
+ (function() {
+
+ var request = [utils.request('add', [5, 2], 'test_batch_event_id')];
+ it('should emit "batch" upon a batch request', reqShouldEmit(request, 'batch', function(batch) {
+ should.exist(batch);
+ batch.should.be.instanceof(Array).and.have.length(1);
+ batch[0].id.should.equal(request[0].id);
+ }));
+
+ }());
});
describe('invalid request with wrong format', function() {
- var request = 'I am a completely invalid request';
- it('should callback a "Parse Error"', reqShouldBeErrorCode(request, ServerErrors.PARSE_ERROR));
+ (function() {
+
+ var request = 'I am a completely invalid request';
+ it('should callback a "Parse Error"', reqShouldBeErrorCode(request, ServerErrors.PARSE_ERROR));
+
+ }());
});
describe('invalid request with an erroneous "jsonrpc"-property', function() {
- var requestInvalidVersion = utils.request('add', []);
- requestInvalidVersion.jsonrpc = '1.0';
- it('should callback a "Request Error" by having a wrong value', reqShouldBeErrorCode(requestInvalidVersion, ServerErrors.INVALID_REQUEST));
+ (function() {
+
+ var request = utils.request('add', []);
+ request.jsonrpc = '1.0';
+ it('should callback a "Request Error" by having a wrong value', reqShouldBeErrorCode(request, ServerErrors.INVALID_REQUEST));
+
+ }());
+
+ (function() {
+
+ var request = utils.request('add', []);
+ delete request.jsonrpc;
+ it('should callback a "Request Error" by being non-existent', reqShouldBeErrorCode(request, ServerErrors.INVALID_REQUEST));
- var requestNoProperty = utils.request('add', []);
- delete requestNoProperty.jsonrpc;
- it('should callback a "Request Error" by being non-existent', reqShouldBeErrorCode(requestNoProperty, ServerErrors.INVALID_REQUEST));
+ }());
});
describe('invalid request with an erroneous "method"-property', function() {
- var requestInvalidMethod = utils.request('add', []);
- requestInvalidMethod.method = true;
- it('should callback with a "Request Error" if it is of the wrong type', reqShouldBeErrorCode(requestInvalidMethod, ServerErrors.INVALID_REQUEST));
+ (function() {
+
+ var request = utils.request('add', []);
+ request.method = true;
+ it('should callback with a "Request Error" if it is of the wrong type', reqShouldBeErrorCode(request, ServerErrors.INVALID_REQUEST));
+
+ }());
+
+ (function() {
+
+ var request = utils.request('add', []);
+ request.method = 'subtract';
+ it('should callback with a "Method Not Found" if it refers to a non-existing method', reqShouldBeErrorCode(request, ServerErrors.METHOD_NOT_FOUND));
- var requestNoMethod = utils.request('add', []);
- requestNoMethod.method = 'subtract';
- it('should callback with a "Method Not Found" if it refers to a non-existing method', reqShouldBeErrorCode(requestNoMethod, ServerErrors.METHOD_NOT_FOUND));
+ }());
});
describe('invalid request with an erroneous "id"-property', function() {
- var requestInvalidId = utils.request('add', []);
- requestInvalidId.id = true;
- it('should callback with a "Request Error" if it is of the wrong type', reqShouldBeErrorCode(requestInvalidId, ServerErrors.INVALID_REQUEST));
+ (function() {
- var requestNoIdInvalid = utils.request('add', []);
- delete requestNoIdInvalid.id;
- requestNoIdInvalid = JSON.stringify(requestNoIdInvalid).slice(0, requestNoIdInvalid.length - 5);
- it('should callback with the "id"-property set to null if it is non-interpretable', reqShouldBeError(requestNoIdInvalid, function(err) {
- should.strictEqual(err.id, null);
- }));
+ var request = utils.request('add', []);
+ request.id = true;
+ it('should callback with a "Request Error" if it is of the wrong type', reqShouldBeErrorCode(request, ServerErrors.INVALID_REQUEST));
+
+ }());
+
+ (function() {
- var requestNoId = utils.request('add', [1, 2]);
- delete requestNoId.id;
- it('should callback empty if the request is interpretable', reqShouldBeEmpty(requestNoId));
+ var request = utils.request('add', []);
+ delete request.id;
+ request = JSON.stringify(request).slice(0, request.length - 5);
+ it('should callback with the "id"-property set to null if it is non-interpretable', reqShouldBeError(request, function(err) {
+ should.strictEqual(err.id, null);
+ }));
+
+ }());
+
+ (function() {
+
+ var request = utils.request('add', [1, 2]);
+ delete request.id;
+ it('should callback empty if the request is interpretable', reqShouldBeEmpty(request));
+
+ }());
});
describe('invalid request with wrong "params"', function() {
-
- var requestInvalidParams = utils.request('add', []);
- requestInvalidParams.params = '1';
- it('should callback with a "Request Error" if it is of the wrong type', reqShouldBeErrorCode(requestInvalidParams, ServerErrors.INVALID_REQUEST));
+
+ (function() {
+
+ var request = utils.request('add', []);
+ request.params = '1';
+ it('should callback with a "Request Error" if it is of the wrong type', reqShouldBeErrorCode(request, ServerErrors.INVALID_REQUEST));
+
+ }());
});
describe('request', function() {
- var simpleAddRequest = utils.request('add', [3, 9]);
- it('should return the expected result', reqShouldBeResult(simpleAddRequest, 3 + 9));
+ (function() {
+
+ var request = utils.request('add', [3, 9]);
+ it('should return the expected result', reqShouldBeResult(request, 3 + 9));
+
+ })()
});
@@ -174,34 +226,56 @@ describe('jayson server instance', function() {
describe('named parameters', function() {
- var namedParamsRequest = utils.request('add', {b: 2, a: 9});
- it('should return as expected', reqShouldBeResult(namedParamsRequest, 9 + 2));
+ (function() {
+
+ var request = utils.request('add', {b: 2, a: 9});
+ it('should return as expected', reqShouldBeResult(request, 9 + 2));
+
+ })();
+
+ (function() {
+
+ var request = utils.request('add', {});
+ it('should not fail when not given sufficient arguments', reqShouldBeResult(request, function(result) {
+ isNaN(result).should.be.true;
+ }));
+
+ })();
- var wrongParamsRequest = utils.request('add', {});
- it('should not fail when not given sufficient arguments', reqShouldBeResult(wrongParamsRequest, function(result) {
- isNaN(result).should.be.true;
- }));
});
describe('notification requests', function() {
- var simpleNotificationRequest = utils.request('add', [3, -3], null);
- it('should handle a valid notification request', reqShouldBeEmpty(simpleNotificationRequest));
+ (function() {
+
+ var request = utils.request('add', [3, -3], null);
+ it('should handle a valid notification request', reqShouldBeEmpty(request));
+
+ })();
+
+ (function() {
+
+ var request = utils.request('subtract', [5, 7], null);
+ it('should handle an erroneous notification request', reqShouldBeEmpty(request));
- var invalidNotificationRequest = utils.request('subtract', [5, 7], null);
- it('should handle an erroneous notification request', reqShouldBeEmpty(invalidNotificationRequest));
+ })();
});
describe('reviving and replacing', function() {
- var simpleInstanceRequestCounter = new support.Counter(5);
- var simpleInstanceRequest = utils.request('incrementCounterBy', [simpleInstanceRequestCounter, 5]);
- it('should be able to return the expected result', reqShouldBeResult(simpleInstanceRequest, function(res) {
- res.should.be.an.instanceof(support.Counter);
- res.count.should.equal(5 + 5);
- }));
+ (function() {
+
+ var counter = new support.Counter(5);
+ var request = utils.request('incrementCounterBy', [counter, 5]);
+
+ it('should be able to return the expected result', reqShouldBeResult(request, function(res) {
+ res.should.be.an.instanceof(support.Counter);
+ res.count.should.equal(5 + 5);
+ }));
+
+ })();
});
@@ -213,11 +287,16 @@ describe('jayson server instance', function() {
server.options.version = 1;
});
- var batchVersionRequests = [
- utils.request('add', [1, 1]),
- utils.request('add', [2, 2])
- ];
- it('should error when version is 1.0', reqShouldBeErrorCode(batchVersionRequests, ServerErrors.INVALID_REQUEST));
+ (function() {
+
+ var request = [
+ utils.request('add', [1, 1]),
+ utils.request('add', [2, 2])
+ ];
+
+ it('should error when version is 1.0', reqShouldBeErrorCode(request, ServerErrors.INVALID_REQUEST));
+
+ })();
});
@@ -231,42 +310,57 @@ describe('jayson server instance', function() {
});
}));
- var onlyNotificationRequests = [
- utils.request('add', [3, 4], null),
- utils.request('add', [4, 5], null)
- ];
- it('should handle a batch with only notifications', reqShouldBeEmpty(onlyNotificationRequests));
+ (function() {
+
+ var request = [
+ utils.request('add', [3, 4], null),
+ utils.request('add', [4, 5], null)
+ ];
+
+ it('should handle a batch with only notifications', reqShouldBeEmpty(request));
+
+ })();
+
+ (function() {
- var mixedRequests = [
+ var request = [
utils.request('add', [1, 1], null),
'invalid request',
utils.request('add', [2, 2])
- ];
- it('should handle mixed requests', reqShouldBe(mixedRequests, function(err, responses) {
- should.not.exist(err);
- responses.should.be.instanceof(Array).and.have.length(2);
- should.exist(responses[0]);
- should.exist(responses[0].error);
- responses[0].error.should.have.property('code', ServerErrors.INVALID_REQUEST);
- should.exist(responses[1]);
- responses[1].should.have.property('result', 2 + 2);
- }));
+ ];
- var mixedCallbackRequests = [
- utils.request('add_slow', [1, 1, true]),
- utils.request('add_slow', [1, 2, false])
- ];
- it('should be able return method invocations in correct order', reqShouldBe(mixedCallbackRequests, function(err, responses) {
- should.not.exist(err);
- should.exist(responses);
- responses.should.be.instanceof(Array).and.have.length(2);
- should.exist(responses[0]);
- should.exist(responses[0].result);
- responses[0].should.have.property('result', 2);
- should.exist(responses[1]);
- should.exist(responses[1].result);
- responses[1].should.have.property('result', 3);
- }));
+ it('should handle mixed requests', reqShouldBe(request, function(err, responses) {
+ should.not.exist(err);
+ responses.should.be.instanceof(Array).and.have.length(2);
+ should.exist(responses[0]);
+ should.exist(responses[0].error);
+ responses[0].error.should.have.property('code', ServerErrors.INVALID_REQUEST);
+ should.exist(responses[1]);
+ responses[1].should.have.property('result', 2 + 2);
+ }));
+
+ })();
+
+ (function() {
+
+ var request = [
+ utils.request('add_slow', [1, 1, true]),
+ utils.request('add_slow', [1, 2, false])
+ ];
+
+ it('should be able return method invocations in correct order', reqShouldBe(request, function(err, responses) {
+ should.not.exist(err);
+ should.exist(responses);
+ responses.should.be.instanceof(Array).and.have.length(2);
+ should.exist(responses[0]);
+ should.exist(responses[0].result);
+ responses[0].should.have.property('result', 2);
+ should.exist(responses[1]);
+ should.exist(responses[1].result);
+ responses[1].should.have.property('result', 3);
+ }));
+
+ })();
});

0 comments on commit ec7bcbb

Please sign in to comment.