| OLD | NEW |
| 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 test.channel; | 5 library test.channel; |
| 6 | 6 |
| 7 import 'dart:async'; | 7 import 'dart:async'; |
| 8 import 'dart:convert'; |
| 9 import 'dart:io'; |
| 8 | 10 |
| 9 import 'package:analysis_server/src/channel.dart'; | 11 import 'package:analysis_server/src/channel.dart'; |
| 10 import 'package:analysis_server/src/protocol.dart'; | 12 import 'package:analysis_server/src/protocol.dart'; |
| 11 import 'package:unittest/unittest.dart'; | 13 import 'package:unittest/unittest.dart'; |
| 12 | 14 |
| 13 import 'mocks.dart'; | 15 import 'mocks.dart'; |
| 14 | 16 |
| 15 main() { | 17 main() { |
| 16 group('WebSocketChannel', () { | 18 group('WebSocketChannel', () { |
| 17 setUp(WebSocketChannelTest.setUp); | 19 setUp(WebSocketChannelTest.setUp); |
| 18 test('close', WebSocketChannelTest.close); | 20 test('close', WebSocketChannelTest.close); |
| 19 test('invalidJsonToClient', WebSocketChannelTest.invalidJsonToClient); | 21 test('invalidJsonToClient', WebSocketChannelTest.invalidJsonToClient); |
| 20 test('invalidJsonToServer', WebSocketChannelTest.invalidJsonToServer); | 22 test('invalidJsonToServer', WebSocketChannelTest.invalidJsonToServer); |
| 21 test('notification', WebSocketChannelTest.notification); | 23 test('notification', WebSocketChannelTest.notification); |
| 22 test('notificationAndResponse', WebSocketChannelTest.notificationAndResponse
); | 24 test('notificationAndResponse', WebSocketChannelTest.notificationAndResponse
); |
| 23 test('request', WebSocketChannelTest.request); | 25 test('request', WebSocketChannelTest.request); |
| 24 test('requestResponse', WebSocketChannelTest.requestResponse); | 26 test('requestResponse', WebSocketChannelTest.requestResponse); |
| 25 test('response', WebSocketChannelTest.response); | 27 test('response', WebSocketChannelTest.response); |
| 26 }); | 28 }); |
| 29 group('ByteStreamServerChannel', () { |
| 30 setUp(ByteStreamServerChannelTest.setUp); |
| 31 test('listen_wellFormedRequest', |
| 32 ByteStreamServerChannelTest.listen_wellFormedRequest); |
| 33 test('listen_invalidRequest', |
| 34 ByteStreamServerChannelTest.listen_invalidRequest); |
| 35 test('listen_invalidJson', ByteStreamServerChannelTest.listen_invalidJson); |
| 36 test('listen_streamError', ByteStreamServerChannelTest.listen_streamError); |
| 37 test('listen_streamDone', ByteStreamServerChannelTest.listen_streamDone); |
| 38 test('sendNotification', ByteStreamServerChannelTest.sendNotification); |
| 39 test('sendResponse', ByteStreamServerChannelTest.sendResponse); |
| 40 }); |
| 27 } | 41 } |
| 28 | 42 |
| 29 class WebSocketChannelTest { | 43 class WebSocketChannelTest { |
| 30 static MockSocket socket; | 44 static MockSocket socket; |
| 31 static WebSocketClientChannel client; | 45 static WebSocketClientChannel client; |
| 32 static WebSocketServerChannel server; | 46 static WebSocketServerChannel server; |
| 33 | 47 |
| 34 static List requestsReceived; | 48 static List requestsReceived; |
| 35 static List responsesReceived; | 49 static List responsesReceived; |
| 36 static List notificationsReceived; | 50 static List notificationsReceived; |
| (...skipping 112 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 149 }); | 163 }); |
| 150 } | 164 } |
| 151 | 165 |
| 152 static void expectMsgCount({requestCount: 0, | 166 static void expectMsgCount({requestCount: 0, |
| 153 responseCount: 0, | 167 responseCount: 0, |
| 154 notificationCount: 0}) { | 168 notificationCount: 0}) { |
| 155 expect(requestsReceived, hasLength(requestCount)); | 169 expect(requestsReceived, hasLength(requestCount)); |
| 156 expect(responsesReceived, hasLength(responseCount)); | 170 expect(responsesReceived, hasLength(responseCount)); |
| 157 expect(notificationsReceived, hasLength(notificationCount)); | 171 expect(notificationsReceived, hasLength(notificationCount)); |
| 158 } | 172 } |
| 159 } | 173 } |
| 174 |
| 175 class ByteStreamServerChannelTest { |
| 176 static ByteStreamServerChannel channel; |
| 177 |
| 178 /** |
| 179 * Sink that may be used to deliver data to the channel, as though it's |
| 180 * coming from the client. |
| 181 */ |
| 182 static IOSink inputSink; |
| 183 |
| 184 /** |
| 185 * Stream of lines sent back to the client by the channel. |
| 186 */ |
| 187 static Stream<String> outputLineStream; |
| 188 |
| 189 /** |
| 190 * Stream of requests received from the channel via [listen()]. |
| 191 */ |
| 192 static Stream<Request> requestStream; |
| 193 |
| 194 /** |
| 195 * Stream of errors received from the channel via [listen()]. |
| 196 */ |
| 197 static Stream errorStream; |
| 198 |
| 199 /** |
| 200 * Future which is completed when then [listen()] reports [onDone]. |
| 201 */ |
| 202 static Future doneFuture; |
| 203 |
| 204 static void setUp() { |
| 205 StreamController<List<int>> inputStream = new StreamController<List<int>>(); |
| 206 inputSink = new IOSink(inputStream); |
| 207 StreamController<List<int>> outputStream = new StreamController<List<int>>( |
| 208 ); |
| 209 outputLineStream = outputStream.stream.transform((new Utf8Codec()).decoder |
| 210 ).transform(new LineSplitter()); |
| 211 IOSink outputSink = new IOSink(outputStream); |
| 212 channel = new ByteStreamServerChannel(inputStream.stream, outputSink); |
| 213 StreamController<Request> requestStreamController = |
| 214 new StreamController<Request>(); |
| 215 requestStream = requestStreamController.stream; |
| 216 StreamController errorStreamController = new StreamController(); |
| 217 errorStream = errorStreamController.stream; |
| 218 Completer doneCompleter = new Completer(); |
| 219 doneFuture = doneCompleter.future; |
| 220 channel.listen((Request request) { |
| 221 requestStreamController.add(request); |
| 222 }, onError: (error) { |
| 223 errorStreamController.add(error); |
| 224 }, onDone: () { |
| 225 doneCompleter.complete(); |
| 226 }); |
| 227 } |
| 228 |
| 229 static Future listen_wellFormedRequest() { |
| 230 inputSink.writeln('{"id":"0","method":"server.version"}'); |
| 231 return inputSink.flush().then((_) => requestStream.first.timeout( |
| 232 new Duration(seconds: 1))).then((Request request) { |
| 233 expect(request.id, equals("0")); |
| 234 expect(request.method, equals("server.version")); |
| 235 }); |
| 236 } |
| 237 |
| 238 static Future listen_invalidRequest() { |
| 239 inputSink.writeln('{"id":"0"}'); |
| 240 return inputSink.flush().then((_) => outputLineStream.first.timeout( |
| 241 new Duration(seconds: 1))).then((String response) { |
| 242 var jsonResponse = new JsonCodec().decode(response); |
| 243 expect(jsonResponse, isMap); |
| 244 expect(jsonResponse, contains('error')); |
| 245 expect(jsonResponse['error'], isNotNull); |
| 246 }); |
| 247 } |
| 248 |
| 249 static Future listen_invalidJson() { |
| 250 inputSink.writeln('{"id":'); |
| 251 return inputSink.flush().then((_) => outputLineStream.first.timeout( |
| 252 new Duration(seconds: 1))).then((String response) { |
| 253 var jsonResponse = new JsonCodec().decode(response); |
| 254 expect(jsonResponse, isMap); |
| 255 expect(jsonResponse, contains('error')); |
| 256 expect(jsonResponse['error'], isNotNull); |
| 257 }); |
| 258 } |
| 259 |
| 260 static Future listen_streamError() { |
| 261 var error = new Error(); |
| 262 inputSink.addError(error); |
| 263 return inputSink.flush().then((_) => errorStream.first.timeout(new Duration( |
| 264 seconds: 1))).then((var receivedError) { |
| 265 expect(receivedError, same(error)); |
| 266 }); |
| 267 } |
| 268 |
| 269 static Future listen_streamDone() { |
| 270 return inputSink.close().then((_) => doneFuture.timeout(new Duration( |
| 271 seconds: 1))); |
| 272 } |
| 273 |
| 274 static Future sendNotification() { |
| 275 channel.sendNotification(new Notification('foo')); |
| 276 return outputLineStream.first.timeout(new Duration(seconds: 1)).then((String |
| 277 notification) { |
| 278 var jsonNotification = new JsonCodec().decode(notification); |
| 279 expect(jsonNotification, isMap); |
| 280 expect(jsonNotification, contains('event')); |
| 281 expect(jsonNotification['event'], equals('foo')); |
| 282 }); |
| 283 } |
| 284 |
| 285 static Future sendResponse() { |
| 286 channel.sendResponse(new Response('foo')); |
| 287 return outputLineStream.first.timeout(new Duration(seconds: 1)).then((String |
| 288 response) { |
| 289 var jsonResponse = new JsonCodec().decode(response); |
| 290 expect(jsonResponse, isMap); |
| 291 expect(jsonResponse, contains('id')); |
| 292 expect(jsonResponse['id'], equals('foo')); |
| 293 }); |
| 294 } |
| 295 } |
| OLD | NEW |