| 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.parameters_test; | |
| 6 | |
| 7 import 'package:unittest/unittest.dart'; | |
| 8 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; | |
| 9 | |
| 10 import 'utils.dart'; | |
| 11 | |
| 12 void main() { | |
| 13 group("with named parameters", () { | |
| 14 var parameters; | |
| 15 setUp(() { | |
| 16 parameters = new json_rpc.Parameters("foo", { | |
| 17 "num": 1.5, | |
| 18 "int": 1, | |
| 19 "bool": true, | |
| 20 "string": "zap", | |
| 21 "list": [1, 2, 3], | |
| 22 "date-time": "1990-01-01 00:00:00.000", | |
| 23 "uri": "http://dartlang.org", | |
| 24 "invalid-uri": "http://[::1", | |
| 25 "map": { | |
| 26 "num": 4.2, | |
| 27 "bool": false | |
| 28 } | |
| 29 }); | |
| 30 }); | |
| 31 | |
| 32 test("value returns the wrapped value", () { | |
| 33 expect(parameters.value, equals({ | |
| 34 "num": 1.5, | |
| 35 "int": 1, | |
| 36 "bool": true, | |
| 37 "string": "zap", | |
| 38 "list": [1, 2, 3], | |
| 39 "date-time": "1990-01-01 00:00:00.000", | |
| 40 "uri": "http://dartlang.org", | |
| 41 "invalid-uri": "http://[::1", | |
| 42 "map": { | |
| 43 "num": 4.2, | |
| 44 "bool": false | |
| 45 } | |
| 46 })); | |
| 47 }); | |
| 48 | |
| 49 test("[int] throws a parameter error", () { | |
| 50 expect(() => parameters[0], | |
| 51 throwsInvalidParams('Parameters for method "foo" must be passed by ' | |
| 52 'position.')); | |
| 53 }); | |
| 54 | |
| 55 test("[].value returns existing parameters", () { | |
| 56 expect(parameters['num'].value, equals(1.5)); | |
| 57 }); | |
| 58 | |
| 59 test("[].valueOr returns existing parameters", () { | |
| 60 expect(parameters['num'].valueOr(7), equals(1.5)); | |
| 61 }); | |
| 62 | |
| 63 test("[].value fails for absent parameters", () { | |
| 64 expect(() => parameters['fblthp'].value, | |
| 65 throwsInvalidParams('Request for method "foo" is missing required ' | |
| 66 'parameter "fblthp".')); | |
| 67 }); | |
| 68 | |
| 69 test("[].valueOr succeeds for absent parameters", () { | |
| 70 expect(parameters['fblthp'].valueOr(7), equals(7)); | |
| 71 }); | |
| 72 | |
| 73 test("[].exists returns true for existing parameters", () { | |
| 74 expect(parameters['num'].exists, isTrue); | |
| 75 }); | |
| 76 | |
| 77 test("[].exists returns false for missing parameters", () { | |
| 78 expect(parameters['fblthp'].exists, isFalse); | |
| 79 }); | |
| 80 | |
| 81 test("[].asNum returns numeric parameters", () { | |
| 82 expect(parameters['num'].asNum, equals(1.5)); | |
| 83 expect(parameters['int'].asNum, equals(1)); | |
| 84 }); | |
| 85 | |
| 86 test("[].asNumOr returns numeric parameters", () { | |
| 87 expect(parameters['num'].asNumOr(7), equals(1.5)); | |
| 88 }); | |
| 89 | |
| 90 test("[].asNum fails for non-numeric parameters", () { | |
| 91 expect(() => parameters['bool'].asNum, | |
| 92 throwsInvalidParams('Parameter "bool" for method "foo" must be a ' | |
| 93 'number, but was true.')); | |
| 94 }); | |
| 95 | |
| 96 test("[].asNumOr fails for non-numeric parameters", () { | |
| 97 expect(() => parameters['bool'].asNumOr(7), | |
| 98 throwsInvalidParams('Parameter "bool" for method "foo" must be a ' | |
| 99 'number, but was true.')); | |
| 100 }); | |
| 101 | |
| 102 test("[].asNum fails for absent parameters", () { | |
| 103 expect(() => parameters['fblthp'].asNum, | |
| 104 throwsInvalidParams('Request for method "foo" is missing required ' | |
| 105 'parameter "fblthp".')); | |
| 106 }); | |
| 107 | |
| 108 test("[].asNumOr succeeds for absent parameters", () { | |
| 109 expect(parameters['fblthp'].asNumOr(7), equals(7)); | |
| 110 }); | |
| 111 | |
| 112 test("[].asInt returns integer parameters", () { | |
| 113 expect(parameters['int'].asInt, equals(1)); | |
| 114 }); | |
| 115 | |
| 116 test("[].asIntOr returns integer parameters", () { | |
| 117 expect(parameters['int'].asIntOr(7), equals(1)); | |
| 118 }); | |
| 119 | |
| 120 test("[].asInt fails for non-integer parameters", () { | |
| 121 expect(() => parameters['bool'].asInt, | |
| 122 throwsInvalidParams('Parameter "bool" for method "foo" must be an ' | |
| 123 'integer, but was true.')); | |
| 124 }); | |
| 125 | |
| 126 test("[].asIntOr succeeds for absent parameters", () { | |
| 127 expect(parameters['fblthp'].asIntOr(7), equals(7)); | |
| 128 }); | |
| 129 | |
| 130 test("[].asBool returns boolean parameters", () { | |
| 131 expect(parameters['bool'].asBool, isTrue); | |
| 132 }); | |
| 133 | |
| 134 test("[].asBoolOr returns boolean parameters", () { | |
| 135 expect(parameters['bool'].asBoolOr(false), isTrue); | |
| 136 }); | |
| 137 | |
| 138 test("[].asBoolOr fails for non-boolean parameters", () { | |
| 139 expect(() => parameters['int'].asBool, | |
| 140 throwsInvalidParams('Parameter "int" for method "foo" must be a ' | |
| 141 'boolean, but was 1.')); | |
| 142 }); | |
| 143 | |
| 144 test("[].asBoolOr succeeds for absent parameters", () { | |
| 145 expect(parameters['fblthp'].asBoolOr(false), isFalse); | |
| 146 }); | |
| 147 | |
| 148 test("[].asString returns string parameters", () { | |
| 149 expect(parameters['string'].asString, equals("zap")); | |
| 150 }); | |
| 151 | |
| 152 test("[].asStringOr returns string parameters", () { | |
| 153 expect(parameters['string'].asStringOr("bap"), equals("zap")); | |
| 154 }); | |
| 155 | |
| 156 test("[].asString fails for non-string parameters", () { | |
| 157 expect(() => parameters['int'].asString, | |
| 158 throwsInvalidParams('Parameter "int" for method "foo" must be a ' | |
| 159 'string, but was 1.')); | |
| 160 }); | |
| 161 | |
| 162 test("[].asStringOr succeeds for absent parameters", () { | |
| 163 expect(parameters['fblthp'].asStringOr("bap"), equals("bap")); | |
| 164 }); | |
| 165 | |
| 166 test("[].asList returns list parameters", () { | |
| 167 expect(parameters['list'].asList, equals([1, 2, 3])); | |
| 168 }); | |
| 169 | |
| 170 test("[].asListOr returns list parameters", () { | |
| 171 expect(parameters['list'].asListOr([5, 6, 7]), equals([1, 2, 3])); | |
| 172 }); | |
| 173 | |
| 174 test("[].asList fails for non-list parameters", () { | |
| 175 expect(() => parameters['int'].asList, | |
| 176 throwsInvalidParams('Parameter "int" for method "foo" must be an ' | |
| 177 'Array, but was 1.')); | |
| 178 }); | |
| 179 | |
| 180 test("[].asListOr succeeds for absent parameters", () { | |
| 181 expect(parameters['fblthp'].asListOr([5, 6, 7]), equals([5, 6, 7])); | |
| 182 }); | |
| 183 | |
| 184 test("[].asMap returns map parameters", () { | |
| 185 expect(parameters['map'].asMap, equals({"num": 4.2, "bool": false})); | |
| 186 }); | |
| 187 | |
| 188 test("[].asMapOr returns map parameters", () { | |
| 189 expect(parameters['map'].asMapOr({}), | |
| 190 equals({"num": 4.2, "bool": false})); | |
| 191 }); | |
| 192 | |
| 193 test("[].asMap fails for non-map parameters", () { | |
| 194 expect(() => parameters['int'].asMap, | |
| 195 throwsInvalidParams('Parameter "int" for method "foo" must be an ' | |
| 196 'Object, but was 1.')); | |
| 197 }); | |
| 198 | |
| 199 test("[].asMapOr succeeds for absent parameters", () { | |
| 200 expect(parameters['fblthp'].asMapOr({}), equals({})); | |
| 201 }); | |
| 202 | |
| 203 test("[].asDateTime returns date/time parameters", () { | |
| 204 expect(parameters['date-time'].asDateTime, equals(new DateTime(1990))); | |
| 205 }); | |
| 206 | |
| 207 test("[].asDateTimeOr returns date/time parameters", () { | |
| 208 expect(parameters['date-time'].asDateTimeOr(new DateTime(2014)), | |
| 209 equals(new DateTime(1990))); | |
| 210 }); | |
| 211 | |
| 212 test("[].asDateTime fails for non-date/time parameters", () { | |
| 213 expect(() => parameters['int'].asDateTime, | |
| 214 throwsInvalidParams('Parameter "int" for method "foo" must be a ' | |
| 215 'string, but was 1.')); | |
| 216 }); | |
| 217 | |
| 218 test("[].asDateTimeOr succeeds for absent parameters", () { | |
| 219 expect(parameters['fblthp'].asDateTimeOr(new DateTime(2014)), | |
| 220 equals(new DateTime(2014))); | |
| 221 }); | |
| 222 | |
| 223 test("[].asDateTime fails for non-date/time parameters", () { | |
| 224 expect(() => parameters['int'].asDateTime, | |
| 225 throwsInvalidParams('Parameter "int" for method "foo" must be a ' | |
| 226 'string, but was 1.')); | |
| 227 }); | |
| 228 | |
| 229 test("[].asDateTime fails for invalid date/times", () { | |
| 230 expect(() => parameters['string'].asDateTime, | |
| 231 throwsInvalidParams('Parameter "string" for method "foo" must be a ' | |
| 232 'valid date/time, but was "zap".\n' | |
| 233 'Invalid date format')); | |
| 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 'Missing end `]` to match `[` in host')); | |
| 267 }); | |
| 268 | |
| 269 group("with a nested parameter map", () { | |
| 270 var nested; | |
| 271 setUp(() => nested = parameters['map']); | |
| 272 | |
| 273 test("[int] fails with a type error", () { | |
| 274 expect(() => nested[0], | |
| 275 throwsInvalidParams('Parameter "map" for method "foo" must be an ' | |
| 276 'Array, but was {"num":4.2,"bool":false}.')); | |
| 277 }); | |
| 278 | |
| 279 test("[].value returns existing parameters", () { | |
| 280 expect(nested['num'].value, equals(4.2)); | |
| 281 expect(nested['bool'].value, isFalse); | |
| 282 }); | |
| 283 | |
| 284 test("[].value fails for absent parameters", () { | |
| 285 expect(() => nested['fblthp'].value, | |
| 286 throwsInvalidParams('Request for method "foo" is missing required ' | |
| 287 'parameter map.fblthp.')); | |
| 288 }); | |
| 289 | |
| 290 test("typed getters return correctly-typed parameters", () { | |
| 291 expect(nested['num'].asNum, equals(4.2)); | |
| 292 }); | |
| 293 | |
| 294 test("typed getters fail for incorrectly-typed parameters", () { | |
| 295 expect(() => nested['bool'].asNum, | |
| 296 throwsInvalidParams('Parameter map.bool for method "foo" must be ' | |
| 297 'a number, but was false.')); | |
| 298 }); | |
| 299 }); | |
| 300 | |
| 301 group("with a nested parameter list", () { | |
| 302 var nested; | |
| 303 setUp(() => nested = parameters['list']); | |
| 304 | |
| 305 test("[string] fails with a type error", () { | |
| 306 expect(() => nested['foo'], | |
| 307 throwsInvalidParams('Parameter "list" for method "foo" must be an ' | |
| 308 'Object, but was [1,2,3].')); | |
| 309 }); | |
| 310 | |
| 311 test("[].value returns existing parameters", () { | |
| 312 expect(nested[0].value, equals(1)); | |
| 313 expect(nested[1].value, equals(2)); | |
| 314 }); | |
| 315 | |
| 316 test("[].value fails for absent parameters", () { | |
| 317 expect(() => nested[5].value, | |
| 318 throwsInvalidParams('Request for method "foo" is missing required ' | |
| 319 'parameter list[5].')); | |
| 320 }); | |
| 321 | |
| 322 test("typed getters return correctly-typed parameters", () { | |
| 323 expect(nested[0].asInt, equals(1)); | |
| 324 }); | |
| 325 | |
| 326 test("typed getters fail for incorrectly-typed parameters", () { | |
| 327 expect(() => nested[0].asBool, | |
| 328 throwsInvalidParams('Parameter list[0] for method "foo" must be ' | |
| 329 'a boolean, but was 1.')); | |
| 330 }); | |
| 331 }); | |
| 332 }); | |
| 333 | |
| 334 group("with positional parameters", () { | |
| 335 var parameters; | |
| 336 setUp(() => parameters = new json_rpc.Parameters("foo", [1, 2, 3, 4, 5])); | |
| 337 | |
| 338 test("value returns the wrapped value", () { | |
| 339 expect(parameters.value, equals([1, 2, 3, 4, 5])); | |
| 340 }); | |
| 341 | |
| 342 test("[string] throws a parameter error", () { | |
| 343 expect(() => parameters['foo'], | |
| 344 throwsInvalidParams('Parameters for method "foo" must be passed by ' | |
| 345 'name.')); | |
| 346 }); | |
| 347 | |
| 348 test("[].value returns existing parameters", () { | |
| 349 expect(parameters[2].value, equals(3)); | |
| 350 }); | |
| 351 | |
| 352 test("[].value fails for out-of-range parameters", () { | |
| 353 expect(() => parameters[10].value, | |
| 354 throwsInvalidParams('Request for method "foo" is missing required ' | |
| 355 'parameter 11.')); | |
| 356 }); | |
| 357 | |
| 358 test("[].exists returns true for existing parameters", () { | |
| 359 expect(parameters[0].exists, isTrue); | |
| 360 }); | |
| 361 | |
| 362 test("[].exists returns false for missing parameters", () { | |
| 363 expect(parameters[10].exists, isFalse); | |
| 364 }); | |
| 365 }); | |
| 366 | |
| 367 test("with a complex parameter path", () { | |
| 368 var parameters = new json_rpc.Parameters("foo", { | |
| 369 'bar baz': [0, 1, 2, {'bang.zap': {'\n': 'qux'}}] | |
| 370 }); | |
| 371 | |
| 372 expect(() => parameters['bar baz'][3]['bang.zap']['\n']['bip'], | |
| 373 throwsInvalidParams('Parameter "bar baz"[3]."bang.zap"."\\n" for ' | |
| 374 'method "foo" must be an Object, but was "qux".')); | |
| 375 }); | |
| 376 } | |
| OLD | NEW |