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 library json_rpc_2.test.server.util; | 5 library json_rpc_2.test.server.util; |
6 | 6 |
| 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
| 9 |
| 10 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; |
| 11 import 'package:json_rpc_2/error_code.dart' as error_code; |
7 import 'package:unittest/unittest.dart'; | 12 import 'package:unittest/unittest.dart'; |
8 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; | |
9 | 13 |
10 void expectErrorResponse(json_rpc.Server server, request, int errorCode, | 14 /// A controller used to test a [json_rpc.Server]. |
| 15 class ServerController { |
| 16 /// The controller for the server's request stream. |
| 17 final _requestController = new StreamController<String>(); |
| 18 |
| 19 /// The controller for the server's response sink. |
| 20 final _responseController = new StreamController<String>(); |
| 21 |
| 22 /// The server. |
| 23 json_rpc.Server get server => _server; |
| 24 json_rpc.Server _server; |
| 25 |
| 26 ServerController() { |
| 27 _server = new json_rpc.Server( |
| 28 _requestController.stream, _responseController.sink); |
| 29 _server.listen(); |
| 30 } |
| 31 |
| 32 /// Passes [request], a decoded request, to [server] and returns its decoded |
| 33 /// response. |
| 34 Future handleRequest(request) => |
| 35 handleJsonRequest(JSON.encode(request)).then(JSON.decode); |
| 36 |
| 37 /// Passes [request], a JSON-encoded request, to [server] and returns its |
| 38 /// encoded response. |
| 39 Future handleJsonRequest(String request) { |
| 40 _requestController.add(request); |
| 41 return _responseController.stream.first; |
| 42 } |
| 43 } |
| 44 |
| 45 /// Expects that [controller]'s server will return an error response to |
| 46 /// [request] with the given [errorCode], [message], and [data]. |
| 47 void expectErrorResponse(ServerController controller, request, int errorCode, |
11 String message, {data}) { | 48 String message, {data}) { |
12 var id; | 49 var id; |
13 if (request is Map) id = request['id']; | 50 if (request is Map) id = request['id']; |
14 if (data == null) data = {'request': request}; | 51 if (data == null) data = {'request': request}; |
15 | 52 |
16 expect(server.handleRequest(request), completion(equals({ | 53 expect(controller.handleRequest(request), completion(equals({ |
17 'jsonrpc': '2.0', | 54 'jsonrpc': '2.0', |
18 'id': id, | 55 'id': id, |
19 'error': { | 56 'error': { |
20 'code': errorCode, | 57 'code': errorCode, |
21 'message': message, | 58 'message': message, |
22 'data': data | 59 'data': data |
23 } | 60 } |
24 }))); | 61 }))); |
25 } | 62 } |
26 | 63 |
| 64 /// Returns a matcher that matches a [json_rpc.RpcException] with an |
| 65 /// `invalid_params` error code. |
27 Matcher throwsInvalidParams(String message) { | 66 Matcher throwsInvalidParams(String message) { |
28 return throwsA(predicate((error) { | 67 return throwsA(predicate((error) { |
29 expect(error, new isInstanceOf<json_rpc.RpcException>()); | 68 expect(error, new isInstanceOf<json_rpc.RpcException>()); |
| 69 expect(error.code, equals(error_code.INVALID_PARAMS)); |
30 expect(error.message, equals(message)); | 70 expect(error.message, equals(message)); |
31 return true; | 71 return true; |
32 })); | 72 })); |
33 } | 73 } |
| 74 |
| 75 /// Returns a [Future] that completes after pumping the event queue [times] |
| 76 /// times. By default, this should pump the event queue enough times to allow |
| 77 /// any code to run, as long as it's not waiting on some external event. |
| 78 Future pumpEventQueue([int times = 20]) { |
| 79 if (times == 0) return new Future.value(); |
| 80 // We use a delayed future to allow microtask events to finish. The |
| 81 // Future.value or Future() constructors use scheduleMicrotask themselves and |
| 82 // would therefore not wait for microtask callbacks that are scheduled after |
| 83 // invoking this method. |
| 84 return new Future.delayed(Duration.ZERO, () => pumpEventQueue(times - 1)); |
| 85 } |
OLD | NEW |