Skip to content
This repository was archived by the owner on Nov 9, 2023. It is now read-only.

Commit 4e707d8

Browse files
committed
Add tests
1 parent 2e8ed44 commit 4e707d8

File tree

2 files changed

+111
-3
lines changed

2 files changed

+111
-3
lines changed

src/utils.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ interface JsonRpcValidatorOptions {
3939

4040
/**
4141
* Gets a function for validating JSON-RPC request / response `id` values.
42-
*
42+
*
4343
* By manipulating the options of this factory, you can control the behavior
4444
* of the resulting validator for some edge cases. This is useful because e.g.
4545
* `null` should sometimes but not always be permitted.
46-
*
46+
*
4747
* Note that the empty string (`''`) is always permitted by the JSON-RPC
4848
* specification, but that kind of sucks and you may want to forbid it in some
4949
* instances anyway.
50-
*
50+
*
5151
* For more details, see the
5252
* [JSON-RPC Specification](https://www.jsonrpc.org/specification).
5353
*

test/utils.test.js

Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
/* eslint-env mocha */
2+
'use strict';
3+
4+
const { strict: assert } = require('assert');
5+
const {
6+
isJsonRpcFailure,
7+
isJsonRpcSuccess,
8+
getJsonRpcIdValidator,
9+
} = require('../dist');
10+
11+
describe('isJsonRpcSuccess', function () {
12+
it('correctly identifies JSON-RPC response objects', function () {
13+
assert.equal(isJsonRpcSuccess({ result: 'success' }), true);
14+
assert.equal(isJsonRpcSuccess({ result: undefined }), true);
15+
assert.equal(isJsonRpcSuccess({ error: new Error('foo') }), false);
16+
assert.equal(isJsonRpcSuccess({}), false);
17+
});
18+
});
19+
20+
describe('isJsonRpcFailure', function () {
21+
it('correctly identifies JSON-RPC response objects', function () {
22+
assert.equal(isJsonRpcFailure({ error: 'failure' }), true);
23+
assert.equal(isJsonRpcFailure({ error: undefined }), true);
24+
assert.equal(isJsonRpcFailure({ result: 'success' }), false);
25+
assert.equal(isJsonRpcFailure({}), false);
26+
});
27+
});
28+
29+
describe('getJsonRpcIdValidator', function () {
30+
const getInputs = () => {
31+
return {
32+
// invariant with respect to options
33+
fractionString: { value: '1.2', expected: true },
34+
negativeInteger: { value: -1, expected: true },
35+
object: { value: {}, expected: false },
36+
positiveInteger: { value: 1, expected: true },
37+
string: { value: 'foo', expected: true },
38+
undefined: { value: undefined, expected: false },
39+
zero: { value: 0, expected: true },
40+
// variant with respect to options
41+
emptyString: { value: '', expected: true },
42+
fraction: { value: 1.2, expected: false },
43+
null: { value: null, expected: true },
44+
};
45+
};
46+
47+
const validateAll = (validate, inputs) => {
48+
for (const input of Object.values(inputs)) {
49+
assert.equal(
50+
validate(input.value),
51+
input.expected,
52+
`should output "${input.expected}" for "${input.value}"`,
53+
);
54+
}
55+
};
56+
57+
it('performs as expected with default options', function () {
58+
const inputs = getInputs();
59+
60+
// The default options are:
61+
// permitEmptyString: true,
62+
// permitFractions: false,
63+
// permitNull: true,
64+
validateAll(getJsonRpcIdValidator(), inputs);
65+
});
66+
67+
it('performs as expected with "permitEmptyString: false"', function () {
68+
const inputs = getInputs();
69+
inputs.emptyString.expected = false;
70+
71+
validateAll(
72+
getJsonRpcIdValidator({
73+
permitEmptyString: false,
74+
permitFractions: false,
75+
permitNull: true,
76+
}),
77+
inputs,
78+
);
79+
});
80+
81+
it('performs as expected with "permitFractions: true"', function () {
82+
const inputs = getInputs();
83+
inputs.fraction.expected = true;
84+
85+
validateAll(
86+
getJsonRpcIdValidator({
87+
permitEmptyString: true,
88+
permitFractions: true,
89+
permitNull: true,
90+
}),
91+
inputs,
92+
);
93+
});
94+
95+
it('performs as expected with "permitNull: false"', function () {
96+
const inputs = getInputs();
97+
inputs.null.expected = false;
98+
99+
validateAll(
100+
getJsonRpcIdValidator({
101+
permitEmptyString: true,
102+
permitFractions: false,
103+
permitNull: false,
104+
}),
105+
inputs,
106+
);
107+
});
108+
});

0 commit comments

Comments
 (0)