Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(667)

Side by Side Diff: pkg/json_rpc_2/test/peer_test.dart

Issue 707063002: Add a Peer class to json_rpc_2. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Code review changes Created 6 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/json_rpc_2/pubspec.yaml ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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(
24 incomingController.stream, outgoingController);
25 });
26
27 group("like a client,", () {
28 test("can send a message and receive a response", () {
29 expect(outgoing.first.then((request) {
30 expect(request, equals({
31 "jsonrpc": "2.0",
32 "method": "foo",
33 "params": {"bar": "baz"},
34 "id": 0
35 }));
36 incoming.add({
37 "jsonrpc": "2.0",
38 "result": "qux",
39 "id": 0
40 });
41 }), completes);
42
43 peer.listen();
44 expect(peer.sendRequest("foo", {"bar": "baz"}),
45 completion(equals("qux")));
46 });
47
48 test("can send a batch of messages and receive a batch of responses", () {
49 expect(outgoing.first.then((request) {
50 expect(request, equals([
51 {
52 "jsonrpc": "2.0",
53 "method": "foo",
54 "params": {"bar": "baz"},
55 "id": 0
56 },
57 {
58 "jsonrpc": "2.0",
59 "method": "a",
60 "params": {"b": "c"},
61 "id": 1
62 },
63 {
64 "jsonrpc": "2.0",
65 "method": "w",
66 "params": {"x": "y"},
67 "id": 2
68 }
69 ]));
70
71 incoming.add([
72 {
73 "jsonrpc": "2.0",
74 "result": "qux",
75 "id": 0
76 },
77 {
78 "jsonrpc": "2.0",
79 "result": "d",
80 "id": 1
81 },
82 {
83 "jsonrpc": "2.0",
84 "result": "z",
85 "id": 2
86 }
87 ]);
88 }), completes);
89
90 peer.listen();
91
92 peer.withBatch(() {
93 expect(peer.sendRequest("foo", {"bar": "baz"}),
94 completion(equals("qux")));
95 expect(peer.sendRequest("a", {"b": "c"}), completion(equals("d")));
96 expect(peer.sendRequest("w", {"x": "y"}), completion(equals("z")));
97 });
98 });
99 });
100
101 group("like a server,", () {
102 test("can receive a call and return a response", () {
103 expect(outgoing.first, completion(equals({
104 "jsonrpc": "2.0",
105 "result": "qux",
106 "id": 0
107 })));
108
109 peer.registerMethod("foo", (_) => "qux");
110 peer.listen();
111
112 incoming.add({
113 "jsonrpc": "2.0",
114 "method": "foo",
115 "params": {"bar": "baz"},
116 "id": 0
117 });
118 });
119
120 test("can receive a batch of calls and return a batch of responses", () {
121 expect(outgoing.first, completion(equals([
122 {
123 "jsonrpc": "2.0",
124 "result": "qux",
125 "id": 0
126 },
127 {
128 "jsonrpc": "2.0",
129 "result": "d",
130 "id": 1
131 },
132 {
133 "jsonrpc": "2.0",
134 "result": "z",
135 "id": 2
136 }
137 ])));
138
139 peer.registerMethod("foo", (_) => "qux");
140 peer.registerMethod("a", (_) => "d");
141 peer.registerMethod("w", (_) => "z");
142 peer.listen();
143
144 incoming.add([
145 {
146 "jsonrpc": "2.0",
147 "method": "foo",
148 "params": {"bar": "baz"},
149 "id": 0
150 },
151 {
152 "jsonrpc": "2.0",
153 "method": "a",
154 "params": {"b": "c"},
155 "id": 1
156 },
157 {
158 "jsonrpc": "2.0",
159 "method": "w",
160 "params": {"x": "y"},
161 "id": 2
162 }
163 ]);
164 });
165
166 test("returns a response for malformed JSON", () {
167 var incomingController = new StreamController();
168 var outgoingController = new StreamController();
169 var jsonPeer = new json_rpc.Peer(
170 incomingController.stream, outgoingController);
171
172 expect(outgoingController.stream.first.then(JSON.decode), completion({
173 "jsonrpc": "2.0",
174 "error": {
175 'code': error_code.PARSE_ERROR,
176 "message": startsWith("Invalid JSON: "),
177 "data": {'request': '{invalid'}
178 },
179 "id": null
180 }));
181
182 jsonPeer.listen();
183
184 incomingController.add("{invalid");
185 });
186
187 test("returns a response for incorrectly-structured JSON", () {
188 expect(outgoing.first, completion({
189 "jsonrpc": "2.0",
190 "error": {
191 'code': error_code.INVALID_REQUEST,
192 "message": 'Request must contain a "jsonrpc" key.',
193 "data": {'request': {'completely': 'wrong'}}
194 },
195 "id": null
196 }));
197
198 peer.listen();
199
200 incoming.add({
201 "completely": "wrong"
202 });
203 });
204 });
205 }
OLDNEW
« no previous file with comments | « pkg/json_rpc_2/pubspec.yaml ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698