OLD | NEW |
(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 test.channel; |
| 6 |
| 7 import 'package:analysis_server/src/protocol.dart'; |
| 8 import 'package:unittest/matcher.dart'; |
| 9 import 'package:unittest/unittest.dart'; |
| 10 import 'package:analysis_server/src/channel.dart'; |
| 11 import 'mocks.dart'; |
| 12 |
| 13 main() { |
| 14 group('Channel', () { |
| 15 test('invalidJsonToClient', ChannelTest.invalidJsonToClient); |
| 16 test('invalidJsonToServer', ChannelTest.invalidJsonToServer); |
| 17 test('notification', ChannelTest.notification); |
| 18 test('request', ChannelTest.request); |
| 19 test('response', ChannelTest.response); |
| 20 }); |
| 21 } |
| 22 |
| 23 class ChannelTest { |
| 24 |
| 25 static void invalidJsonToClient() { |
| 26 InvalidJsonMockSocket mockSocket = new InvalidJsonMockSocket(); |
| 27 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); |
| 28 var responsesReceived = new List(); |
| 29 var notificationsReceived = new List(); |
| 30 client.listen((Response response) => responsesReceived.add(response), |
| 31 (Notification notification) => notificationsReceived.add(notification)); |
| 32 |
| 33 mockSocket.addInvalid('"blat"'); |
| 34 mockSocket.addInvalid('{foo:bar}'); |
| 35 |
| 36 expect(responsesReceived.length, equals(0)); |
| 37 expect(notificationsReceived.length, equals(0)); |
| 38 expect(mockSocket.responseCount, equals(0)); |
| 39 } |
| 40 |
| 41 static void invalidJsonToServer() { |
| 42 InvalidJsonMockSocket mockSocket = new InvalidJsonMockSocket(); |
| 43 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); |
| 44 var received = new List(); |
| 45 server.listen((Request request) => received.add(request)); |
| 46 |
| 47 mockSocket.addInvalid('"blat"'); |
| 48 mockSocket.addInvalid('{foo:bar}'); |
| 49 |
| 50 expect(received.length, equals(0)); |
| 51 expect(mockSocket.responseCount, equals(2)); |
| 52 } |
| 53 |
| 54 static void notification() { |
| 55 MockSocket mockSocket = new MockSocket(); |
| 56 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); |
| 57 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); |
| 58 var responsesReceived = new List(); |
| 59 var notificationsReceived = new List(); |
| 60 client.listen((Response response) => responsesReceived.add(response), |
| 61 (Notification notification) => notificationsReceived.add(notification)); |
| 62 |
| 63 server.sendNotification(new Notification('myEvent')); |
| 64 |
| 65 expect(responsesReceived.length, equals(0)); |
| 66 expect(notificationsReceived.length, equals(1)); |
| 67 expect(notificationsReceived.first.runtimeType, equals(Notification)); |
| 68 Notification actual = notificationsReceived.first; |
| 69 expect(actual.event, equals('myEvent')); |
| 70 } |
| 71 |
| 72 static void request() { |
| 73 MockSocket mockSocket = new MockSocket(); |
| 74 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); |
| 75 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); |
| 76 var requestsReceived = new List(); |
| 77 server.listen((Request request) => requestsReceived.add(request)); |
| 78 |
| 79 client.sendRequest(new Request('myId', 'aMethod')); |
| 80 |
| 81 expect(requestsReceived.length, equals(1)); |
| 82 expect(requestsReceived.first.runtimeType, equals(Request)); |
| 83 Request actual = requestsReceived.first; |
| 84 expect(actual.id, equals('myId')); |
| 85 expect(actual.method, equals('aMethod')); |
| 86 } |
| 87 |
| 88 static void response() { |
| 89 MockSocket mockSocket = new MockSocket(); |
| 90 WebSocketClientChannel client = new WebSocketClientChannel(mockSocket); |
| 91 WebSocketServerChannel server = new WebSocketServerChannel(mockSocket); |
| 92 var responsesReceived = new List(); |
| 93 var notificationsReceived = new List(); |
| 94 client.listen((Response response) => responsesReceived.add(response), |
| 95 (Notification notification) => notificationsReceived.add(notification)); |
| 96 |
| 97 server.sendResponse(new Response('myId')); |
| 98 |
| 99 expect(responsesReceived.length, equals(1)); |
| 100 expect(notificationsReceived.length, equals(0)); |
| 101 expect(responsesReceived.first.runtimeType, equals(Response)); |
| 102 Response actual = responsesReceived.first; |
| 103 expect(actual.id, equals('myId')); |
| 104 } |
| 105 } |
OLD | NEW |