diff --git a/index.js b/index.js index 76699db..b09c1ed 100644 --- a/index.js +++ b/index.js @@ -335,7 +335,7 @@ function onMessage(bugout, identifier, wire, message) { } else if (packet.y == "r") { // rpc call debug("rpc", identifier, packet); var call = packet.c.toString(); - var argsstring = packet.a.toString(); + var argsstring = packet["a"] ? packet.a.toString() : "null"; try { var args = JSON.parse(argsstring); } catch(e) { diff --git a/test.js b/test.js index be3c850..692f20f 100644 --- a/test.js +++ b/test.js @@ -143,6 +143,31 @@ test("RPC and message passing", function(t) { }); }); +test("RPC Call without passing any message/data", function(t) { + t.plan(5); + + const bs = new Bugout({dht: false, tracker: false}); + const bc = new Bugout(bs.address(), {dht: false, tracker: false}); + + bs.register("call_without_args", function(pk,args){ + t.assert(args === null, 'args should be null, when called without args from another peer') + }); + bs.register("call_with_args", function(pk,args){ + t.assert(typeof args === 'object' , 'type of args should be an object') + t.assert( args.hasOwnProperty('hello'), 'args has correct message data props') + t.assert( args.hello === 'world', 'args has correct message data') + }); + bc.on("server", function(address) { + t.equal(address,bs.address(), "server check client was rpc sender"); + bc.rpc(bs.address(), 'call_without_args') + bc.rpc(bs.address(), 'call_with_args', { hello: 'world'}) + }); + // connect the two clients together + bs.torrent.on("infoHash", function() { + bs.torrent.addPeer("127.0.0.1:" + bc.wt.address().port); + }); +}); + test("3 party incomplete graph gossip test", function(t) { t.plan(10);