Skip to content

Commit

Permalink
Add PendingJsonRpcResponse type (#43)
Browse files Browse the repository at this point in the history
* Add PendingJsonRpcResponse type

* Add generic result parameter
  • Loading branch information
Mrtenz committed Oct 6, 2022
1 parent ad5fc45 commit c971d54
Show file tree
Hide file tree
Showing 3 changed files with 432 additions and 13 deletions.
319 changes: 319 additions & 0 deletions src/__fixtures__/json.ts
Expand Up @@ -671,6 +671,325 @@ export const JSON_RPC_RESPONSE_FIXTURES = {
],
};

export const JSON_RPC_PENDING_RESPONSE_FIXTURES = {
valid: [
...JSON_RPC_SUCCESS_FIXTURES.valid,
...JSON_RPC_FAILURE_FIXTURES.valid,
{
id: 1,
jsonrpc: '2.0',
},
{
id: 1,
jsonrpc: '2.0',
error: undefined,
},
{
id: 1,
jsonrpc: '2.0',
result: undefined,
},
{
id: 1,
jsonrpc: '2.0',
result: undefined,
error: undefined,
},
{
id: 1,
jsonrpc: '2.0',
result: {
foo: 'bar',
},
error: {
code: -32000,
message: 'Internal error',
},
},
],
invalid: [
{},
[],
true,
false,
null,
undefined,
1,
'foo',
{
jsonrpc: '2.0',
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: {},
jsonrpc: '2.0',
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: [],
jsonrpc: '2.0',
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: true,
jsonrpc: '2.0',
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: false,
jsonrpc: '2.0',
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: undefined,
jsonrpc: '2.0',
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '1.0',
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: 2.0,
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: {},
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: [],
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: true,
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: false,
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: null,
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: undefined,
error: {
code: -32000,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: -32000,
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: [],
},
{
id: 1,
jsonrpc: '2.0',
error: {},
},
{
id: 1,
jsonrpc: '2.0',
error: true,
},
{
id: 1,
jsonrpc: '2.0',
error: false,
},
{
id: 1,
jsonrpc: '2.0',
error: null,
},
{
id: 1,
jsonrpc: '2.0',
error: 'foo',
},
{
id: 1,
jsonrpc: '2.0',
error: 1,
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: {},
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: [],
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: true,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: false,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: null,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: undefined,
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: 'foo',
message: 'Internal error',
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: -32000,
message: {},
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: -32000,
message: [],
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: -32000,
message: true,
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: -32000,
message: false,
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: -32000,
message: null,
},
},
{
id: 1,
jsonrpc: '2.0',
error: {
code: -32000,
message: undefined,
},
},
],
};

export const COMPLEX_OBJECT = {
data: {
account: {
Expand Down
47 changes: 47 additions & 0 deletions src/json.test.ts
Expand Up @@ -6,6 +6,7 @@ import {
JSON_FIXTURES,
JSON_RPC_FAILURE_FIXTURES,
JSON_RPC_NOTIFICATION_FIXTURES,
JSON_RPC_PENDING_RESPONSE_FIXTURES,
JSON_RPC_REQUEST_FIXTURES,
JSON_RPC_RESPONSE_FIXTURES,
JSON_RPC_SUCCESS_FIXTURES,
Expand All @@ -18,12 +19,14 @@ import {
assertIsJsonRpcRequest,
assertIsJsonRpcResponse,
assertIsJsonRpcSuccess,
assertIsPendingJsonRpcResponse,
getJsonRpcIdValidator,
isJsonRpcFailure,
isJsonRpcNotification,
isJsonRpcRequest,
isJsonRpcResponse,
isJsonRpcSuccess,
isPendingJsonRpcResponse,
isValidJson,
validateJsonAndGetSize,
} from '.';
Expand Down Expand Up @@ -254,6 +257,50 @@ describe('json', () => {
});
});

describe('isPendingJsonRpcResponse', () => {
it.each(JSON_RPC_PENDING_RESPONSE_FIXTURES.valid)(
'returns true for a valid pending JSON-RPC response',
(response) => {
expect(isPendingJsonRpcResponse(response)).toBe(true);
},
);

it.each(JSON_RPC_PENDING_RESPONSE_FIXTURES.invalid)(
'returns false for an invalid pending JSON-RPC response',
(response) => {
expect(isPendingJsonRpcResponse(response)).toBe(false);
},
);
});

describe('assertIsPendingJsonRpcResponse', () => {
it.each(JSON_RPC_PENDING_RESPONSE_FIXTURES.valid)(
'does not throw for a valid pending JSON-RPC response',
(response) => {
expect(() => assertIsPendingJsonRpcResponse(response)).not.toThrow();
},
);

it.each(JSON_RPC_PENDING_RESPONSE_FIXTURES.invalid)(
'throws for an invalid pending JSON-RPC response',
(response) => {
expect(() => assertIsPendingJsonRpcResponse(response)).toThrow(
'Not a pending JSON-RPC response',
);
},
);

it('includes the value thrown in the message if it is not an error', () => {
jest.spyOn(superstructModule, 'assert').mockImplementation(() => {
throw 'oops';
});

expect(() =>
assertIsPendingJsonRpcResponse(JSON_RPC_FAILURE_FIXTURES.invalid[0]),
).toThrow('Not a pending JSON-RPC response: oops');
});
});

describe('isJsonRpcResponse', () => {
it.each(JSON_RPC_RESPONSE_FIXTURES.valid)(
'returns true for a valid JSON-RPC response',
Expand Down

0 comments on commit c971d54

Please sign in to comment.