All files RPCClient.spec.ts

100% Statements 64/64
100% Branches 0/0
100% Functions 34/34
100% Lines 57/57

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 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 1911x 1x   1x     1x 1x 1x       1x     1x           1x       1x       1x 1x       1x     1x           1x       1x   1x     1x       1x     1x                 1x     1x 1x   1x 1x       1x       1x       1x       1x   1x 1x   1x         1x 1x       1x   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 { RPCClient } from './RPCClient';
import { RPCMethodError } from './Defines';
 
describe('RPCClient', () => {
    describe('notify', () => {
        it('Should send request without id when called', () => {
            let downstreamcb: any;
    
            // Setup RPCClient with dummy transport
            let client = new RPCClient({
                SendUpstream: (data: string) => {
                    // Parse json to avoid string comparison
                    expect(JSON.parse(data)).to.deep.equal({
                        jsonrpc: "2.0",
                        method: "test",
                        params: [1, 2, 3]
                    });
                },
                SetDownstreamCb: (cb: TransportCb) => downstreamcb = cb
            });
    
            // Send notification
            client.notify("test", 1, 2, 3);
        });
    });
 
    describe('call', () => {
        it('Should return results when using call', async () => {
            let downstreamcb: any;
 
            // Setup RPCClient with dummy transport
            let client = new RPCClient({
                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 client.call("test", 1, 2);
 
            expect(res).to.equal(10);
        });
 
        it('Should throw error when error response is received', async () => {
            let downstreamcb: any;
 
            // Setup RPCClient with dummy transport
            let client = new RPCClient({
                SendUpstream: (data: string) => {
                    // Imitate server response
                    downstreamcb(JSON.stringify({
                        jsonrpc: "2.0",
                        id: JSON.parse(data).id,
                        error: {
                            code: 123,
                            message: "Testing"
                        }
                    }));
                },
                SetDownstreamCb: (cb: TransportCb) => downstreamcb = cb
            });
 
            try {
                await client.call("test");
            } catch (e) {
                expect(e.code).to.equal(123);
                expect(e.message).to.equal("Testing");
            }
        });
 
        it('Should throw timeout error when no response from server is received', async () => {
            let downstreamcb: any;
 
            // Setup RPCClient with dummy transport
            let client = new RPCClient({
                SendUpstream: /* istanbul ignore next */ (data: string) => {
                    // Do nothing
                },
                SetDownstreamCb: (cb: TransportCb) => downstreamcb = cb
            });
 
            // Reduce timeout to speed up tests
            client.requestTimeout = 10;
 
            try {
                await client.call("test");
            } catch (e) {
                expect(e.message).to.equal("Request timed out");
            }
        });
    });
 
    describe(`on('error')`, () => {
        it('Should emit error when non numeric response id is received', async () => {
            let downstreamcb: any;
 
            // Setup RPCClient with dummy transport
            let client = new RPCClient({
                SendUpstream: /* istanbul ignore next */ (data: string) => { },
                SetDownstreamCb: (cb: TransportCb) => downstreamcb = cb
            });
 
            // Listen for errors
            client.on('error', (e) => {
                expect(e.message).to.equal("Response id is not a number");
            })
 
            // Send a forged server message
            downstreamcb(JSON.stringify({
                jsonrpc: "2.0",
                id: "test",
                result: true
            }));
        });
 
        it('Should emit error when non existing response id is received', async () => {
            let downstreamcb: any;
 
            // Setup RPCClient with dummy transport
            let client = new RPCClient({
                SendUpstream: /* istanbul ignore next */ (data: string) => { },
                SetDownstreamCb: (cb: TransportCb) => downstreamcb = cb
            });
 
            // Listen for errors
            client.on('error', (e) => {
                expect(e.message).to.equal("Request with id 10 not found");
            })
 
            // Send a forged server message
            downstreamcb(JSON.stringify({
                jsonrpc: "2.0",
                id: 10,
                result: true
            }));
        });
 
        it('Should emit parse error when response is malformed', async () => {
            let downstreamcb: any;
 
            // Setup RPCClient with dummy transport
            let client = new RPCClient({
                SendUpstream: /* istanbul ignore next */ (data: string) => { },
                SetDownstreamCb: (cb: TransportCb) => downstreamcb = cb
            });
 
            // Listen for errors
            client.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
            }));
        });
 
        it('Should emit error when response type is not RPCResponse', async () => {
            let downstreamcb: any;
 
            // Setup RPCClient with dummy transport
            let client = new RPCClient({
                SendUpstream: /* istanbul ignore next */ (data: string) => { },
                SetDownstreamCb: (cb: TransportCb) => downstreamcb = cb
            });
 
            // Listen for errors
            client.on('error', (e) => {
                expect(e.message).to.equal("Received message of non RPCResponse type");
            })
 
            // Send a forged server message
            downstreamcb(JSON.stringify({
                jsonrpc: "2.0",
                method: "test"
            }));
        });
    });
});