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

Side by Side Diff: pkg/json_rpc_2/test/server/server_test.dart

Issue 812253002: Delete a bunch of packages that are now on GitHub. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Un-delete http Created 6 years 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 unified diff | Download patch | Annotate | Revision Log
OLDNEW
(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.server_test;
6
7 import 'dart:convert';
8
9 import 'package:unittest/unittest.dart';
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;
12
13 import 'utils.dart';
14
15 void main() {
16 var controller;
17 setUp(() => controller = new ServerController());
18
19 test("calls a registered method with the given name", () {
20 controller.server.registerMethod('foo', (params) {
21 return {'params': params.value};
22 });
23
24 expect(controller.handleRequest({
25 'jsonrpc': '2.0',
26 'method': 'foo',
27 'params': {'param': 'value'},
28 'id': 1234
29 }), completion(equals({
30 'jsonrpc': '2.0',
31 'result': {'params': {'param': 'value'}},
32 'id': 1234
33 })));
34 });
35
36 test("calls a method that takes no parameters", () {
37 controller.server.registerMethod('foo', () => 'foo');
38
39 expect(controller.handleRequest({
40 'jsonrpc': '2.0',
41 'method': 'foo',
42 'id': 1234
43 }), completion(equals({
44 'jsonrpc': '2.0',
45 'result': 'foo',
46 'id': 1234
47 })));
48 });
49
50 test("a method that takes no parameters rejects parameters", () {
51 controller.server.registerMethod('foo', () => 'foo');
52
53 expectErrorResponse(controller, {
54 'jsonrpc': '2.0',
55 'method': 'foo',
56 'params': {},
57 'id': 1234
58 },
59 error_code.INVALID_PARAMS,
60 'No parameters are allowed for method "foo".');
61 });
62
63 test("an unexpected error in a method is captured", () {
64 controller.server.registerMethod('foo', () => throw new FormatException('bad format'));
65
66 expect(controller.handleRequest({
67 'jsonrpc': '2.0',
68 'method': 'foo',
69 'id': 1234
70 }), completion({
71 'jsonrpc': '2.0',
72 'id': 1234,
73 'error': {
74 'code': error_code.SERVER_ERROR,
75 'message': 'bad format',
76 'data': {
77 'request': {'jsonrpc': '2.0', 'method': 'foo', 'id': 1234},
78 'full': 'FormatException: bad format',
79 'stack': new isInstanceOf<String>()
80 }
81 }
82 }));
83 });
84
85 test("doesn't return a result for a notification", () {
86 controller.server.registerMethod('foo', (args) => 'result');
87
88 expect(controller.handleRequest({
89 'jsonrpc': '2.0',
90 'method': 'foo',
91 'params': {}
92 }), completion(isNull));
93 });
94
95 test("includes the error data in the response", () {
96 controller.server.registerMethod('foo', (params) {
97 throw new json_rpc.RpcException(5, 'Error message.', data: 'data value');
98 });
99
100 expectErrorResponse(controller, {
101 'jsonrpc': '2.0',
102 'method': 'foo',
103 'params': {},
104 'id': 1234
105 },
106 5,
107 'Error message.',
108 data: 'data value');
109 });
110
111 test("a JSON parse error is rejected", () {
112 return controller.handleJsonRequest('invalid json {').then((result) {
113 expect(JSON.decode(result), {
114 'jsonrpc': '2.0',
115 'error': {
116 'code': error_code.PARSE_ERROR,
117 'message': startsWith("Invalid JSON: "),
118 'data': {'request': 'invalid json {'}
119 },
120 'id': null
121 });
122 });
123 });
124
125 group("fallbacks", () {
126 test("calls a fallback if no method matches", () {
127 controller.server
128 ..registerMethod('foo', () => 'foo')
129 ..registerMethod('bar', () => 'foo')
130 ..registerFallback((params) => {'fallback': params.value});
131
132 expect(controller.handleRequest({
133 'jsonrpc': '2.0',
134 'method': 'baz',
135 'params': {'param': 'value'},
136 'id': 1234
137 }), completion(equals({
138 'jsonrpc': '2.0',
139 'result': {'fallback': {'param': 'value'}},
140 'id': 1234
141 })));
142 });
143
144 test("calls the first matching fallback", () {
145 controller.server
146 ..registerFallback((params) =>
147 throw new json_rpc.RpcException.methodNotFound(params.method))
148 ..registerFallback((params) => 'fallback 2')
149 ..registerFallback((params) => 'fallback 3');
150
151 expect(controller.handleRequest({
152 'jsonrpc': '2.0',
153 'method': 'fallback 2',
154 'id': 1234
155 }), completion(equals({
156 'jsonrpc': '2.0',
157 'result': 'fallback 2',
158 'id': 1234
159 })));
160 });
161
162 test("an unexpected error in a fallback is captured", () {
163 controller.server.registerFallback((_) =>
164 throw new FormatException('bad format'));
165
166 expect(controller.handleRequest({
167 'jsonrpc': '2.0',
168 'method': 'foo',
169 'id': 1234
170 }), completion({
171 'jsonrpc': '2.0',
172 'id': 1234,
173 'error': {
174 'code': error_code.SERVER_ERROR,
175 'message': 'bad format',
176 'data': {
177 'request': {'jsonrpc': '2.0', 'method': 'foo', 'id': 1234},
178 'full': 'FormatException: bad format',
179 'stack': new isInstanceOf<String>()
180 }
181 }
182 }));
183 });
184 });
185
186 test("disallows multiple methods with the same name", () {
187 controller.server.registerMethod('foo', () => null);
188 expect(() => controller.server.registerMethod('foo', () => null),
189 throwsArgumentError);
190 });
191 }
OLDNEW
« no previous file with comments | « pkg/json_rpc_2/test/server/parameters_test.dart ('k') | pkg/json_rpc_2/test/server/stream_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698