| 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.web_socket; |
| 6 |
| 7 import 'dart:async'; |
| 8 |
| 9 import 'package:analysis_server/src/channel/web_socket_channel.dart'; |
| 10 import 'package:analysis_server/src/protocol.dart'; |
| 11 import 'package:unittest/unittest.dart'; |
| 12 |
| 13 import '../mocks.dart'; |
| 14 |
| 15 main() { |
| 16 group('WebSocketChannel', () { |
| 17 setUp(WebSocketChannelTest.setUp); |
| 18 test('close', WebSocketChannelTest.close); |
| 19 test('invalidJsonToClient', WebSocketChannelTest.invalidJsonToClient); |
| 20 test('invalidJsonToServer', WebSocketChannelTest.invalidJsonToServer); |
| 21 test('notification', WebSocketChannelTest.notification); |
| 22 test('notificationAndResponse', WebSocketChannelTest.notificationAndResponse
); |
| 23 test('request', WebSocketChannelTest.request); |
| 24 test('requestResponse', WebSocketChannelTest.requestResponse); |
| 25 test('response', WebSocketChannelTest.response); |
| 26 }); |
| 27 } |
| 28 |
| 29 class WebSocketChannelTest { |
| 30 static MockSocket socket; |
| 31 static WebSocketClientChannel client; |
| 32 static WebSocketServerChannel server; |
| 33 |
| 34 static List requestsReceived; |
| 35 static List responsesReceived; |
| 36 static List notificationsReceived; |
| 37 |
| 38 static Future close() { |
| 39 var timeout = new Duration(seconds: 1); |
| 40 var future = client.responseStream.drain().timeout(timeout); |
| 41 client.close(); |
| 42 return future; |
| 43 } |
| 44 |
| 45 static void expectMsgCount({requestCount: 0, |
| 46 responseCount: 0, |
| 47 notificationCount: 0}) { |
| 48 expect(requestsReceived, hasLength(requestCount)); |
| 49 expect(responsesReceived, hasLength(responseCount)); |
| 50 expect(notificationsReceived, hasLength(notificationCount)); |
| 51 } |
| 52 |
| 53 static Future invalidJsonToClient() { |
| 54 var result = client.responseStream |
| 55 .first |
| 56 .timeout(new Duration(seconds: 1)) |
| 57 .then((Response response) { |
| 58 expect(response.id, equals('myId')); |
| 59 expectMsgCount(responseCount: 1); |
| 60 }); |
| 61 socket.twin.add('{"foo":"bar"}'); |
| 62 server.sendResponse(new Response('myId')); |
| 63 return result; |
| 64 } |
| 65 |
| 66 static Future invalidJsonToServer() { |
| 67 var result = client.responseStream |
| 68 .first |
| 69 .timeout(new Duration(seconds: 1)) |
| 70 .then((Response response) { |
| 71 expect(response.id, equals('')); |
| 72 expect(response.error, isNotNull); |
| 73 expectMsgCount(responseCount: 1); |
| 74 }); |
| 75 socket.add('"blat"'); |
| 76 return result; |
| 77 } |
| 78 |
| 79 static Future notification() { |
| 80 var result = client.notificationStream |
| 81 .first |
| 82 .timeout(new Duration(seconds: 1)) |
| 83 .then((Notification notification) { |
| 84 expect(notification.event, equals('myEvent')); |
| 85 expectMsgCount(notificationCount: 1); |
| 86 expect(notificationsReceived.first, equals(notification)); |
| 87 }); |
| 88 server.sendNotification(new Notification('myEvent')); |
| 89 return result; |
| 90 } |
| 91 |
| 92 static Future notificationAndResponse() { |
| 93 var result = Future |
| 94 .wait([ |
| 95 client.notificationStream.first, |
| 96 client.responseStream.first]) |
| 97 .timeout(new Duration(seconds: 1)) |
| 98 .then((_) => expectMsgCount(responseCount: 1, notificationCount: 1)); |
| 99 server |
| 100 ..sendNotification(new Notification('myEvent')) |
| 101 ..sendResponse(new Response('myId')); |
| 102 return result; |
| 103 } |
| 104 |
| 105 static void request() { |
| 106 client.sendRequest(new Request('myId', 'myMth')); |
| 107 server.listen((Request request) { |
| 108 expect(request.id, equals('myId')); |
| 109 expect(request.method, equals('myMth')); |
| 110 expectMsgCount(requestCount: 1); |
| 111 }); |
| 112 } |
| 113 |
| 114 static Future requestResponse() { |
| 115 // Simulate server sending a response by echoing the request. |
| 116 server.listen((Request request) => |
| 117 server.sendResponse(new Response(request.id))); |
| 118 return client.sendRequest(new Request('myId', 'myMth')) |
| 119 .timeout(new Duration(seconds: 1)) |
| 120 .then((Response response) { |
| 121 expect(response.id, equals('myId')); |
| 122 expectMsgCount(requestCount: 1, responseCount: 1); |
| 123 |
| 124 expect(requestsReceived.first is Request, isTrue); |
| 125 Request request = requestsReceived.first; |
| 126 expect(request.id, equals('myId')); |
| 127 expect(request.method, equals('myMth')); |
| 128 expect(responsesReceived.first, equals(response)); |
| 129 }); |
| 130 } |
| 131 |
| 132 static Future response() { |
| 133 server.sendResponse(new Response('myId')); |
| 134 return client.responseStream |
| 135 .first |
| 136 .timeout(new Duration(seconds: 1)) |
| 137 .then((Response response) { |
| 138 expect(response.id, equals('myId')); |
| 139 expectMsgCount(responseCount: 1); |
| 140 }); |
| 141 } |
| 142 |
| 143 static void setUp() { |
| 144 socket = new MockSocket.pair(); |
| 145 client = new WebSocketClientChannel(socket); |
| 146 server = new WebSocketServerChannel(socket.twin); |
| 147 |
| 148 requestsReceived = []; |
| 149 responsesReceived = []; |
| 150 notificationsReceived = []; |
| 151 |
| 152 // Allow multiple listeners on server side for testing. |
| 153 socket.twin.allowMultipleListeners(); |
| 154 |
| 155 server.listen(requestsReceived.add); |
| 156 client.responseStream.listen(responsesReceived.add); |
| 157 client.notificationStream.listen(notificationsReceived.add); |
| 158 } |
| 159 } |
| OLD | NEW |