1
1
import * as t from "io-ts" ;
2
- import { createRpc , parse } from "./index" ;
3
- import { isLeft , isRight } from "fp-ts/lib/Either" ;
2
+ import { createServer , parse , createClient } from "./index" ;
3
+ import { isLeft , isRight , either } from "fp-ts/lib/Either" ;
4
4
5
5
describe ( "json rpc" , ( ) => {
6
- const rpc = createRpc (
7
- {
8
- hello : {
9
- request : t . type ( { } ) ,
10
- response : t . string
11
- } ,
12
- echo : {
13
- request : t . type ( { arg : t . string } ) ,
14
- response : t . string
15
- }
6
+ const methods = {
7
+ hello : {
8
+ request : t . type ( { } ) ,
9
+ response : t . string
16
10
} ,
17
- {
18
- hello : _ => "Hello World!" ,
19
- echo : ( { arg } ) => arg
11
+ echo : {
12
+ request : t . type ( { arg : t . string } ) ,
13
+ response : t . string
20
14
}
21
- ) ;
15
+ } ;
16
+
17
+ const server = createServer ( methods , {
18
+ hello : _ => "Hello World!" ,
19
+ echo : ( { arg } ) => arg
20
+ } ) ;
21
+
22
+ const client = createClient ( methods , x => server ( x , undefined ) ) ;
22
23
23
24
describe ( "parse" , ( ) => {
24
25
it ( "should parse json" , ( ) => {
@@ -42,10 +43,10 @@ describe("json rpc", () => {
42
43
} ) ;
43
44
} ) ;
44
45
45
- describe ( "rpc " , ( ) => {
46
+ describe ( "server " , ( ) => {
46
47
describe ( "object" , ( ) => {
47
48
it ( "should respond" , async ( ) => {
48
- const res = await rpc ( {
49
+ const res = await server ( {
49
50
jsonrpc : "2.0" ,
50
51
id : "test" ,
51
52
method : "hello"
@@ -59,7 +60,7 @@ describe("json rpc", () => {
59
60
} ) ;
60
61
61
62
it ( "should not respond to notification" , async ( ) => {
62
- const res = await rpc ( {
63
+ const res = await server ( {
63
64
jsonrpc : "2.0" ,
64
65
method : "hello"
65
66
} ) ;
@@ -68,7 +69,7 @@ describe("json rpc", () => {
68
69
} ) ;
69
70
70
71
it ( "should accept an array of parameters" , async ( ) => {
71
- const res = await rpc ( {
72
+ const res = await server ( {
72
73
jsonrpc : "2.0" ,
73
74
id : "test" ,
74
75
method : "echo" ,
@@ -83,7 +84,7 @@ describe("json rpc", () => {
83
84
} ) ;
84
85
85
86
it ( "should accept an object of parameters" , async ( ) => {
86
- const res = await rpc ( {
87
+ const res = await server ( {
87
88
jsonrpc : "2.0" ,
88
89
id : "test" ,
89
90
method : "echo" ,
@@ -100,7 +101,7 @@ describe("json rpc", () => {
100
101
101
102
describe ( "array" , ( ) => {
102
103
it ( "should respond" , async ( ) => {
103
- const res = await rpc ( [
104
+ const res = await server ( [
104
105
{
105
106
jsonrpc : "2.0" ,
106
107
id : 1 ,
@@ -130,7 +131,7 @@ describe("json rpc", () => {
130
131
131
132
describe ( "invalid" , ( ) => {
132
133
it ( "should fail on malformed request" , async ( ) => {
133
- const res = await rpc ( 123 ) ;
134
+ const res = await server ( 123 ) ;
134
135
135
136
expect ( res ) . toEqual ( {
136
137
jsonrpc : "2.0" ,
@@ -143,7 +144,7 @@ describe("json rpc", () => {
143
144
} ) ;
144
145
145
146
it ( "should fail on malformed request object" , async ( ) => {
146
- const res = await rpc ( { } ) ;
147
+ const res = await server ( { } ) ;
147
148
148
149
expect ( res ) . toEqual ( {
149
150
jsonrpc : "2.0" ,
@@ -156,7 +157,7 @@ describe("json rpc", () => {
156
157
} ) ;
157
158
158
159
it ( "should fail on empty batch request" , async ( ) => {
159
- const res = await rpc ( [ ] ) ;
160
+ const res = await server ( [ ] ) ;
160
161
161
162
expect ( res ) . toEqual ( {
162
163
jsonrpc : "2.0" ,
@@ -169,7 +170,7 @@ describe("json rpc", () => {
169
170
} ) ;
170
171
171
172
it ( "should fail on invalid request rpc id" , async ( ) => {
172
- const res = await rpc ( {
173
+ const res = await server ( {
173
174
jsonrpc : "2.0" ,
174
175
id : { }
175
176
} ) ;
@@ -185,7 +186,7 @@ describe("json rpc", () => {
185
186
} ) ;
186
187
187
188
it ( "should fail on fractional numeric request rpc id" , async ( ) => {
188
- const res = await rpc ( {
189
+ const res = await server ( {
189
190
jsonrpc : "2.0" ,
190
191
id : 123.5
191
192
} ) ;
@@ -201,7 +202,7 @@ describe("json rpc", () => {
201
202
} ) ;
202
203
203
204
it ( "should fail on method not found" , async ( ) => {
204
- const res = await rpc ( {
205
+ const res = await server ( {
205
206
jsonrpc : "2.0" ,
206
207
id : "test" ,
207
208
method : "missing"
@@ -218,7 +219,7 @@ describe("json rpc", () => {
218
219
} ) ;
219
220
220
221
it ( "should fail on missing parameters" , async ( ) => {
221
- const res = await rpc ( {
222
+ const res = await server ( {
222
223
jsonrpc : "2.0" ,
223
224
id : "test" ,
224
225
method : "echo"
@@ -236,7 +237,7 @@ describe("json rpc", () => {
236
237
} ) ;
237
238
238
239
it ( "should fail on invalid parameters" , async ( ) => {
239
- const res = await rpc ( {
240
+ const res = await server ( {
240
241
jsonrpc : "2.0" ,
241
242
id : "test" ,
242
243
method : "echo" ,
@@ -254,7 +255,7 @@ describe("json rpc", () => {
254
255
} ) ;
255
256
256
257
it ( "should fail on invalid parameters" , async ( ) => {
257
- const res = await rpc ( {
258
+ const res = await server ( {
258
259
jsonrpc : "2.0" ,
259
260
id : "test" ,
260
261
method : "echo" ,
@@ -272,4 +273,54 @@ describe("json rpc", () => {
272
273
} ) ;
273
274
} ) ;
274
275
} ) ;
276
+
277
+ describe ( "client" , ( ) => {
278
+ describe ( "request" , ( ) => {
279
+ it ( "should make a request" , async ( ) => {
280
+ const result = await client ( { method : "hello" , params : { } } ) ;
281
+
282
+ expect ( result ) . toEqual ( "Hello World!" ) ;
283
+ } ) ;
284
+
285
+ it ( "should make a notification request" , async ( ) => {
286
+ const result = await client ( {
287
+ method : "hello" ,
288
+ params : { } ,
289
+ async : true
290
+ } ) ;
291
+
292
+ expect ( result ) . toEqual ( undefined ) ;
293
+ } ) ;
294
+ } ) ;
295
+
296
+ describe ( "batch" , ( ) => {
297
+ it ( "should make a batch request" , async ( ) => {
298
+ const result = await client . batch (
299
+ { method : "hello" , params : { } } ,
300
+ { method : "echo" , params : { arg : "test" } }
301
+ ) ;
302
+
303
+ expect ( result ) . toEqual ( [ "Hello World!" , "test" ] ) ;
304
+ } ) ;
305
+
306
+ it ( "should make a batch notification request" , async ( ) => {
307
+ const result = await client . batch (
308
+ { method : "hello" , params : { } , async : true } ,
309
+ { method : "echo" , params : { arg : "test" } , async : true }
310
+ ) ;
311
+
312
+ expect ( result ) . toEqual ( [ undefined , undefined ] ) ;
313
+ } ) ;
314
+
315
+ it ( "should handle mixed batch responses" , async ( ) => {
316
+ const result = await client . batch (
317
+ { method : "hello" , params : { } , async : true } ,
318
+ { method : "echo" , params : { arg : "test" } } ,
319
+ { method : "hello" , params : { } , async : true }
320
+ ) ;
321
+
322
+ expect ( result ) . toEqual ( [ undefined , "test" , undefined ] ) ;
323
+ } ) ;
324
+ } ) ;
325
+ } ) ;
275
326
} ) ;
0 commit comments