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.server.server_test; |
| 6 |
| 7 import 'dart:convert'; |
| 8 |
| 9 import 'package:unittest/unittest.dart'; |
| 10 import 'package:json_rpc_2/error_code.dart' as error_code; |
| 11 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; |
| 12 |
| 13 import 'utils.dart'; |
| 14 |
| 15 void main() { |
| 16 var server; |
| 17 setUp(() => server = new json_rpc.Server()); |
| 18 |
| 19 test("calls a registered method with the given name", () { |
| 20 server.registerMethod('foo', (params) { |
| 21 return {'params': params.value}; |
| 22 }); |
| 23 |
| 24 expect(server.handleRequest({ |
| 25 'jsonrpc': '2.0', |
| 26 'method': 'foo', |
| 27 'params': {'param': 'value'}, |
| 28 'id': 1234 |
| 29 }), completion(equals({ |
| 30 'jsonrpc': '2.0', |
| 31 'result': {'params': {'param': 'value'}}, |
| 32 'id': 1234 |
| 33 }))); |
| 34 }); |
| 35 |
| 36 test("calls a method that takes no parameters", () { |
| 37 server.registerMethod('foo', () => 'foo'); |
| 38 |
| 39 expect(server.handleRequest({ |
| 40 'jsonrpc': '2.0', |
| 41 'method': 'foo', |
| 42 'id': 1234 |
| 43 }), completion(equals({ |
| 44 'jsonrpc': '2.0', |
| 45 'result': 'foo', |
| 46 'id': 1234 |
| 47 }))); |
| 48 }); |
| 49 |
| 50 test("a method that takes no parameters rejects parameters", () { |
| 51 server.registerMethod('foo', () => 'foo'); |
| 52 |
| 53 expectErrorResponse(server, { |
| 54 'jsonrpc': '2.0', |
| 55 'method': 'foo', |
| 56 'params': {}, |
| 57 'id': 1234 |
| 58 }, |
| 59 error_code.INVALID_PARAMS, |
| 60 'No parameters are allowed for method "foo".'); |
| 61 }); |
| 62 |
| 63 test("an unexpected error in a method is captured", () { |
| 64 server.registerMethod('foo', () => throw new FormatException('bad format')); |
| 65 |
| 66 expect(server.handleRequest({ |
| 67 'jsonrpc': '2.0', |
| 68 'method': 'foo', |
| 69 'id': 1234 |
| 70 }), completion({ |
| 71 'jsonrpc': '2.0', |
| 72 'id': 1234, |
| 73 'error': { |
| 74 'code': error_code.SERVER_ERROR, |
| 75 'message': 'bad format', |
| 76 'data': { |
| 77 'request': {'jsonrpc': '2.0', 'method': 'foo', 'id': 1234}, |
| 78 'full': 'FormatException: bad format', |
| 79 'stack': contains('server_test.dart') |
| 80 } |
| 81 } |
| 82 })); |
| 83 }); |
| 84 |
| 85 test("doesn't return a result for a notification", () { |
| 86 server.registerMethod('foo', (args) => 'result'); |
| 87 |
| 88 expect(server.handleRequest({ |
| 89 'jsonrpc': '2.0', |
| 90 'method': 'foo', |
| 91 'params': {} |
| 92 }), completion(isNull)); |
| 93 }); |
| 94 |
| 95 group("JSON", () { |
| 96 test("handles a request parsed from JSON", () { |
| 97 server.registerMethod('foo', (params) { |
| 98 return {'params': params.value}; |
| 99 }); |
| 100 |
| 101 expect(server.parseRequest(JSON.encode({ |
| 102 'jsonrpc': '2.0', |
| 103 'method': 'foo', |
| 104 'params': {'param': 'value'}, |
| 105 'id': 1234 |
| 106 })), completion(equals(JSON.encode({ |
| 107 'jsonrpc': '2.0', |
| 108 'result': {'params': {'param': 'value'}}, |
| 109 'id': 1234 |
| 110 })))); |
| 111 }); |
| 112 |
| 113 test("handles a notification parsed from JSON", () { |
| 114 server.registerMethod('foo', (params) { |
| 115 return {'params': params}; |
| 116 }); |
| 117 |
| 118 expect(server.parseRequest(JSON.encode({ |
| 119 'jsonrpc': '2.0', |
| 120 'method': 'foo', |
| 121 'params': {'param': 'value'} |
| 122 })), completion(isNull)); |
| 123 }); |
| 124 |
| 125 test("a JSON parse error is rejected", () { |
| 126 expect(server.parseRequest('invalid json {'), |
| 127 completion(equals(JSON.encode({ |
| 128 'jsonrpc': '2.0', |
| 129 'error': { |
| 130 'code': error_code.PARSE_ERROR, |
| 131 'message': "Invalid JSON: Unexpected character at 0: 'invalid json " |
| 132 "{'", |
| 133 'data': {'request': 'invalid json {'} |
| 134 }, |
| 135 'id': null |
| 136 })))); |
| 137 }); |
| 138 }); |
| 139 |
| 140 group("fallbacks", () { |
| 141 test("calls a fallback if no method matches", () { |
| 142 server.registerMethod('foo', () => 'foo'); |
| 143 server.registerMethod('bar', () => 'foo'); |
| 144 server.registerFallback((params) => {'fallback': params.value}); |
| 145 |
| 146 expect(server.handleRequest({ |
| 147 'jsonrpc': '2.0', |
| 148 'method': 'baz', |
| 149 'params': {'param': 'value'}, |
| 150 'id': 1234 |
| 151 }), completion(equals({ |
| 152 'jsonrpc': '2.0', |
| 153 'result': {'fallback': {'param': 'value'}}, |
| 154 'id': 1234 |
| 155 }))); |
| 156 }); |
| 157 |
| 158 test("calls the first matching fallback", () { |
| 159 server.registerFallback((params) => |
| 160 throw new json_rpc.RpcException.methodNotFound(params.method)); |
| 161 |
| 162 server.registerFallback((params) => 'fallback 2'); |
| 163 server.registerFallback((params) => 'fallback 3'); |
| 164 |
| 165 expect(server.handleRequest({ |
| 166 'jsonrpc': '2.0', |
| 167 'method': 'fallback 2', |
| 168 'id': 1234 |
| 169 }), completion(equals({ |
| 170 'jsonrpc': '2.0', |
| 171 'result': 'fallback 2', |
| 172 'id': 1234 |
| 173 }))); |
| 174 }); |
| 175 |
| 176 test("an unexpected error in a fallback is captured", () { |
| 177 server.registerFallback((_) => throw new FormatException('bad format')); |
| 178 |
| 179 expect(server.handleRequest({ |
| 180 'jsonrpc': '2.0', |
| 181 'method': 'foo', |
| 182 'id': 1234 |
| 183 }), completion({ |
| 184 'jsonrpc': '2.0', |
| 185 'id': 1234, |
| 186 'error': { |
| 187 'code': error_code.SERVER_ERROR, |
| 188 'message': 'bad format', |
| 189 'data': { |
| 190 'request': {'jsonrpc': '2.0', 'method': 'foo', 'id': 1234}, |
| 191 'full': 'FormatException: bad format', |
| 192 'stack': contains('server_test.dart') |
| 193 } |
| 194 } |
| 195 })); |
| 196 }); |
| 197 }); |
| 198 |
| 199 test("disallows multiple methods with the same name", () { |
| 200 server.registerMethod('foo', () => null); |
| 201 expect(() => server.registerMethod('foo', () => null), throwsArgumentError); |
| 202 }); |
| 203 } |
OLD | NEW |