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

Side by Side 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: 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 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 server;
17 setUp(() => server = new json_rpc.Server());
18
19 test("calls a registered method with the given name", () {
20 server.registerMethod('foo', (params) {
21 return {'params': params.value};
22 });
23
24 expect(server.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 server.registerMethod('foo', () => 'foo');
38
39 expect(server.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 server.registerMethod('foo', () => 'foo');
52
53 expectErrorResponse(server, {
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 server.registerMethod('foo', () => throw new FormatException('bad format'));
65
66 expect(server.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': contains('server_test.dart')
80 }
81 }
82 }));
83 });
84
85 test("doesn't return a result for a notification", () {
86 server.registerMethod('foo', (args) => 'result');
87
88 expect(server.handleRequest({
89 'jsonrpc': '2.0',
90 'method': 'foo',
91 'params': {}
92 }), completion(isNull));
93 });
94
95 group("JSON", () {
96 test("handles a request parsed from JSON", () {
97 server.registerMethod('foo', (params) {
98 return {'params': params.value};
99 });
100
101 expect(server.parseRequest(JSON.encode({
102 'jsonrpc': '2.0',
103 'method': 'foo',
104 'params': {'param': 'value'},
105 'id': 1234
106 })), completion(equals(JSON.encode({
107 'jsonrpc': '2.0',
108 'result': {'params': {'param': 'value'}},
109 'id': 1234
110 }))));
111 });
112
113 test("handles a notification parsed from JSON", () {
114 server.registerMethod('foo', (params) {
115 return {'params': params};
116 });
117
118 expect(server.parseRequest(JSON.encode({
119 'jsonrpc': '2.0',
120 'method': 'foo',
121 'params': {'param': 'value'}
122 })), completion(isNull));
123 });
124
125 test("a JSON parse error is rejected", () {
126 expect(server.parseRequest('invalid json {'),
127 completion(equals(JSON.encode({
128 'jsonrpc': '2.0',
129 'error': {
130 'code': error_code.PARSE_ERROR,
131 'message': "Invalid JSON: Unexpected character at 0: 'invalid json "
132 "{'",
133 'data': {'request': 'invalid json {'}
134 },
135 'id': null
136 }))));
137 });
138 });
139
140 group("fallbacks", () {
141 test("calls a fallback if no method matches", () {
142 server.registerMethod('foo', () => 'foo');
143 server.registerMethod('bar', () => 'foo');
144 server.registerFallback((params) => {'fallback': params.value});
145
146 expect(server.handleRequest({
147 'jsonrpc': '2.0',
148 'method': 'baz',
149 'params': {'param': 'value'},
150 'id': 1234
151 }), completion(equals({
152 'jsonrpc': '2.0',
153 'result': {'fallback': {'param': 'value'}},
154 'id': 1234
155 })));
156 });
157
158 test("calls the first matching fallback", () {
159 server.registerFallback((_) => throw 'bad',
160 test: (name) => name.endsWith('1'));
161
162 server.registerFallback((_) => 'fallback 2',
163 test: (name) => name.endsWith('2'));
164
165 server.registerFallback((_) => throw 'bad',
166 test: (name) => name.startsWith('fallback'));
167
168 expect(server.handleRequest({
169 'jsonrpc': '2.0',
170 'method': 'fallback 2',
171 'id': 1234
172 }), completion(equals({
173 'jsonrpc': '2.0',
174 'result': 'fallback 2',
175 'id': 1234
176 })));
177 });
178
179 test("an unexpected error in a fallback is captured", () {
180 server.registerFallback((_) => throw new FormatException('bad format'));
181
182 expect(server.handleRequest({
183 'jsonrpc': '2.0',
184 'method': 'foo',
185 'id': 1234
186 }), completion({
187 'jsonrpc': '2.0',
188 'id': 1234,
189 'error': {
190 'code': error_code.SERVER_ERROR,
191 'message': 'bad format',
192 'data': {
193 'request': {'jsonrpc': '2.0', 'method': 'foo', 'id': 1234},
194 'full': 'FormatException: bad format',
195 'stack': contains('server_test.dart')
196 }
197 }
198 }));
199 });
200 });
201
202 test("disallows multiple methods with the same name", () {
203 server.registerMethod('foo', () => null);
204 expect(() => server.registerMethod('foo', () => null), throwsArgumentError);
205 });
206 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698