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.parameters_test; | 5 library json_rpc_2.test.server.parameters_test; |
6 | 6 |
7 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
8 import 'package:json_rpc_2/error_code.dart' as error_code; | 8 import 'package:json_rpc_2/error_code.dart' as error_code; |
9 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; | 9 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; |
10 | 10 |
11 import 'utils.dart'; | 11 import 'utils.dart'; |
12 | 12 |
13 void main() { | 13 void main() { |
14 group("with named parameters", () { | 14 group("with named parameters", () { |
15 var parameters; | 15 var parameters; |
16 setUp(() { | 16 setUp(() { |
17 parameters = new json_rpc.Parameters("foo", { | 17 parameters = new json_rpc.Parameters("foo", { |
18 "num": 1.5, | 18 "num": 1.5, |
19 "int": 1, | 19 "int": 1, |
20 "bool": true, | 20 "bool": true, |
21 "string": "zap", | 21 "string": "zap", |
22 "list": [1, 2, 3], | 22 "list": [1, 2, 3], |
| 23 "date-time": "1990-01-01 00:00:00.000", |
| 24 "uri": "http://dartlang.org", |
| 25 "invalid-uri": "http://[::1", |
23 "map": { | 26 "map": { |
24 "num": 4.2, | 27 "num": 4.2, |
25 "bool": false | 28 "bool": false |
26 } | 29 } |
27 }); | 30 }); |
28 }); | 31 }); |
29 | 32 |
30 test("value returns the wrapped value", () { | 33 test("value returns the wrapped value", () { |
31 expect(parameters.value, equals({ | 34 expect(parameters.value, equals({ |
32 "num": 1.5, | 35 "num": 1.5, |
33 "int": 1, | 36 "int": 1, |
34 "bool": true, | 37 "bool": true, |
35 "string": "zap", | 38 "string": "zap", |
36 "list": [1, 2, 3], | 39 "list": [1, 2, 3], |
| 40 "date-time": "1990-01-01 00:00:00.000", |
| 41 "uri": "http://dartlang.org", |
| 42 "invalid-uri": "http://[::1", |
37 "map": { | 43 "map": { |
38 "num": 4.2, | 44 "num": 4.2, |
39 "bool": false | 45 "bool": false |
40 } | 46 } |
41 })); | 47 })); |
42 }); | 48 }); |
43 | 49 |
44 test("[int] throws a parameter error", () { | 50 test("[int] throws a parameter error", () { |
45 expect(() => parameters[0], | 51 expect(() => parameters[0], |
46 throwsInvalidParams('Parameters for method "foo" must be passed by ' | 52 throwsInvalidParams('Parameters for method "foo" must be passed by ' |
(...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
188 test("[].asMap fails for non-map parameters", () { | 194 test("[].asMap fails for non-map parameters", () { |
189 expect(() => parameters['int'].asMap, | 195 expect(() => parameters['int'].asMap, |
190 throwsInvalidParams('Parameter "int" for method "foo" must be an ' | 196 throwsInvalidParams('Parameter "int" for method "foo" must be an ' |
191 'Object, but was 1.')); | 197 'Object, but was 1.')); |
192 }); | 198 }); |
193 | 199 |
194 test("[].asMapOr succeeds for absent parameters", () { | 200 test("[].asMapOr succeeds for absent parameters", () { |
195 expect(parameters['fblthp'].asMapOr({}), equals({})); | 201 expect(parameters['fblthp'].asMapOr({}), equals({})); |
196 }); | 202 }); |
197 | 203 |
| 204 test("[].asDateTime returns date/time parameters", () { |
| 205 expect(parameters['date-time'].asDateTime, equals(new DateTime(1990))); |
| 206 }); |
| 207 |
| 208 test("[].asDateTimeOr returns date/time parameters", () { |
| 209 expect(parameters['date-time'].asDateTimeOr(new DateTime(2014)), |
| 210 equals(new DateTime(1990))); |
| 211 }); |
| 212 |
| 213 test("[].asDateTime fails for non-date/time parameters", () { |
| 214 expect(() => parameters['int'].asDateTime, |
| 215 throwsInvalidParams('Parameter "int" for method "foo" must be a ' |
| 216 'string, but was 1.')); |
| 217 }); |
| 218 |
| 219 test("[].asDateTimeOr succeeds for absent parameters", () { |
| 220 expect(parameters['fblthp'].asDateTimeOr(new DateTime(2014)), |
| 221 equals(new DateTime(2014))); |
| 222 }); |
| 223 |
| 224 test("[].asDateTime fails for non-date/time parameters", () { |
| 225 expect(() => parameters['int'].asDateTime, |
| 226 throwsInvalidParams('Parameter "int" for method "foo" must be a ' |
| 227 'string, but was 1.')); |
| 228 }); |
| 229 |
| 230 test("[].asDateTime fails for invalid date/times", () { |
| 231 expect(() => parameters['string'].asDateTime, |
| 232 throwsInvalidParams('Parameter "string" for method "foo" must be a ' |
| 233 'valid date/time, but was "zap".')); |
| 234 }); |
| 235 |
| 236 test("[].asUri returns URI parameters", () { |
| 237 expect(parameters['uri'].asUri, equals(Uri.parse('http://dartlang.org'))); |
| 238 }); |
| 239 |
| 240 test("[].asUriOr returns URI parameters", () { |
| 241 expect(parameters['uri'].asUriOr(Uri.parse('http://google.com')), |
| 242 equals(Uri.parse('http://dartlang.org'))); |
| 243 }); |
| 244 |
| 245 test("[].asUri fails for non-URI parameters", () { |
| 246 expect(() => parameters['int'].asUri, |
| 247 throwsInvalidParams('Parameter "int" for method "foo" must be a ' |
| 248 'string, but was 1.')); |
| 249 }); |
| 250 |
| 251 test("[].asUriOr succeeds for absent parameters", () { |
| 252 expect(parameters['fblthp'].asUriOr(Uri.parse('http://google.com')), |
| 253 equals(Uri.parse('http://google.com'))); |
| 254 }); |
| 255 |
| 256 test("[].asUri fails for non-URI parameters", () { |
| 257 expect(() => parameters['int'].asUri, |
| 258 throwsInvalidParams('Parameter "int" for method "foo" must be a ' |
| 259 'string, but was 1.')); |
| 260 }); |
| 261 |
| 262 test("[].asUri fails for invalid URIs", () { |
| 263 expect(() => parameters['invalid-uri'].asUri, |
| 264 throwsInvalidParams('Parameter "invalid-uri" for method "foo" must ' |
| 265 'be a valid URI, but was "http://[::1".\n' |
| 266 'Bad end of IPv6 host')); |
| 267 }); |
| 268 |
198 group("with a nested parameter map", () { | 269 group("with a nested parameter map", () { |
199 var nested; | 270 var nested; |
200 setUp(() => nested = parameters['map']); | 271 setUp(() => nested = parameters['map']); |
201 | 272 |
202 test("[int] fails with a type error", () { | 273 test("[int] fails with a type error", () { |
203 expect(() => nested[0], | 274 expect(() => nested[0], |
204 throwsInvalidParams('Parameter "map" for method "foo" must be an ' | 275 throwsInvalidParams('Parameter "map" for method "foo" must be an ' |
205 'Array, but was {"num":4.2,"bool":false}.')); | 276 'Array, but was {"num":4.2,"bool":false}.')); |
206 }); | 277 }); |
207 | 278 |
(...skipping 88 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
296 test("with a complex parameter path", () { | 367 test("with a complex parameter path", () { |
297 var parameters = new json_rpc.Parameters("foo", { | 368 var parameters = new json_rpc.Parameters("foo", { |
298 'bar baz': [0, 1, 2, {'bang.zap': {'\n': 'qux'}}] | 369 'bar baz': [0, 1, 2, {'bang.zap': {'\n': 'qux'}}] |
299 }); | 370 }); |
300 | 371 |
301 expect(() => parameters['bar baz'][3]['bang.zap']['\n']['bip'], | 372 expect(() => parameters['bar baz'][3]['bang.zap']['\n']['bip'], |
302 throwsInvalidParams('Parameter "bar baz"[3]."bang.zap"."\\n" for ' | 373 throwsInvalidParams('Parameter "bar baz"[3]."bang.zap"."\\n" for ' |
303 'method "foo" must be an Object, but was "qux".')); | 374 'method "foo" must be an Object, but was "qux".')); |
304 }); | 375 }); |
305 } | 376 } |
OLD | NEW |