Chromium Code Reviews| Index: pkg/json_rpc_2/test/server/invalid_request_test.dart |
| diff --git a/pkg/json_rpc_2/test/server/invalid_request_test.dart b/pkg/json_rpc_2/test/server/invalid_request_test.dart |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..d18439af50b2f6fd6819abf4ae6c58da190c80ed |
| --- /dev/null |
| +++ b/pkg/json_rpc_2/test/server/invalid_request_test.dart |
| @@ -0,0 +1,89 @@ |
| +// Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file |
| +// for details. All rights reserved. Use of this source code is governed by a |
| +// BSD-style license that can be found in the LICENSE file. |
| + |
| +library json_rpc_2.test.server.invalid_request_test; |
| + |
| +import 'dart:convert'; |
| + |
| +import 'package:unittest/unittest.dart'; |
| +import 'package:json_rpc_2/error_code.dart' as error_code; |
| +import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc; |
| + |
| +import 'utils.dart'; |
| + |
| +void main() { |
| + var server; |
| + setUp(() => server = new json_rpc.Server()); |
| + |
| + test("a non-Array/Object request is invalid", () { |
| + expectErrorResponse(server, 'foo', error_code.INVALID_REQUEST, |
| + 'Requests must be Arrays or Objects.'); |
|
Bob Nystrom
2014/03/20 18:25:58
I think this error message should be singular.
nweiz
2014/03/20 22:55:41
Done.
|
| + }); |
| + |
| + test("requests must have a jsonrpc key", () { |
| + expectErrorResponse(server, { |
| + 'method': 'foo', |
| + 'id': 1234 |
| + }, error_code.INVALID_REQUEST, 'Requests must contain a "jsonrpc" key.'); |
|
Bob Nystrom
2014/03/20 18:25:58
Ditto.
nweiz
2014/03/20 22:55:41
Done.
|
| + }); |
| + |
| + test("the jsonrpc version must be 2.0", () { |
| + expectErrorResponse(server, { |
| + 'jsonrpc': '1.0', |
| + 'method': 'foo', |
| + 'id': 1234 |
| + }, |
| + error_code.INVALID_REQUEST, |
|
Bob Nystrom
2014/03/20 18:25:58
Nit: move this to the previous line? Here and belo
nweiz
2014/03/20 22:55:41
Done.
|
| + 'Invalid JSON-RPC version "1.0", expected "2.0".'); |
| + }); |
| + |
| + test("requests must have a method key", () { |
| + expectErrorResponse(server, { |
| + 'jsonrpc': '2.0', |
| + 'id': 1234 |
| + }, error_code.INVALID_REQUEST, 'Requests must contain a "method" key.'); |
| + }); |
| + |
| + test("request method must be a string", () { |
| + expectErrorResponse(server, { |
| + 'jsonrpc': '2.0', |
| + 'method': 1234, |
| + 'id': 1234 |
| + }, |
| + error_code.INVALID_REQUEST, |
| + 'Request method must be a string, but was "1234".'); |
| + }); |
| + |
| + test("request params must be an Array or Object", () { |
| + expectErrorResponse(server, { |
| + 'jsonrpc': '2.0', |
| + 'method': 'foo', |
| + 'params': 1234, |
| + 'id': 1234 |
| + }, |
| + error_code.INVALID_REQUEST, |
| + 'Request params must be an Array or an Object, but was "1234".'); |
| + }); |
| + |
| + test("request id must be an Array or Object", () { |
|
Bob Nystrom
2014/03/20 18:25:58
Fix description.
|
| + expect(server.handleRequest({ |
| + 'jsonrpc': '2.0', |
| + 'method': 'foo', |
| + 'id': {'bad': 'id'} |
| + }), completion(equals({ |
| + 'jsonrpc': '2.0', |
| + 'id': null, |
| + 'error': { |
| + 'code': error_code.INVALID_REQUEST, |
| + 'message': 'Request id must be a string, number, or null, but was ' |
| + '"{bad: id}".', |
| + 'data': {'request': { |
| + 'jsonrpc': '2.0', |
| + 'method': 'foo', |
| + 'id': {'bad': 'id'} |
| + }} |
| + } |
| + }))); |
| + }); |
| +} |