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.invalid_request_test; | 5 library json_rpc_2.test.server.invalid_request_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("a non-Array/Object request is invalid", () { | 19 test("a non-Array/Object request is invalid", () { |
20 expectErrorResponse(server, 'foo', error_code.INVALID_REQUEST, | 20 expectErrorResponse(controller, 'foo', error_code.INVALID_REQUEST, |
21 'Request must be an Array or an Object.'); | 21 'Request must be an Array or an Object.'); |
22 }); | 22 }); |
23 | 23 |
24 test("requests must have a jsonrpc key", () { | 24 test("requests must have a jsonrpc key", () { |
25 expectErrorResponse(server, { | 25 expectErrorResponse(controller, { |
26 'method': 'foo', | 26 'method': 'foo', |
27 'id': 1234 | 27 'id': 1234 |
28 }, error_code.INVALID_REQUEST, 'Request must contain a "jsonrpc" key.'); | 28 }, error_code.INVALID_REQUEST, 'Request must contain a "jsonrpc" key.'); |
29 }); | 29 }); |
30 | 30 |
31 test("the jsonrpc version must be 2.0", () { | 31 test("the jsonrpc version must be 2.0", () { |
32 expectErrorResponse(server, { | 32 expectErrorResponse(controller, { |
33 'jsonrpc': '1.0', | 33 'jsonrpc': '1.0', |
34 'method': 'foo', | 34 'method': 'foo', |
35 'id': 1234 | 35 'id': 1234 |
36 }, error_code.INVALID_REQUEST, | 36 }, error_code.INVALID_REQUEST, |
37 'Invalid JSON-RPC version "1.0", expected "2.0".'); | 37 'Invalid JSON-RPC version "1.0", expected "2.0".'); |
38 }); | 38 }); |
39 | 39 |
40 test("requests must have a method key", () { | 40 test("requests must have a method key", () { |
41 expectErrorResponse(server, { | 41 expectErrorResponse(controller, { |
42 'jsonrpc': '2.0', | 42 'jsonrpc': '2.0', |
43 'id': 1234 | 43 'id': 1234 |
44 }, error_code.INVALID_REQUEST, 'Request must contain a "method" key.'); | 44 }, error_code.INVALID_REQUEST, 'Request must contain a "method" key.'); |
45 }); | 45 }); |
46 | 46 |
47 test("request method must be a string", () { | 47 test("request method must be a string", () { |
48 expectErrorResponse(server, { | 48 expectErrorResponse(controller, { |
49 'jsonrpc': '2.0', | 49 'jsonrpc': '2.0', |
50 'method': 1234, | 50 'method': 1234, |
51 'id': 1234 | 51 'id': 1234 |
52 }, error_code.INVALID_REQUEST, | 52 }, error_code.INVALID_REQUEST, |
53 'Request method must be a string, but was 1234.'); | 53 'Request method must be a string, but was 1234.'); |
54 }); | 54 }); |
55 | 55 |
56 test("request params must be an Array or Object", () { | 56 test("request params must be an Array or Object", () { |
57 expectErrorResponse(server, { | 57 expectErrorResponse(controller, { |
58 'jsonrpc': '2.0', | 58 'jsonrpc': '2.0', |
59 'method': 'foo', | 59 'method': 'foo', |
60 'params': 1234, | 60 'params': 1234, |
61 'id': 1234 | 61 'id': 1234 |
62 }, error_code.INVALID_REQUEST, | 62 }, error_code.INVALID_REQUEST, |
63 'Request params must be an Array or an Object, but was 1234.'); | 63 'Request params must be an Array or an Object, but was 1234.'); |
64 }); | 64 }); |
65 | 65 |
66 test("request id may not be an Array or Object", () { | 66 test("request id may not be an Array or Object", () { |
67 expect(server.handleRequest({ | 67 expect(controller.handleRequest({ |
68 'jsonrpc': '2.0', | 68 'jsonrpc': '2.0', |
69 'method': 'foo', | 69 'method': 'foo', |
70 'id': {'bad': 'id'} | 70 'id': {'bad': 'id'} |
71 }), completion(equals({ | 71 }), completion(equals({ |
72 'jsonrpc': '2.0', | 72 'jsonrpc': '2.0', |
73 'id': null, | 73 'id': null, |
74 'error': { | 74 'error': { |
75 'code': error_code.INVALID_REQUEST, | 75 'code': error_code.INVALID_REQUEST, |
76 'message': 'Request id must be a string, number, or null, but was ' | 76 'message': 'Request id must be a string, number, or null, but was ' |
77 '{"bad":"id"}.', | 77 '{"bad":"id"}.', |
78 'data': {'request': { | 78 'data': {'request': { |
79 'jsonrpc': '2.0', | 79 'jsonrpc': '2.0', |
80 'method': 'foo', | 80 'method': 'foo', |
81 'id': {'bad': 'id'} | 81 'id': {'bad': 'id'} |
82 }} | 82 }} |
83 } | 83 } |
84 }))); | 84 }))); |
85 }); | 85 }); |
86 } | 86 } |
OLD | NEW |