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