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

Side by Side Diff: pkg/json_rpc_2/test/server/batch_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 unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « pkg/json_rpc_2/pubspec.yaml ('k') | pkg/json_rpc_2/test/server/invalid_request_test.dart » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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.batch_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(() {
18 server = new json_rpc.Server()
19 ..registerMethod('foo', () => 'foo')
20 ..registerMethod('id', (params) => params.value)
21 ..registerMethod('arg', (params) => params['arg'].value);
22 });
23
24 test('handles a batch of requests', () {
25 expect(server.handleRequest([
26 {'jsonrpc': '2.0', 'method': 'foo', 'id': 1},
27 {'jsonrpc': '2.0', 'method': 'id', 'params': ['value'], 'id': 2},
28 {'jsonrpc': '2.0', 'method': 'arg', 'params': {'arg': 'value'}, 'id': 3}
29 ]), completion(equals([
30 {'jsonrpc': '2.0', 'result': 'foo', 'id': 1},
31 {'jsonrpc': '2.0', 'result': ['value'], 'id': 2},
32 {'jsonrpc': '2.0', 'result': 'value', 'id': 3}
33 ])));
34 });
35
36 test('handles errors individually', () {
37 expect(server.handleRequest([
38 {'jsonrpc': '2.0', 'method': 'foo', 'id': 1},
39 {'jsonrpc': '2.0', 'method': 'zap', 'id': 2},
40 {'jsonrpc': '2.0', 'method': 'arg', 'params': {'arg': 'value'}, 'id': 3}
41 ]), completion(equals([
42 {'jsonrpc': '2.0', 'result': 'foo', 'id': 1},
43 {
44 'jsonrpc': '2.0',
45 'id': 2,
46 'error': {
47 'code': error_code.METHOD_NOT_FOUND,
48 'message': 'Unknown method "zap".',
49 'data': {'request': {'jsonrpc': '2.0', 'method': 'zap', 'id': 2}},
50 }
51 },
52 {'jsonrpc': '2.0', 'result': 'value', 'id': 3}
53 ])));
54 });
55
56 test('handles notifications individually', () {
57 expect(server.handleRequest([
58 {'jsonrpc': '2.0', 'method': 'foo', 'id': 1},
59 {'jsonrpc': '2.0', 'method': 'id', 'params': ['value']},
60 {'jsonrpc': '2.0', 'method': 'arg', 'params': {'arg': 'value'}, 'id': 3}
61 ]), completion(equals([
62 {'jsonrpc': '2.0', 'result': 'foo', 'id': 1},
63 {'jsonrpc': '2.0', 'result': 'value', 'id': 3}
64 ])));
65 });
66
67 test('returns nothing if every request is a notification', () {
68 expect(server.handleRequest([
69 {'jsonrpc': '2.0', 'method': 'foo'},
70 {'jsonrpc': '2.0', 'method': 'id', 'params': ['value']},
71 {'jsonrpc': '2.0', 'method': 'arg', 'params': {'arg': 'value'}}
72 ]), completion(isNull));
73 });
74
75 test('returns an error if the batch is empty', () {
76 expectErrorResponse(server, [], error_code.INVALID_REQUEST,
77 'A batch must contain at least one request.');
78 });
79
80 test('handles a batch of requests parsed from JSON', () {
81 expect(server.parseRequest(JSON.encode([
82 {'jsonrpc': '2.0', 'method': 'foo', 'id': 1},
83 {'jsonrpc': '2.0', 'method': 'id', 'params': ['value'], 'id': 2},
84 {'jsonrpc': '2.0', 'method': 'arg', 'params': {'arg': 'value'}, 'id': 3}
85 ])), completion(equals(JSON.encode([
86 {'jsonrpc': '2.0', 'result': 'foo', 'id': 1},
87 {'jsonrpc': '2.0', 'result': ['value'], 'id': 2},
88 {'jsonrpc': '2.0', 'result': 'value', 'id': 3}
89 ]))));
90 });
91
92 test('disallows nested batches', () {
93 expect(server.handleRequest([
94 [{'jsonrpc': '2.0', 'method': 'foo', 'id': 1}]
95 ]), completion(equals([{
96 'jsonrpc': '2.0',
97 'id': null,
98 'error': {
99 'code': error_code.INVALID_REQUEST,
100 'message': 'Request must be an Array or an Object.',
101 'data': {'request': [{'jsonrpc': '2.0', 'method': 'foo', 'id': 1}]}
102 }
103 }])));
104 });
105 }
OLDNEW
« no previous file with comments | « pkg/json_rpc_2/pubspec.yaml ('k') | pkg/json_rpc_2/test/server/invalid_request_test.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698