From 1407332871883b8ca0cec013a5b6d8cb0d7c9881 Mon Sep 17 00:00:00 2001 From: zemse Date: Tue, 23 Jun 2020 00:24:51 +0530 Subject: [PATCH] Add request parser test cases (#17) --- test/json-rpc/index.ts | 2 + test/json-rpc/request-parser.test.ts | 59 ++++++++++++++++++++++++++++ 2 files changed, 61 insertions(+) create mode 100644 test/json-rpc/request-parser.test.ts diff --git a/test/json-rpc/index.ts b/test/json-rpc/index.ts index 10d66a5..6f9d789 100644 --- a/test/json-rpc/index.ts +++ b/test/json-rpc/index.ts @@ -1,3 +1,4 @@ +import { RequestParser } from './request-parser.test'; import { ComputeBunchProposal } from './compute-bunch-proposal.test'; import { SignBunchRPC } from './sign-bunch.test'; import { SerializeRequest } from './serialize-request.test'; @@ -8,6 +9,7 @@ import { BlockProposal } from './block-proposal.test'; export const JsonRpc = () => describe('JSON-RPC', () => { + RequestParser(); ComputeBunchProposal(); SignBunchRPC(); SerializeRequest(); diff --git a/test/json-rpc/request-parser.test.ts b/test/json-rpc/request-parser.test.ts new file mode 100644 index 0000000..93f4cc9 --- /dev/null +++ b/test/json-rpc/request-parser.test.ts @@ -0,0 +1,59 @@ +import { parseRequest } from '../../src/json-rpc/parser'; +import { Bytes, Signature } from '../../src/utils/bytes'; +import assert from 'assert'; +import { ethers } from 'ethers'; + +export const RequestParser = () => + describe('Request Parser', () => { + it('parses a valid request correctly', () => { + const request = { + jsonrpc: '2.0', + method: 'temp_method', + params: [1, '0x2345'], + id: ethers.utils.hexlify(ethers.utils.randomBytes(32)), + }; + + const parsed = parseRequest(request); + + assert.ok( + parsed.id instanceof Bytes, + 'hex string should be converted into a Bytes instance' + ); + }); + + it('throws for invalid json rpc version', () => { + try { + const request = { + jsonrpc: '3.0', + method: 'temp_method', + params: [1, '0x2345'], + id: null, + }; + + parseRequest(request); + + assert(false, 'should have thrown for invalid json rpc version'); + } catch { + assert(true); + } + }); + + it('parses nonce and signature correctly', () => { + const request = { + jsonrpc: '2.0', + method: 'temp_method', + params: [1, '0x2345'], + id: null, + nonce: 23, + signature: + '0xd5f3bcfbc09bef8e232bf7090091eb883cf911937175739107fcdca58ffb6d3b5c47ebc40d9820e7d2c104814a36f87bb3f03c09f8cd0361da296d9712a469921b', + }; + + const parsed = parseRequest(request); + + assert.ok( + parsed.signature instanceof Signature, + 'hex string should be converted into a Signature instance' + ); + }); + });