Permalink
Browse files

Can generate requests without params. Closes #17.

  • Loading branch information...
1 parent 6d567ba commit 7f602ac293dd0bdf6499836780e735830370d403 @tedeh committed Dec 23, 2013
Showing with 24 additions and 7 deletions.
  1. +14 −6 lib/utils.js
  2. +3 −0 test/support/index.js
  3. +7 −1 test/utils.test.js
View
@@ -21,19 +21,27 @@ Utils.request = function(method, params, id, options) {
throw new TypeError(method + ' must be a string');
}
- if(!params || (typeof(params) !== 'object' && !Array.isArray(params))) {
- throw new TypeError(params + ' must be an object or an array');
- }
-
options = options || {};
var request = {
- params: params,
method: method
};
// assume that we are doing a 2.0 request unless specified differently
- if (typeof(options.version) === 'undefined' || options.version !== 1) request.jsonrpc = "2.0";
+ if(typeof(options.version) === 'undefined' || options.version !== 1) {
+ request.jsonrpc = '2.0';
+ }
+
+ if(params) {
+
+ // params given, but invalid?
+ if(typeof(params) !== 'object' && !Array.isArray(params)) {
+ throw new TypeError(params + ' must be an object, array or omitted');
+ }
+
+ request.params = params;
+
+ }
// if id was left out, generate one (null means explicit notification)
if(typeof(id) === 'undefined') {
View
@@ -27,6 +27,9 @@ exports.server.methods = {
},
empty: function(arg, callback) {
callback();
+ },
+ no_args: function(callback) {
+ callback(null, true);
}
};
View
@@ -21,10 +21,16 @@ describe('utils', function() {
it('should throw a TypeError on an invalid params argument', function() {
(function() {
- utils.request('a_method', null);
+ utils.request('a_method', true);
}).should.throw(TypeError);
});
+ it('should omit the params argument when not given', function() {
+ var request = utils.request('a_method', null);
+ request.should.have.property('method', 'a_method');
+ request.should.not.have.property('params');
+ });
+
});
describe('getParameterNames()', function() {

0 comments on commit 7f602ac

Please sign in to comment.