OLD | NEW |
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 import 'dart:async'; | 5 import 'dart:async'; |
6 | 6 |
| 7 import 'package:stream_channel/stream_channel.dart'; |
7 import 'package:test/test.dart'; | 8 import 'package:test/test.dart'; |
| 9 |
8 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; | 10 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; |
9 | 11 |
10 import 'utils.dart'; | 12 import 'utils.dart'; |
11 | 13 |
12 void main() { | 14 void main() { |
| 15 var requestController; |
| 16 var responseController; |
| 17 var server; |
| 18 setUp(() { |
| 19 requestController = new StreamController(); |
| 20 responseController = new StreamController(); |
| 21 server = new json_rpc.Server.withoutJson( |
| 22 new StreamChannel(requestController.stream, responseController.sink)); |
| 23 }); |
| 24 |
13 test(".withoutJson supports decoded stream and sink", () { | 25 test(".withoutJson supports decoded stream and sink", () { |
14 var requestController = new StreamController(); | |
15 var responseController = new StreamController(); | |
16 var server = new json_rpc.Server.withoutJson( | |
17 requestController.stream, responseController.sink); | |
18 server.listen(); | 26 server.listen(); |
19 | 27 |
20 server.registerMethod('foo', (params) { | 28 server.registerMethod('foo', (params) { |
21 return {'params': params.value}; | 29 return {'params': params.value}; |
22 }); | 30 }); |
23 | 31 |
24 requestController.add({ | 32 requestController.add({ |
25 'jsonrpc': '2.0', | 33 'jsonrpc': '2.0', |
26 'method': 'foo', | 34 'method': 'foo', |
27 'params': {'param': 'value'}, | 35 'params': {'param': 'value'}, |
28 'id': 1234 | 36 'id': 1234 |
29 }); | 37 }); |
30 | 38 |
31 expect(responseController.stream.first, completion(equals({ | 39 expect(responseController.stream.first, completion(equals({ |
32 'jsonrpc': '2.0', | 40 'jsonrpc': '2.0', |
33 'result': {'params': {'param': 'value'}}, | 41 'result': {'params': {'param': 'value'}}, |
34 'id': 1234 | 42 'id': 1234 |
35 }))); | 43 }))); |
36 }); | 44 }); |
37 | 45 |
38 test(".listen returns when the controller is closed", () { | 46 test(".listen returns when the controller is closed", () { |
39 var requestController = new StreamController(); | |
40 var responseController = new StreamController(); | |
41 var server = new json_rpc.Server( | |
42 requestController.stream, responseController.sink); | |
43 | |
44 var hasListenCompeted = false; | 47 var hasListenCompeted = false; |
45 expect(server.listen().then((_) => hasListenCompeted = true), completes); | 48 expect(server.listen().then((_) => hasListenCompeted = true), completes); |
46 | 49 |
47 return pumpEventQueue().then((_) { | 50 return pumpEventQueue().then((_) { |
48 expect(hasListenCompeted, isFalse); | 51 expect(hasListenCompeted, isFalse); |
49 | 52 |
50 // This should cause listen to complete. | 53 // This should cause listen to complete. |
51 return requestController.close(); | 54 return requestController.close(); |
52 }); | 55 }); |
53 }); | 56 }); |
54 | 57 |
55 test(".listen returns a stream error", () { | 58 test(".listen returns a stream error", () { |
56 var requestController = new StreamController(); | |
57 var responseController = new StreamController(); | |
58 var server = new json_rpc.Server( | |
59 requestController.stream, responseController.sink); | |
60 | |
61 expect(server.listen(), throwsA('oh no')); | 59 expect(server.listen(), throwsA('oh no')); |
62 requestController.addError('oh no'); | 60 requestController.addError('oh no'); |
63 }); | 61 }); |
64 | 62 |
65 test(".listen can't be called twice", () { | 63 test(".listen can't be called twice", () { |
66 var requestController = new StreamController(); | |
67 var responseController = new StreamController(); | |
68 var server = new json_rpc.Server( | |
69 requestController.stream, responseController.sink); | |
70 server.listen(); | 64 server.listen(); |
71 | 65 |
72 expect(() => server.listen(), throwsStateError); | 66 expect(() => server.listen(), throwsStateError); |
73 }); | 67 }); |
74 | 68 |
75 test(".close cancels the stream subscription and closes the sink", () { | 69 test(".close cancels the stream subscription and closes the sink", () { |
76 var requestController = new StreamController(); | 70 // Work around sdk#19095. |
77 var responseController = new StreamController(); | 71 responseController.stream.listen(null); |
78 var server = new json_rpc.Server( | |
79 requestController.stream, responseController.sink); | |
80 | 72 |
81 expect(server.listen(), completes); | 73 expect(server.listen(), completes); |
82 | 74 |
83 expect(server.isClosed, isFalse); | 75 expect(server.isClosed, isFalse); |
84 expect(server.close(), completes); | 76 expect(server.close(), completes); |
85 expect(server.isClosed, isTrue); | 77 expect(server.isClosed, isTrue); |
86 | 78 |
87 expect(() => requestController.stream.listen((_) {}), throwsStateError); | 79 expect(() => requestController.stream.listen((_) {}), throwsStateError); |
88 expect(responseController.isClosed, isTrue); | 80 expect(responseController.isClosed, isTrue); |
89 }); | 81 }); |
90 | |
91 test(".close can't be called before .listen", () { | |
92 var requestController = new StreamController(); | |
93 var responseController = new StreamController(); | |
94 var server = new json_rpc.Server( | |
95 requestController.stream, responseController.sink); | |
96 | |
97 expect(() => server.close(), throwsStateError); | |
98 }); | |
99 } | 82 } |
OLD | NEW |