All files RPCNode.spec.ts

100% Statements 25/25
100% Branches 0/0
100% Functions 13/13
100% Lines 23/23

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 761x 1x   1x     1x 1x       1x     1x           1x       1x   1x     1x       1x     1x           1x       1x 1x       1x     1x       1x   1x       1x 1x       1x            
import { describe, it } from 'mocha';
import { expect, assert } from 'chai';
import { TransportCb } from './Transport';
import { RPCNode } from './RPCNode';
import { RPCMethodError } from './Defines';
 
describe('RPCClient', () => {
    it('Should work as RPCClient when using call', async () => {
        let downstreamcb: any;
 
        // Setup RPCNode with dummy transport
        let node = new RPCNode({
            SendUpstream: (data: string) => {
                // Imitate server response
                downstreamcb(JSON.stringify({
                    jsonrpc: "2.0",
                    id: JSON.parse(data).id,
                    result: 10
                }));
            },
            SetDownstreamCb: (cb: TransportCb) => downstreamcb = cb
        });
 
        // call rpc method
        var res = await node.call("test", 1, 2);
 
        expect(res).to.equal(10);
    });
 
    it('Should work as RPCServer when request is received', () => {
        let downstreamcb: any;
 
        // Setup RPCNode with dummy transport
        let node = new RPCNode({
            SendUpstream: (data: string) => {
                // Parse json to avoid string comparison
                expect(JSON.parse(data)).to.deep.equal({
                    jsonrpc: "2.0",
                    id: 10,
                    result: 20
                });
            },
            SetDownstreamCb: (cb: TransportCb) => downstreamcb = cb
        });
 
        // bind method
        node.bind("test", (a: number, b: number) => {
            return a + b;
        });
 
        // call rpc method
        downstreamcb('{"jsonrpc":"2.0","id":10,"method":"test","params":[5,15]}');
    });
 
    it('Should emit parse error when response is malformed', async () => {
        let downstreamcb: any;
 
        // Setup RPCNode with dummy transport
        let node = new RPCNode({
            SendUpstream: /* istanbul ignore next */ (data: string) => { },
            SetDownstreamCb: (cb: TransportCb) => downstreamcb = cb
        });
 
        // Listen for errors
        node.on('error', (e) => {
            expect(e.message).to.equal("Message parse failed: Invalid Message: wrong json rpc version");
        })
 
        // Send a forged server message
        downstreamcb(JSON.stringify({
            jsonrpc: "2.999",
            id: 10,
            result: true
        }));
    });
});