Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(138)

Unified Diff: pkg/json_rpc_2/test/server/server_test.dart

Issue 205533005: Create a package that implements a JSON-RPC 2.0 server. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « pkg/json_rpc_2/test/server/parameters_test.dart ('k') | pkg/json_rpc_2/test/server/utils.dart » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/json_rpc_2/test/server/server_test.dart
diff --git a/pkg/json_rpc_2/test/server/server_test.dart b/pkg/json_rpc_2/test/server/server_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..f1e0af5c7d70fcedc2017ea10c55adbd26e2f06e
--- /dev/null
+++ b/pkg/json_rpc_2/test/server/server_test.dart
@@ -0,0 +1,203 @@
+// 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.server_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("calls a registered method with the given name", () {
+ server.registerMethod('foo', (params) {
+ return {'params': params.value};
+ });
+
+ expect(server.handleRequest({
+ 'jsonrpc': '2.0',
+ 'method': 'foo',
+ 'params': {'param': 'value'},
+ 'id': 1234
+ }), completion(equals({
+ 'jsonrpc': '2.0',
+ 'result': {'params': {'param': 'value'}},
+ 'id': 1234
+ })));
+ });
+
+ test("calls a method that takes no parameters", () {
+ server.registerMethod('foo', () => 'foo');
+
+ expect(server.handleRequest({
+ 'jsonrpc': '2.0',
+ 'method': 'foo',
+ 'id': 1234
+ }), completion(equals({
+ 'jsonrpc': '2.0',
+ 'result': 'foo',
+ 'id': 1234
+ })));
+ });
+
+ test("a method that takes no parameters rejects parameters", () {
+ server.registerMethod('foo', () => 'foo');
+
+ expectErrorResponse(server, {
+ 'jsonrpc': '2.0',
+ 'method': 'foo',
+ 'params': {},
+ 'id': 1234
+ },
+ error_code.INVALID_PARAMS,
+ 'No parameters are allowed for method "foo".');
+ });
+
+ test("an unexpected error in a method is captured", () {
+ server.registerMethod('foo', () => throw new FormatException('bad format'));
+
+ expect(server.handleRequest({
+ 'jsonrpc': '2.0',
+ 'method': 'foo',
+ 'id': 1234
+ }), completion({
+ 'jsonrpc': '2.0',
+ 'id': 1234,
+ 'error': {
+ 'code': error_code.SERVER_ERROR,
+ 'message': 'bad format',
+ 'data': {
+ 'request': {'jsonrpc': '2.0', 'method': 'foo', 'id': 1234},
+ 'full': 'FormatException: bad format',
+ 'stack': contains('server_test.dart')
+ }
+ }
+ }));
+ });
+
+ test("doesn't return a result for a notification", () {
+ server.registerMethod('foo', (args) => 'result');
+
+ expect(server.handleRequest({
+ 'jsonrpc': '2.0',
+ 'method': 'foo',
+ 'params': {}
+ }), completion(isNull));
+ });
+
+ group("JSON", () {
+ test("handles a request parsed from JSON", () {
+ server.registerMethod('foo', (params) {
+ return {'params': params.value};
+ });
+
+ expect(server.parseRequest(JSON.encode({
+ 'jsonrpc': '2.0',
+ 'method': 'foo',
+ 'params': {'param': 'value'},
+ 'id': 1234
+ })), completion(equals(JSON.encode({
+ 'jsonrpc': '2.0',
+ 'result': {'params': {'param': 'value'}},
+ 'id': 1234
+ }))));
+ });
+
+ test("handles a notification parsed from JSON", () {
+ server.registerMethod('foo', (params) {
+ return {'params': params};
+ });
+
+ expect(server.parseRequest(JSON.encode({
+ 'jsonrpc': '2.0',
+ 'method': 'foo',
+ 'params': {'param': 'value'}
+ })), completion(isNull));
+ });
+
+ test("a JSON parse error is rejected", () {
+ expect(server.parseRequest('invalid json {'),
+ completion(equals(JSON.encode({
+ 'jsonrpc': '2.0',
+ 'error': {
+ 'code': error_code.PARSE_ERROR,
+ 'message': "Invalid JSON: Unexpected character at 0: 'invalid json "
+ "{'",
+ 'data': {'request': 'invalid json {'}
+ },
+ 'id': null
+ }))));
+ });
+ });
+
+ group("fallbacks", () {
+ test("calls a fallback if no method matches", () {
+ server.registerMethod('foo', () => 'foo');
+ server.registerMethod('bar', () => 'foo');
+ server.registerFallback((params) => {'fallback': params.value});
+
+ expect(server.handleRequest({
+ 'jsonrpc': '2.0',
+ 'method': 'baz',
+ 'params': {'param': 'value'},
+ 'id': 1234
+ }), completion(equals({
+ 'jsonrpc': '2.0',
+ 'result': {'fallback': {'param': 'value'}},
+ 'id': 1234
+ })));
+ });
+
+ test("calls the first matching fallback", () {
+ server.registerFallback((params) =>
+ throw new json_rpc.RpcException.methodNotFound(params.method));
+
+ server.registerFallback((params) => 'fallback 2');
+ server.registerFallback((params) => 'fallback 3');
+
+ expect(server.handleRequest({
+ 'jsonrpc': '2.0',
+ 'method': 'fallback 2',
+ 'id': 1234
+ }), completion(equals({
+ 'jsonrpc': '2.0',
+ 'result': 'fallback 2',
+ 'id': 1234
+ })));
+ });
+
+ test("an unexpected error in a fallback is captured", () {
+ server.registerFallback((_) => throw new FormatException('bad format'));
+
+ expect(server.handleRequest({
+ 'jsonrpc': '2.0',
+ 'method': 'foo',
+ 'id': 1234
+ }), completion({
+ 'jsonrpc': '2.0',
+ 'id': 1234,
+ 'error': {
+ 'code': error_code.SERVER_ERROR,
+ 'message': 'bad format',
+ 'data': {
+ 'request': {'jsonrpc': '2.0', 'method': 'foo', 'id': 1234},
+ 'full': 'FormatException: bad format',
+ 'stack': contains('server_test.dart')
+ }
+ }
+ }));
+ });
+ });
+
+ test("disallows multiple methods with the same name", () {
+ server.registerMethod('foo', () => null);
+ expect(() => server.registerMethod('foo', () => null), throwsArgumentError);
+ });
+}
« no previous file with comments | « pkg/json_rpc_2/test/server/parameters_test.dart ('k') | pkg/json_rpc_2/test/server/utils.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698