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

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

Issue 309503005: Convert json_rpc.Server to take a Stream and StreamSink. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: code review Created 6 years, 6 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
1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file 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 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 library json_rpc_2.test.server.batch_test; 5 library json_rpc_2.test.server.batch_test;
6 6
7 import 'dart:convert'; 7 import 'dart:convert';
8 8
9 import 'package:unittest/unittest.dart'; 9 import 'package:unittest/unittest.dart';
10 import 'package:json_rpc_2/error_code.dart' as error_code; 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; 11 import 'package:json_rpc_2/json_rpc_2.dart' as json_rpc;
12 12
13 import 'utils.dart'; 13 import 'utils.dart';
14 14
15 void main() { 15 void main() {
16 var server; 16 var controller;
17 setUp(() { 17 setUp(() {
18 server = new json_rpc.Server() 18 controller = new ServerController();
19 controller.server
19 ..registerMethod('foo', () => 'foo') 20 ..registerMethod('foo', () => 'foo')
20 ..registerMethod('id', (params) => params.value) 21 ..registerMethod('id', (params) => params.value)
21 ..registerMethod('arg', (params) => params['arg'].value); 22 ..registerMethod('arg', (params) => params['arg'].value);
22 }); 23 });
23 24
24 test('handles a batch of requests', () { 25 test('handles a batch of requests', () {
25 expect(server.handleRequest([ 26 expect(controller.handleRequest([
26 {'jsonrpc': '2.0', 'method': 'foo', 'id': 1}, 27 {'jsonrpc': '2.0', 'method': 'foo', 'id': 1},
27 {'jsonrpc': '2.0', 'method': 'id', 'params': ['value'], 'id': 2}, 28 {'jsonrpc': '2.0', 'method': 'id', 'params': ['value'], 'id': 2},
28 {'jsonrpc': '2.0', 'method': 'arg', 'params': {'arg': 'value'}, 'id': 3} 29 {'jsonrpc': '2.0', 'method': 'arg', 'params': {'arg': 'value'}, 'id': 3}
29 ]), completion(equals([ 30 ]), completion(equals([
30 {'jsonrpc': '2.0', 'result': 'foo', 'id': 1}, 31 {'jsonrpc': '2.0', 'result': 'foo', 'id': 1},
31 {'jsonrpc': '2.0', 'result': ['value'], 'id': 2}, 32 {'jsonrpc': '2.0', 'result': ['value'], 'id': 2},
32 {'jsonrpc': '2.0', 'result': 'value', 'id': 3} 33 {'jsonrpc': '2.0', 'result': 'value', 'id': 3}
33 ]))); 34 ])));
34 }); 35 });
35 36
36 test('handles errors individually', () { 37 test('handles errors individually', () {
37 expect(server.handleRequest([ 38 expect(controller.handleRequest([
38 {'jsonrpc': '2.0', 'method': 'foo', 'id': 1}, 39 {'jsonrpc': '2.0', 'method': 'foo', 'id': 1},
39 {'jsonrpc': '2.0', 'method': 'zap', 'id': 2}, 40 {'jsonrpc': '2.0', 'method': 'zap', 'id': 2},
40 {'jsonrpc': '2.0', 'method': 'arg', 'params': {'arg': 'value'}, 'id': 3} 41 {'jsonrpc': '2.0', 'method': 'arg', 'params': {'arg': 'value'}, 'id': 3}
41 ]), completion(equals([ 42 ]), completion(equals([
42 {'jsonrpc': '2.0', 'result': 'foo', 'id': 1}, 43 {'jsonrpc': '2.0', 'result': 'foo', 'id': 1},
43 { 44 {
44 'jsonrpc': '2.0', 45 'jsonrpc': '2.0',
45 'id': 2, 46 'id': 2,
46 'error': { 47 'error': {
47 'code': error_code.METHOD_NOT_FOUND, 48 'code': error_code.METHOD_NOT_FOUND,
48 'message': 'Unknown method "zap".', 49 'message': 'Unknown method "zap".',
49 'data': {'request': {'jsonrpc': '2.0', 'method': 'zap', 'id': 2}}, 50 'data': {'request': {'jsonrpc': '2.0', 'method': 'zap', 'id': 2}},
50 } 51 }
51 }, 52 },
52 {'jsonrpc': '2.0', 'result': 'value', 'id': 3} 53 {'jsonrpc': '2.0', 'result': 'value', 'id': 3}
53 ]))); 54 ])));
54 }); 55 });
55 56
56 test('handles notifications individually', () { 57 test('handles notifications individually', () {
57 expect(server.handleRequest([ 58 expect(controller.handleRequest([
58 {'jsonrpc': '2.0', 'method': 'foo', 'id': 1}, 59 {'jsonrpc': '2.0', 'method': 'foo', 'id': 1},
59 {'jsonrpc': '2.0', 'method': 'id', 'params': ['value']}, 60 {'jsonrpc': '2.0', 'method': 'id', 'params': ['value']},
60 {'jsonrpc': '2.0', 'method': 'arg', 'params': {'arg': 'value'}, 'id': 3} 61 {'jsonrpc': '2.0', 'method': 'arg', 'params': {'arg': 'value'}, 'id': 3}
61 ]), completion(equals([ 62 ]), completion(equals([
62 {'jsonrpc': '2.0', 'result': 'foo', 'id': 1}, 63 {'jsonrpc': '2.0', 'result': 'foo', 'id': 1},
63 {'jsonrpc': '2.0', 'result': 'value', 'id': 3} 64 {'jsonrpc': '2.0', 'result': 'value', 'id': 3}
64 ]))); 65 ])));
65 }); 66 });
66 67
67 test('returns nothing if every request is a notification', () { 68 test('returns nothing if every request is a notification', () {
68 expect(server.handleRequest([ 69 expect(controller.handleRequest([
69 {'jsonrpc': '2.0', 'method': 'foo'}, 70 {'jsonrpc': '2.0', 'method': 'foo'},
70 {'jsonrpc': '2.0', 'method': 'id', 'params': ['value']}, 71 {'jsonrpc': '2.0', 'method': 'id', 'params': ['value']},
71 {'jsonrpc': '2.0', 'method': 'arg', 'params': {'arg': 'value'}} 72 {'jsonrpc': '2.0', 'method': 'arg', 'params': {'arg': 'value'}}
72 ]), completion(isNull)); 73 ]), completion(isNull));
73 }); 74 });
74 75
75 test('returns an error if the batch is empty', () { 76 test('returns an error if the batch is empty', () {
76 expectErrorResponse(server, [], error_code.INVALID_REQUEST, 77 expectErrorResponse(controller, [], error_code.INVALID_REQUEST,
77 'A batch must contain at least one request.'); 78 'A batch must contain at least one request.');
78 }); 79 });
79 80
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', () { 81 test('disallows nested batches', () {
93 expect(server.handleRequest([ 82 expect(controller.handleRequest([
94 [{'jsonrpc': '2.0', 'method': 'foo', 'id': 1}] 83 [{'jsonrpc': '2.0', 'method': 'foo', 'id': 1}]
95 ]), completion(equals([{ 84 ]), completion(equals([{
96 'jsonrpc': '2.0', 85 'jsonrpc': '2.0',
97 'id': null, 86 'id': null,
98 'error': { 87 'error': {
99 'code': error_code.INVALID_REQUEST, 88 'code': error_code.INVALID_REQUEST,
100 'message': 'Request must be an Array or an Object.', 89 'message': 'Request must be an Array or an Object.',
101 'data': {'request': [{'jsonrpc': '2.0', 'method': 'foo', 'id': 1}]} 90 'data': {'request': [{'jsonrpc': '2.0', 'method': 'foo', 'id': 1}]}
102 } 91 }
103 }]))); 92 }])));
104 }); 93 });
105 } 94 }
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