OLD | NEW |
---|---|
(Empty) | |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | |
2 // for details. All rights reserved. Use of this source code is governed by a | |
3 // BSD-style license that can be found in the LICENSE file. | |
4 | |
5 library json_rpc_2.test.client.client_test; | |
6 | |
7 import 'dart:async'; | |
8 import 'dart:convert'; | |
9 | |
10 import 'package:unittest/unittest.dart'; | |
11 import 'package:json_rpc_2/error_code.dart' as error_code; | |
12 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; | |
13 | |
14 void main() { | |
15 var incoming; | |
16 var outgoing; | |
17 var peer; | |
18 setUp(() { | |
19 var incomingController = new StreamController(); | |
20 incoming = incomingController.sink; | |
21 var outgoingController = new StreamController(); | |
22 outgoing = outgoingController.stream; | |
23 peer = new json_rpc.Peer.withoutJson(incomingController.stream, outgoingCont roller); | |
Bob Nystrom
2014/11/07 17:41:51
Long line. More below.
nweiz
2014/11/10 21:45:30
Done.
| |
24 }); | |
25 | |
26 group("like a client,", () { | |
27 test("can send a message and receive a response", () { | |
28 expect(outgoing.first.then((request) { | |
29 expect(request, equals({ | |
30 "jsonrpc": "2.0", | |
31 "method": "foo", | |
32 "params": {"bar": "baz"}, | |
33 "id": 0 | |
34 })); | |
35 incoming.add({ | |
36 "jsonrpc": "2.0", | |
37 "result": "qux", | |
38 "id": 0 | |
39 }); | |
40 }), completes); | |
41 | |
42 peer.listen(); | |
43 expect(peer.sendRequest("foo", {"bar": "baz"}), completion(equals("qux"))) ; | |
44 }); | |
45 | |
46 test("can send a batch of messages and receive a batch of responses", () { | |
47 expect(outgoing.first.then((request) { | |
48 expect(request, equals([ | |
49 { | |
50 "jsonrpc": "2.0", | |
51 "method": "foo", | |
52 "params": {"bar": "baz"}, | |
53 "id": 0 | |
54 }, | |
55 { | |
56 "jsonrpc": "2.0", | |
57 "method": "a", | |
58 "params": {"b": "c"}, | |
59 "id": 1 | |
60 }, | |
61 { | |
62 "jsonrpc": "2.0", | |
63 "method": "w", | |
64 "params": {"x": "y"}, | |
65 "id": 2 | |
66 } | |
67 ])); | |
68 | |
69 incoming.add([ | |
70 { | |
71 "jsonrpc": "2.0", | |
72 "result": "qux", | |
73 "id": 0 | |
74 }, | |
75 { | |
76 "jsonrpc": "2.0", | |
77 "result": "d", | |
78 "id": 1 | |
79 }, | |
80 { | |
81 "jsonrpc": "2.0", | |
82 "result": "z", | |
83 "id": 2 | |
84 } | |
85 ]); | |
86 }), completes); | |
87 | |
88 peer.listen(); | |
89 | |
90 peer.withBatch(() { | |
91 expect(peer.sendRequest("foo", {"bar": "baz"}), completion(equals("qux") )); | |
92 expect(peer.sendRequest("a", {"b": "c"}), completion(equals("d"))); | |
93 expect(peer.sendRequest("w", {"x": "y"}), completion(equals("z"))); | |
94 }); | |
95 }); | |
96 }); | |
97 | |
98 group("like a server,", () { | |
99 test("can receive a call and return a response", () { | |
100 expect(outgoing.first, completion(equals({ | |
101 "jsonrpc": "2.0", | |
102 "result": "qux", | |
103 "id": 0 | |
104 }))); | |
105 | |
106 peer.registerMethod("foo", (_) => "qux"); | |
107 peer.listen(); | |
108 | |
109 incoming.add({ | |
110 "jsonrpc": "2.0", | |
111 "method": "foo", | |
112 "params": {"bar": "baz"}, | |
113 "id": 0 | |
114 }); | |
115 }); | |
116 | |
117 test("can receive a batch of calls and return a batch of responses", () { | |
118 expect(outgoing.first, completion(equals([ | |
119 { | |
120 "jsonrpc": "2.0", | |
121 "result": "qux", | |
122 "id": 0 | |
123 }, | |
124 { | |
125 "jsonrpc": "2.0", | |
126 "result": "d", | |
127 "id": 1 | |
128 }, | |
129 { | |
130 "jsonrpc": "2.0", | |
131 "result": "z", | |
132 "id": 2 | |
133 } | |
134 ]))); | |
135 | |
136 peer.registerMethod("foo", (_) => "qux"); | |
137 peer.registerMethod("a", (_) => "d"); | |
138 peer.registerMethod("w", (_) => "z"); | |
139 peer.listen(); | |
140 | |
141 incoming.add([ | |
142 { | |
143 "jsonrpc": "2.0", | |
144 "method": "foo", | |
145 "params": {"bar": "baz"}, | |
146 "id": 0 | |
147 }, | |
148 { | |
149 "jsonrpc": "2.0", | |
150 "method": "a", | |
151 "params": {"b": "c"}, | |
152 "id": 1 | |
153 }, | |
154 { | |
155 "jsonrpc": "2.0", | |
156 "method": "w", | |
157 "params": {"x": "y"}, | |
158 "id": 2 | |
159 } | |
160 ]); | |
161 }); | |
162 | |
163 test("returns a response for malformed JSON", () { | |
164 var incomingController = new StreamController(); | |
165 var outgoingController = new StreamController(); | |
166 var jsonPeer = new json_rpc.Peer( | |
167 incomingController.stream, outgoingController); | |
168 | |
169 expect(outgoingController.stream.first.then(JSON.decode), completion({ | |
170 "jsonrpc": "2.0", | |
171 "error": { | |
172 'code': error_code.PARSE_ERROR, | |
173 "message": startsWith("Invalid JSON: "), | |
174 "data": {'request': '{invalid'} | |
175 }, | |
176 "id": null | |
177 })); | |
178 | |
179 jsonPeer.listen(); | |
180 | |
181 incomingController.add("{invalid"); | |
182 }); | |
183 | |
184 test("returns a response for incorrectly-structured JSON", () { | |
185 expect(outgoing.first, completion({ | |
186 "jsonrpc": "2.0", | |
187 "error": { | |
188 'code': error_code.INVALID_REQUEST, | |
189 "message": 'Request must contain a "jsonrpc" key.', | |
190 "data": {'request': {'completely': 'wrong'}} | |
191 }, | |
192 "id": null | |
193 })); | |
194 | |
195 peer.listen(); | |
196 | |
197 incoming.add({ | |
198 "completely": "wrong" | |
199 }); | |
200 }); | |
201 }); | |
202 } | |
OLD | NEW |