Skip to content

Commit

Permalink
Add request parser test cases (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
zemse committed Jun 22, 2020
1 parent d678646 commit 1407332
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 0 deletions.
2 changes: 2 additions & 0 deletions test/json-rpc/index.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand All @@ -8,6 +9,7 @@ import { BlockProposal } from './block-proposal.test';

export const JsonRpc = () =>
describe('JSON-RPC', () => {
RequestParser();
ComputeBunchProposal();
SignBunchRPC();
SerializeRequest();
Expand Down
59 changes: 59 additions & 0 deletions test/json-rpc/request-parser.test.ts
Original file line number Diff line number Diff line change
@@ -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'
);
});
});

0 comments on commit 1407332

Please sign in to comment.