11import {
2- jsonrpc2 ,
3- isJsonRpcSuccess ,
4- JsonRpcError ,
2+ assertIsJsonRpcFailure ,
3+ assertIsJsonRpcNotification ,
4+ assertIsJsonRpcRequest ,
5+ assertIsJsonRpcSuccess ,
6+ getJsonRpcIdValidator ,
57 isJsonRpcFailure ,
8+ isJsonRpcNotification ,
9+ isJsonRpcRequest ,
10+ isJsonRpcSuccess ,
611 isValidJson ,
12+ jsonrpc2 ,
13+ JsonRpcError ,
714} from '.' ;
815
916const getError = ( ) => {
@@ -40,6 +47,88 @@ describe('json', () => {
4047 } ) ;
4148 } ) ;
4249
50+ describe ( 'isJsonRpcNotification' , ( ) => {
51+ it ( 'identifies a JSON-RPC notification' , ( ) => {
52+ expect (
53+ isJsonRpcNotification ( {
54+ jsonrpc : jsonrpc2 ,
55+ method : 'foo' ,
56+ } ) ,
57+ ) . toBe ( true ) ;
58+ } ) ;
59+
60+ it ( 'identifies a JSON-RPC request' , ( ) => {
61+ expect (
62+ isJsonRpcNotification ( {
63+ jsonrpc : jsonrpc2 ,
64+ id : 1 ,
65+ method : 'foo' ,
66+ } ) ,
67+ ) . toBe ( false ) ;
68+ } ) ;
69+ } ) ;
70+
71+ describe ( 'assertIsJsonRpcNotification' , ( ) => {
72+ it ( 'identifies JSON-RPC notification objects' , ( ) => {
73+ [
74+ { jsonrpc : jsonrpc2 , method : 'foo' } ,
75+ { jsonrpc : jsonrpc2 , method : 'bar' , params : [ 'baz' ] } ,
76+ ] . forEach ( ( input ) => {
77+ expect ( ( ) => assertIsJsonRpcNotification ( input ) ) . not . toThrow ( ) ;
78+ } ) ;
79+
80+ [
81+ { id : 1 , jsonrpc : jsonrpc2 , method : 'foo' } ,
82+ { id : 1 , jsonrpc : jsonrpc2 , method : 'bar' , params : [ 'baz' ] } ,
83+ ] . forEach ( ( input ) => {
84+ expect ( ( ) => assertIsJsonRpcNotification ( input ) ) . toThrow (
85+ 'Not a JSON-RPC notification.' ,
86+ ) ;
87+ } ) ;
88+ } ) ;
89+ } ) ;
90+
91+ describe ( 'isJsonRpcRequest' , ( ) => {
92+ it ( 'identifies a JSON-RPC notification' , ( ) => {
93+ expect (
94+ isJsonRpcRequest ( {
95+ id : 1 ,
96+ jsonrpc : jsonrpc2 ,
97+ method : 'foo' ,
98+ } ) ,
99+ ) . toBe ( true ) ;
100+ } ) ;
101+
102+ it ( 'identifies a JSON-RPC request' , ( ) => {
103+ expect (
104+ isJsonRpcRequest ( {
105+ jsonrpc : jsonrpc2 ,
106+ method : 'foo' ,
107+ } ) ,
108+ ) . toBe ( false ) ;
109+ } ) ;
110+ } ) ;
111+
112+ describe ( 'assertIsJsonRpcRequest' , ( ) => {
113+ it ( 'identifies JSON-RPC notification objects' , ( ) => {
114+ [
115+ { id : 1 , jsonrpc : jsonrpc2 , method : 'foo' } ,
116+ { id : 1 , jsonrpc : jsonrpc2 , method : 'bar' , params : [ 'baz' ] } ,
117+ ] . forEach ( ( input ) => {
118+ expect ( ( ) => assertIsJsonRpcRequest ( input ) ) . not . toThrow ( ) ;
119+ } ) ;
120+
121+ [
122+ { jsonrpc : jsonrpc2 , method : 'foo' } ,
123+ { jsonrpc : jsonrpc2 , method : 'bar' , params : [ 'baz' ] } ,
124+ ] . forEach ( ( input ) => {
125+ expect ( ( ) => assertIsJsonRpcRequest ( input ) ) . toThrow (
126+ 'Not a JSON-RPC request.' ,
127+ ) ;
128+ } ) ;
129+ } ) ;
130+ } ) ;
131+
43132 describe ( 'isJsonRpcSuccess' , ( ) => {
44133 it ( 'identifies a successful JSON-RPC response' , ( ) => {
45134 expect (
@@ -62,6 +151,26 @@ describe('json', () => {
62151 } ) ;
63152 } ) ;
64153
154+ describe ( 'assertIsJsonRpcSuccess' , ( ) => {
155+ it ( 'identifies JSON-RPC response objects' , ( ) => {
156+ [
157+ { id : 1 , jsonrpc : jsonrpc2 , result : 'success' } ,
158+ { id : 1 , jsonrpc : jsonrpc2 , result : null } ,
159+ ] . forEach ( ( input ) => {
160+ expect ( ( ) => assertIsJsonRpcSuccess ( input ) ) . not . toThrow ( ) ;
161+ } ) ;
162+
163+ [
164+ { id : 1 , jsonrpc : jsonrpc2 , error : getError ( ) } ,
165+ { id : 1 , jsonrpc : jsonrpc2 , error : null as any } ,
166+ ] . forEach ( ( input ) => {
167+ expect ( ( ) => assertIsJsonRpcSuccess ( input ) ) . toThrow (
168+ 'Not a successful JSON-RPC response.' ,
169+ ) ;
170+ } ) ;
171+ } ) ;
172+ } ) ;
173+
65174 describe ( 'isJsonRpcFailure' , ( ) => {
66175 it ( 'identifies a failed JSON-RPC response' , ( ) => {
67176 expect (
@@ -83,4 +192,98 @@ describe('json', () => {
83192 ) . toBe ( false ) ;
84193 } ) ;
85194 } ) ;
195+
196+ describe ( 'assertIsJsonRpcFailure' , ( ) => {
197+ it ( 'identifies JSON-RPC response objects' , ( ) => {
198+ ( [ { error : 'failure' } , { error : null } ] as any [ ] ) . forEach ( ( input ) => {
199+ expect ( ( ) => assertIsJsonRpcFailure ( input ) ) . not . toThrow ( ) ;
200+ } ) ;
201+
202+ ( [ { result : 'success' } , { } ] as any [ ] ) . forEach ( ( input ) => {
203+ expect ( ( ) => assertIsJsonRpcFailure ( input ) ) . toThrow (
204+ 'Not a failed JSON-RPC response.' ,
205+ ) ;
206+ } ) ;
207+ } ) ;
208+ } ) ;
209+
210+ describe ( 'getJsonRpcIdValidator' , ( ) => {
211+ const getInputs = ( ) => {
212+ return {
213+ // invariant with respect to options
214+ fractionString : { value : '1.2' , expected : true } ,
215+ negativeInteger : { value : - 1 , expected : true } ,
216+ object : { value : { } , expected : false } ,
217+ positiveInteger : { value : 1 , expected : true } ,
218+ string : { value : 'foo' , expected : true } ,
219+ undefined : { value : undefined , expected : false } ,
220+ zero : { value : 0 , expected : true } ,
221+ // variant with respect to options
222+ emptyString : { value : '' , expected : true } ,
223+ fraction : { value : 1.2 , expected : false } ,
224+ null : { value : null , expected : true } ,
225+ } ;
226+ } ;
227+
228+ const validateAll = (
229+ validate : ReturnType < typeof getJsonRpcIdValidator > ,
230+ inputs : ReturnType < typeof getInputs > ,
231+ ) => {
232+ for ( const input of Object . values ( inputs ) ) {
233+ expect ( validate ( input . value ) ) . toStrictEqual ( input . expected ) ;
234+ }
235+ } ;
236+
237+ it ( 'performs as expected with default options' , ( ) => {
238+ const inputs = getInputs ( ) ;
239+
240+ // The default options are:
241+ // permitEmptyString: true,
242+ // permitFractions: false,
243+ // permitNull: true,
244+ expect ( ( ) => validateAll ( getJsonRpcIdValidator ( ) , inputs ) ) . not . toThrow ( ) ;
245+ } ) ;
246+
247+ it ( 'performs as expected with "permitEmptyString: false"' , ( ) => {
248+ const inputs = getInputs ( ) ;
249+ inputs . emptyString . expected = false ;
250+
251+ expect ( ( ) =>
252+ validateAll (
253+ getJsonRpcIdValidator ( {
254+ permitEmptyString : false ,
255+ } ) ,
256+ inputs ,
257+ ) ,
258+ ) . not . toThrow ( ) ;
259+ } ) ;
260+
261+ it ( 'performs as expected with "permitFractions: true"' , ( ) => {
262+ const inputs = getInputs ( ) ;
263+ inputs . fraction . expected = true ;
264+
265+ expect ( ( ) =>
266+ validateAll (
267+ getJsonRpcIdValidator ( {
268+ permitFractions : true ,
269+ } ) ,
270+ inputs ,
271+ ) ,
272+ ) . not . toThrow ( ) ;
273+ } ) ;
274+
275+ it ( 'performs as expected with "permitNull: false"' , ( ) => {
276+ const inputs = getInputs ( ) ;
277+ inputs . null . expected = false ;
278+
279+ expect ( ( ) =>
280+ validateAll (
281+ getJsonRpcIdValidator ( {
282+ permitNull : false ,
283+ } ) ,
284+ inputs ,
285+ ) ,
286+ ) . not . toThrow ( ) ;
287+ } ) ;
288+ } ) ;
86289} ) ;
0 commit comments