Skip to content

Commit

Permalink
Added callback functions to the jsonrpc callbacks
Browse files Browse the repository at this point in the history
  • Loading branch information
tj committed Aug 6, 2010
1 parent 8a2d232 commit caff524
Showing 1 changed file with 19 additions and 32 deletions.
51 changes: 19 additions & 32 deletions test/jsonrpc.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,11 @@ var connect = require('connect'),
helpers = require('./helpers'),
assert = require('assert'),
http = require('http'),
jsonrpc = require('connect/middleware/jsonrpc')
jsonrpc = require('connect/middleware/jsonrpc');

function run(procedures){
var server = helpers.run(
connect.jsonrpc(procedures)
connect.jsonrpc(procedures);
);
server.call = function(obj, fn){
var req = server.request('POST', '/', { 'Content-Type': 'application/json; charset=foobar' });
Expand Down Expand Up @@ -74,17 +74,17 @@ module.exports = {

'test passing method exceptions': function(){
var server = run({
add: function(a, b){
add: function(a, b, fn){
if (arguments.length === 2) {
if (typeof a === 'number' && typeof b === 'number') {
this(null, a + b);
fn(null, a + b);
} else {
var err = new Error('Arguments must be numeric.');
err.code = jsonrpc.INVALID_PARAMS;
this(err);
fn(err);
}
} else {
this(jsonrpc.INVALID_PARAMS);
fn(jsonrpc.INVALID_PARAMS);
}
}
});
Expand Down Expand Up @@ -125,8 +125,8 @@ module.exports = {

'test methode call': function(){
var server = run({
add: function(a, b){
this(null, a + b);
add: function(a, b, fn){
fn(null, a + b);
}
});
server.call({
Expand All @@ -142,11 +142,13 @@ module.exports = {
'test variable arguments': function(){
var server = run({
add: function(){
var sum = 0;
for (var i = 0, len = arguments.length; i < len; ++i) {
var sum = 0,
fn = arguments[arguments.length - 1],
len = arguments.length - 1;
for (var i = 0; i < len; ++i) {
sum += arguments[i];
}
this(null, sum);
fn(null, sum);
}
});
server.call({
Expand All @@ -161,14 +163,10 @@ module.exports = {

'test named params': function(){
var server = run({
delay: function(ms, msg, unused){
var respond = this;
delay: function(ms, msg, unused, fn){
setTimeout(function(){
respond(null, msg);
fn(null, msg);
}, ms);
},
invalid: function( ){
this(null, 'shouldnt reach here because I dont have named param support :)');
}
});

Expand All @@ -180,26 +178,15 @@ module.exports = {
}, function(res, body){
assert.eql({ id: 1, result: 'Whoop!', jsonrpc: '2.0' }, body);
});

server.call({
jsonrpc: '2.0',
method: 'invalid',
params: { msg: 'Whoop!', ms: 50 },
id: 2
}, function(res, body){
assert.eql({ id: 2, error:
{ code: jsonrpc.INVALID_PARAMS, message: 'This service does not support named parameters.' },
jsonrpc: '2.0' }, body);
});
},

'test batch calls': function(){
var server = run({
multiply: function(a, b){
this(null, a * b);
multiply: function(a, b, fn){
fn(null, a * b);
},
sub: function(a, b){
this(null, a - b);
sub: function(a, b, fn){
fn(null, a - b);
}
});
server.call([{
Expand Down

0 comments on commit caff524

Please sign in to comment.