| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 | 5 |
| 6 // TODO(7157): Remove this test once the bug is fixed. | 6 // TODO(7157): Remove this test once the bug is fixed. |
| 7 // This is a copy of web_socket_test.dart with the secure connection | 7 // This is a copy of web_socket_test.dart with the secure connection |
| 8 // tests disabled, so it does not crash on Windows. | 8 // tests disabled, so it does not crash on Windows. |
| 9 import "dart:async"; | 9 import "dart:async"; |
| 10 import "dart:io"; | 10 import "dart:io"; |
| 11 import "dart:isolate"; | 11 import "dart:isolate"; |
| 12 import "dart:scalarlist"; | 12 import "dart:scalarlist"; |
| 13 import "dart:uri"; | 13 import "dart:uri"; |
| 14 | 14 |
| 15 const SERVER_ADDRESS = "127.0.0.1"; | 15 const SERVER_ADDRESS = "127.0.0.1"; |
| 16 const HOST_NAME = "localhost"; | 16 const HOST_NAME = "localhost"; |
| 17 | 17 |
| 18 // We will run the tests once over HTTP and once over HTTPS. | |
| 19 bool secure = false; | |
| 20 | |
| 21 Future testRequestResponseClientCloses( | |
| 22 int totalConnections, int closeStatus, String closeReason) { | |
| 23 Completer done = new Completer(); | |
| 24 HttpServer server = secure ? new HttpsServer() : new HttpServer(); | |
| 25 HttpClient client = new HttpClient(); | |
| 26 | |
| 27 server.listen(SERVER_ADDRESS, | |
| 28 0, | |
| 29 backlog: totalConnections, | |
| 30 certificate_name: "CN=$HOST_NAME"); | |
| 31 | |
| 32 // Create a web socket handler and set it as the HTTP server default handler. | |
| 33 WebSocketHandler wsHandler = new WebSocketHandler(); | |
| 34 wsHandler.onOpen = (WebSocketConnection conn) { | |
| 35 var count = 0; | |
| 36 conn.onMessage = (Object message) => conn.send(message); | |
| 37 conn.onClosed = (status, reason) { | |
| 38 Expect.equals(closeStatus == null | |
| 39 ? WebSocketStatus.NO_STATUS_RECEIVED | |
| 40 : closeStatus, status); | |
| 41 Expect.equals(closeReason == null ? "" : closeReason, reason); | |
| 42 }; | |
| 43 }; | |
| 44 server.defaultRequestHandler = wsHandler.onRequest; | |
| 45 | |
| 46 int closeCount = 0; | |
| 47 String messageText = "Hello, world!"; | |
| 48 for (int i = 0; i < totalConnections; i++) { | |
| 49 int messageCount = 0; | |
| 50 HttpClientConnection conn = client.getUrl(new Uri.fromString( | |
| 51 '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/')); | |
| 52 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | |
| 53 wsconn.onOpen = () => wsconn.send(messageText); | |
| 54 wsconn.onMessage = (message) { | |
| 55 messageCount++; | |
| 56 if (messageCount < 10) { | |
| 57 Expect.equals(messageText, message); | |
| 58 wsconn.send(message); | |
| 59 } else { | |
| 60 wsconn.close(closeStatus, closeReason); | |
| 61 } | |
| 62 }; | |
| 63 wsconn.onClosed = (status, reason) { | |
| 64 Expect.equals(closeStatus == null | |
| 65 ? WebSocketStatus.NO_STATUS_RECEIVED | |
| 66 : closeStatus, status); | |
| 67 Expect.equals("", reason); | |
| 68 closeCount++; | |
| 69 if (closeCount == totalConnections) { | |
| 70 client.shutdown(); | |
| 71 server.close(); | |
| 72 done.complete(null); | |
| 73 } | |
| 74 }; | |
| 75 } | |
| 76 return done.future; | |
| 77 } | |
| 78 | |
| 79 | |
| 80 Future testRequestResponseServerCloses( | |
| 81 int totalConnections, int closeStatus, String closeReason) { | |
| 82 Completer done = new Completer(); | |
| 83 HttpServer server = secure ? new HttpsServer() : new HttpServer(); | |
| 84 HttpClient client = new HttpClient(); | |
| 85 | |
| 86 server.listen(SERVER_ADDRESS, | |
| 87 0, | |
| 88 backlog: totalConnections, | |
| 89 certificate_name: "CN=$HOST_NAME"); | |
| 90 | |
| 91 // Create a web socket handler and set is as the HTTP server default | |
| 92 // handler. | |
| 93 int closeCount = 0; | |
| 94 WebSocketHandler wsHandler = new WebSocketHandler(); | |
| 95 wsHandler.onOpen = (WebSocketConnection conn) { | |
| 96 String messageText = "Hello, world!"; | |
| 97 int messageCount = 0; | |
| 98 conn.onMessage = (Object message) { | |
| 99 messageCount++; | |
| 100 if (messageCount < 10) { | |
| 101 Expect.equals(messageText, message); | |
| 102 conn.send(message); | |
| 103 } else { | |
| 104 conn.close(closeStatus, closeReason); | |
| 105 } | |
| 106 }; | |
| 107 conn.onClosed = (status, reason) { | |
| 108 Expect.equals(closeStatus == null | |
| 109 ? WebSocketStatus.NO_STATUS_RECEIVED | |
| 110 : closeStatus, status); | |
| 111 Expect.equals("", reason); | |
| 112 closeCount++; | |
| 113 if (closeCount == totalConnections) { | |
| 114 client.shutdown(); | |
| 115 server.close(); | |
| 116 done.complete(null); | |
| 117 } | |
| 118 }; | |
| 119 conn.send(messageText); | |
| 120 }; | |
| 121 server.defaultRequestHandler = wsHandler.onRequest; | |
| 122 | |
| 123 for (int i = 0; i < totalConnections; i++) { | |
| 124 HttpClientConnection conn = client.getUrl(new Uri.fromString( | |
| 125 '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/')); | |
| 126 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | |
| 127 wsconn.onMessage = (message) => wsconn.send(message); | |
| 128 wsconn.onClosed = (status, reason) { | |
| 129 Expect.equals(closeStatus == null | |
| 130 ? WebSocketStatus.NO_STATUS_RECEIVED | |
| 131 : closeStatus, status); | |
| 132 Expect.equals(closeReason == null ? "" : closeReason, reason); | |
| 133 }; | |
| 134 } | |
| 135 return done.future; | |
| 136 } | |
| 137 | |
| 138 | |
| 139 Future testMessageLength(int messageLength) { | |
| 140 Completer done = new Completer(); | |
| 141 HttpServer server = secure ? new HttpsServer() : new HttpServer(); | |
| 142 HttpClient client = new HttpClient(); | |
| 143 bool serverReceivedMessage = false; | |
| 144 bool clientReceivedMessage = false; | |
| 145 | |
| 146 server.listen(SERVER_ADDRESS, | |
| 147 0, | |
| 148 backlog: 1, | |
| 149 certificate_name: "CN=$HOST_NAME"); | |
| 150 | |
| 151 // Create a web socket handler and set is as the HTTP server default | |
| 152 // handler. | |
| 153 Uint8List originalMessage = new Uint8List(messageLength); | |
| 154 WebSocketHandler wsHandler = new WebSocketHandler(); | |
| 155 wsHandler.onOpen = (WebSocketConnection conn) { | |
| 156 conn.onMessage = (Object message) { | |
| 157 serverReceivedMessage = true; | |
| 158 Expect.listEquals(originalMessage, message); | |
| 159 conn.send(message); | |
| 160 }; | |
| 161 conn.onClosed = (status, reason) { | |
| 162 }; | |
| 163 }; | |
| 164 server.defaultRequestHandler = wsHandler.onRequest; | |
| 165 | |
| 166 HttpClientConnection conn = client.getUrl(new Uri.fromString( | |
| 167 '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/')); | |
| 168 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | |
| 169 wsconn.onMessage = (message) { | |
| 170 clientReceivedMessage = true; | |
| 171 Expect.listEquals(originalMessage, message); | |
| 172 wsconn.close(); | |
| 173 }; | |
| 174 wsconn.onClosed = (status, reason) { | |
| 175 Expect.isTrue(serverReceivedMessage); | |
| 176 Expect.isTrue(clientReceivedMessage); | |
| 177 client.shutdown(); | |
| 178 server.close(); | |
| 179 done.complete(null); | |
| 180 }; | |
| 181 wsconn.onOpen = () { | |
| 182 wsconn.send(originalMessage); | |
| 183 }; | |
| 184 return done.future; | |
| 185 } | |
| 186 | |
| 187 | |
| 188 Future testNoUpgrade() { | |
| 189 Completer done = new Completer(); | |
| 190 HttpServer server = secure ? new HttpsServer() : new HttpServer(); | |
| 191 HttpClient client = new HttpClient(); | |
| 192 | |
| 193 server.listen(SERVER_ADDRESS, | |
| 194 0, | |
| 195 backlog: 5, | |
| 196 certificate_name: "CN=$HOST_NAME"); | |
| 197 | |
| 198 // Create a server which always responds with a redirect. | |
| 199 server.defaultRequestHandler = (request, response) { | |
| 200 response.statusCode = HttpStatus.MOVED_PERMANENTLY; | |
| 201 response.outputStream.close(); | |
| 202 }; | |
| 203 | |
| 204 HttpClientConnection conn = client.getUrl(new Uri.fromString( | |
| 205 '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/')); | |
| 206 conn.followRedirects = false; | |
| 207 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | |
| 208 wsconn.onNoUpgrade = (response) { | |
| 209 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); | |
| 210 client.shutdown(); | |
| 211 server.close(); | |
| 212 done.complete(null); | |
| 213 }; | |
| 214 return done.future; | |
| 215 } | |
| 216 | |
| 217 | |
| 218 Future testUsePOST() { | |
| 219 Completer done = new Completer(); | |
| 220 HttpServer server = secure ? new HttpsServer() : new HttpServer(); | |
| 221 HttpClient client = new HttpClient(); | |
| 222 | |
| 223 server.listen(SERVER_ADDRESS, | |
| 224 0, | |
| 225 backlog: 5, | |
| 226 certificate_name: "CN=$HOST_NAME"); | |
| 227 | |
| 228 // Create a web socket handler and set is as the HTTP server default | |
| 229 // handler. | |
| 230 int closeCount = 0; | |
| 231 WebSocketHandler wsHandler = new WebSocketHandler(); | |
| 232 wsHandler.onOpen = (WebSocketConnection conn) { | |
| 233 Expect.fail("No connection expected"); | |
| 234 }; | |
| 235 server.defaultRequestHandler = wsHandler.onRequest; | |
| 236 | |
| 237 HttpClientConnection conn = client.postUrl(new Uri.fromString( | |
| 238 '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/')); | |
| 239 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | |
| 240 wsconn.onNoUpgrade = (response) { | |
| 241 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); | |
| 242 client.shutdown(); | |
| 243 server.close(); | |
| 244 done.complete(null); | |
| 245 }; | |
| 246 return done.future; | |
| 247 } | |
| 248 | |
| 249 | 18 |
| 250 class WebSocketInfo { | 19 class WebSocketInfo { |
| 251 int messageCount = 0; | 20 int messageCount = 0; |
| 252 } | 21 } |
| 253 | 22 |
| 254 | 23 |
| 255 Future testHashCode(int totalConnections) { | 24 /** |
| 256 Completer done = new Completer(); | 25 * A SecurityConfiguration lets us run the tests over HTTP or HTTPS. |
| 257 HttpServer server = secure ? new HttpsServer() : new HttpServer(); | 26 */ |
| 258 HttpClient client = new HttpClient(); | 27 class SecurityConfiguration { |
| 259 Map connections = new Map(); | 28 final bool secure; |
| 260 | 29 HttpClient client; |
| 261 server.listen(SERVER_ADDRESS, | 30 |
| 262 0, | 31 SecurityConfiguration({bool this.secure}) : client = new HttpClient(); |
| 263 backlog: totalConnections, | 32 |
| 264 certificate_name: "CN=$HOST_NAME"); | 33 HttpServer createServer({int backlog}) { |
| 265 | 34 HttpServer server = secure ? new HttpsServer() : new HttpServer(); |
| 266 void handleMessage(conn, message) { | 35 server.listen(SERVER_ADDRESS, |
| 267 var info = connections[conn]; | 36 0, |
| 268 Expect.isNotNull(info); | 37 backlog: backlog, |
| 269 info.messageCount++; | 38 certificate_name: "CN=$HOST_NAME"); |
| 270 if (info.messageCount < 10) { | 39 return server; |
| 271 conn.send(message); | 40 } |
| 272 } else { | 41 |
| 273 conn.close(); | 42 WebSocketClientConnection createClient(int port, |
| 274 } | 43 {bool followRedirects, |
| 275 } | 44 String method: "GET"}) { |
| 276 | 45 HttpClientConnection conn = client.openUrl(method, new Uri.fromString( |
| 277 // Create a web socket handler and set is as the HTTP server default | 46 '${secure ? "https" : "http"}://$HOST_NAME:$port/')); |
| 278 // handler. | 47 if (followRedirects != null) { |
| 279 int closeCount = 0; | 48 conn.followRedirects = followRedirects; |
| 280 WebSocketHandler wsHandler = new WebSocketHandler(); | 49 } |
| 281 wsHandler.onOpen = (WebSocketConnection conn) { | 50 return new WebSocketClientConnection(conn); |
| 282 connections[conn] = new WebSocketInfo(); | 51 } |
| 52 |
| 53 void testRequestResponseClientCloses( |
| 54 int totalConnections, int closeStatus, String closeReason) { |
| 55 HttpServer server = createServer(backlog: totalConnections); |
| 56 HttpClient client = new HttpClient(); |
| 57 |
| 58 // Make a web socket handler and set it as the HTTP server default handler. |
| 59 WebSocketHandler wsHandler = new WebSocketHandler(); |
| 60 wsHandler.onOpen = (WebSocketConnection conn) { |
| 61 var count = 0; |
| 62 conn.onMessage = (Object message) => conn.send(message); |
| 63 conn.onClosed = (status, reason) { |
| 64 Expect.equals(closeStatus == null |
| 65 ? WebSocketStatus.NO_STATUS_RECEIVED |
| 66 : closeStatus, status); |
| 67 Expect.equals(closeReason == null ? "" : closeReason, reason); |
| 68 }; |
| 69 }; |
| 70 server.defaultRequestHandler = wsHandler.onRequest; |
| 71 |
| 72 int closeCount = 0; |
| 283 String messageText = "Hello, world!"; | 73 String messageText = "Hello, world!"; |
| 284 conn.onMessage = (Object message) { | 74 for (int i = 0; i < totalConnections; i++) { |
| 285 handleMessage(conn, message); | 75 int messageCount = 0; |
| 286 }; | 76 WebSocketClientConnection wsconn = createClient(server.port); |
| 287 conn.onClosed = (status, reason) { | 77 wsconn.onOpen = () => wsconn.send(messageText); |
| 288 closeCount++; | 78 wsconn.onMessage = (message) { |
| 79 messageCount++; |
| 80 if (messageCount < 10) { |
| 81 Expect.equals(messageText, message); |
| 82 wsconn.send(message); |
| 83 } else { |
| 84 wsconn.close(closeStatus, closeReason); |
| 85 } |
| 86 }; |
| 87 wsconn.onClosed = (status, reason) { |
| 88 Expect.equals(closeStatus == null |
| 89 ? WebSocketStatus.NO_STATUS_RECEIVED |
| 90 : closeStatus, status); |
| 91 Expect.equals("", reason); |
| 92 closeCount++; |
| 93 if (closeCount == totalConnections) { |
| 94 client.shutdown(); |
| 95 server.close(); |
| 96 } |
| 97 }; |
| 98 } |
| 99 } |
| 100 |
| 101 |
| 102 void testRequestResponseServerCloses( |
| 103 int totalConnections, int closeStatus, String closeReason) { |
| 104 ReceivePort keepAlive = new ReceivePort(); |
| 105 HttpServer server = createServer(backlog: totalConnections); |
| 106 |
| 107 // Create a web socket handler and set is as the HTTP server default |
| 108 // handler. |
| 109 int closeCount = 0; |
| 110 WebSocketHandler wsHandler = new WebSocketHandler(); |
| 111 wsHandler.onOpen = (WebSocketConnection conn) { |
| 112 String messageText = "Hello, world!"; |
| 113 int messageCount = 0; |
| 114 conn.onMessage = (Object message) { |
| 115 messageCount++; |
| 116 if (messageCount < 10) { |
| 117 Expect.equals(messageText, message); |
| 118 conn.send(message); |
| 119 } else { |
| 120 conn.close(closeStatus, closeReason); |
| 121 } |
| 122 }; |
| 123 conn.onClosed = (status, reason) { |
| 124 Expect.equals(closeStatus == null |
| 125 ? WebSocketStatus.NO_STATUS_RECEIVED |
| 126 : closeStatus, status); |
| 127 Expect.equals("", reason); |
| 128 closeCount++; |
| 129 if (closeCount == totalConnections) { |
| 130 client.shutdown(); |
| 131 server.close(); |
| 132 keepAlive.close(); |
| 133 } |
| 134 }; |
| 135 conn.send(messageText); |
| 136 }; |
| 137 server.defaultRequestHandler = wsHandler.onRequest; |
| 138 |
| 139 for (int i = 0; i < totalConnections; i++) { |
| 140 WebSocketClientConnection wsconn = createClient(server.port); |
| 141 wsconn.onMessage = (message) => wsconn.send(message); |
| 142 wsconn.onClosed = (status, reason) { |
| 143 Expect.equals(closeStatus == null |
| 144 ? WebSocketStatus.NO_STATUS_RECEIVED |
| 145 : closeStatus, status); |
| 146 Expect.equals(closeReason == null ? "" : closeReason, reason); |
| 147 }; |
| 148 } |
| 149 } |
| 150 |
| 151 |
| 152 void testMessageLength(int messageLength) { |
| 153 HttpServer server = createServer(backlog: 1); |
| 154 bool serverReceivedMessage = false; |
| 155 bool clientReceivedMessage = false; |
| 156 |
| 157 // Create a web socket handler and set is as the HTTP server default |
| 158 // handler. |
| 159 Uint8List originalMessage = new Uint8List(messageLength); |
| 160 WebSocketHandler wsHandler = new WebSocketHandler(); |
| 161 wsHandler.onOpen = (WebSocketConnection conn) { |
| 162 conn.onMessage = (Object message) { |
| 163 serverReceivedMessage = true; |
| 164 Expect.listEquals(originalMessage, message); |
| 165 conn.send(message); |
| 166 }; |
| 167 conn.onClosed = (status, reason) { |
| 168 }; |
| 169 }; |
| 170 server.defaultRequestHandler = wsHandler.onRequest; |
| 171 |
| 172 WebSocketClientConnection wsconn = createClient(server.port); |
| 173 wsconn.onMessage = (message) { |
| 174 clientReceivedMessage = true; |
| 175 Expect.listEquals(originalMessage, message); |
| 176 wsconn.close(); |
| 177 }; |
| 178 wsconn.onClosed = (status, reason) { |
| 179 Expect.isTrue(serverReceivedMessage); |
| 180 Expect.isTrue(clientReceivedMessage); |
| 181 client.shutdown(); |
| 182 server.close(); |
| 183 }; |
| 184 wsconn.onOpen = () { |
| 185 wsconn.send(originalMessage); |
| 186 }; |
| 187 } |
| 188 |
| 189 |
| 190 void testNoUpgrade() { |
| 191 HttpServer server = createServer(backlog: 5); |
| 192 |
| 193 // Create a server which always responds with a redirect. |
| 194 server.defaultRequestHandler = (request, response) { |
| 195 response.statusCode = HttpStatus.MOVED_PERMANENTLY; |
| 196 response.outputStream.close(); |
| 197 }; |
| 198 |
| 199 WebSocketClientConnection wsconn = createClient(server.port, |
| 200 followRedirects: false); |
| 201 wsconn.onNoUpgrade = (response) { |
| 202 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); |
| 203 client.shutdown(); |
| 204 server.close(); |
| 205 }; |
| 206 } |
| 207 |
| 208 |
| 209 void testUsePOST() { |
| 210 HttpServer server = createServer(backlog: 5); |
| 211 |
| 212 // Create a web socket handler and set is as the HTTP server default |
| 213 // handler. |
| 214 int closeCount = 0; |
| 215 WebSocketHandler wsHandler = new WebSocketHandler(); |
| 216 wsHandler.onOpen = (WebSocketConnection conn) { |
| 217 Expect.fail("No connection expected"); |
| 218 }; |
| 219 server.defaultRequestHandler = wsHandler.onRequest; |
| 220 |
| 221 WebSocketClientConnection wsconn = createClient(server.port, |
| 222 method: "POST"); |
| 223 wsconn.onNoUpgrade = (response) { |
| 224 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); |
| 225 client.shutdown(); |
| 226 server.close(); |
| 227 }; |
| 228 } |
| 229 |
| 230 |
| 231 void testHashCode(int totalConnections) { |
| 232 ReceivePort keepAlive = new ReceivePort(); |
| 233 HttpServer server = createServer(backlog: totalConnections); |
| 234 Map connections = new Map(); |
| 235 |
| 236 void handleMessage(conn, message) { |
| 289 var info = connections[conn]; | 237 var info = connections[conn]; |
| 290 Expect.equals(10, info.messageCount); | 238 Expect.isNotNull(info); |
| 291 if (closeCount == totalConnections) { | 239 info.messageCount++; |
| 292 client.shutdown(); | 240 if (info.messageCount < 10) { |
| 293 server.close(); | |
| 294 done.complete(); | |
| 295 } | |
| 296 }; | |
| 297 conn.send(messageText); | |
| 298 }; | |
| 299 server.defaultRequestHandler = wsHandler.onRequest; | |
| 300 | |
| 301 for (int i = 0; i < totalConnections; i++) { | |
| 302 HttpClientConnection conn = client.getUrl(new Uri.fromString( | |
| 303 '${secure ? "https" : "http"}://$HOST_NAME:${server.port}/')); | |
| 304 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | |
| 305 wsconn.onMessage = (message) => wsconn.send(message); | |
| 306 } | |
| 307 return done.future; | |
| 308 } | |
| 309 | |
| 310 | |
| 311 Future testW3CInterface( | |
| 312 int totalConnections, int closeStatus, String closeReason) { | |
| 313 List<Future> tasks = []; | |
| 314 HttpServer server = secure ? new HttpsServer() : new HttpServer(); | |
| 315 | |
| 316 server.listen(SERVER_ADDRESS, | |
| 317 0, | |
| 318 backlog: totalConnections, | |
| 319 certificate_name: "CN=$HOST_NAME"); | |
| 320 | |
| 321 // Create a web socket handler and set is as the HTTP server default | |
| 322 // handler. | |
| 323 int closeCount = 0; | |
| 324 WebSocketHandler wsHandler = new WebSocketHandler(); | |
| 325 Completer serverDone = new Completer(); | |
| 326 tasks.add(serverDone.future); | |
| 327 wsHandler.onOpen = (WebSocketConnection conn) { | |
| 328 String messageText = "Hello, world!"; | |
| 329 int messageCount = 0; | |
| 330 conn.onMessage = (Object message) { | |
| 331 messageCount++; | |
| 332 if (messageCount < 10) { | |
| 333 Expect.equals(messageText, message); | |
| 334 conn.send(message); | 241 conn.send(message); |
| 335 } else { | 242 } else { |
| 336 conn.close(closeStatus, closeReason); | 243 conn.close(); |
| 337 } | 244 } |
| 338 }; | 245 } |
| 339 conn.onClosed = (status, reason) { | 246 |
| 340 Expect.equals(closeStatus, status); | 247 // Create a web socket handler and set is as the HTTP server default |
| 341 Expect.equals("", reason); | 248 // handler. |
| 342 closeCount++; | 249 int closeCount = 0; |
| 343 if (closeCount == totalConnections) { | 250 WebSocketHandler wsHandler = new WebSocketHandler(); |
| 344 server.close(); | 251 wsHandler.onOpen = (WebSocketConnection conn) { |
| 345 serverDone.complete(null); | 252 connections[conn] = new WebSocketInfo(); |
| 346 } | 253 String messageText = "Hello, world!"; |
| 347 }; | 254 conn.onMessage = (Object message) { |
| 348 conn.send(messageText); | 255 handleMessage(conn, message); |
| 349 }; | 256 }; |
| 350 server.defaultRequestHandler = wsHandler.onRequest; | 257 conn.onClosed = (status, reason) { |
| 351 | 258 closeCount++; |
| 352 void webSocketConnection() { | 259 var info = connections[conn]; |
| 353 Completer clientDone = new Completer(); | 260 Expect.equals(10, info.messageCount); |
| 354 tasks.add(clientDone.future); | 261 if (closeCount == totalConnections) { |
| 355 bool onopenCalled = false; | 262 client.shutdown(); |
| 356 int onmessageCalled = 0; | 263 server.close(); |
| 357 bool oncloseCalled = false; | 264 keepAlive.close(); |
| 358 | 265 } |
| 359 var websocket = | 266 }; |
| 360 new WebSocket('${secure ? "wss" : "ws"}://$HOST_NAME:${server.port}'); | 267 conn.send(messageText); |
| 361 Expect.equals(WebSocket.CONNECTING, websocket.readyState); | 268 }; |
| 362 websocket.onopen = () { | 269 server.defaultRequestHandler = wsHandler.onRequest; |
| 363 Expect.isFalse(onopenCalled); | 270 |
| 364 Expect.equals(0, onmessageCalled); | 271 for (int i = 0; i < totalConnections; i++) { |
| 365 Expect.isFalse(oncloseCalled); | 272 WebSocketClientConnection wsconn = createClient(server.port); |
| 366 onopenCalled = true; | 273 wsconn.onMessage = (message) => wsconn.send(message); |
| 367 Expect.equals(WebSocket.OPEN, websocket.readyState); | 274 } |
| 368 }; | 275 } |
| 369 websocket.onmessage = (event) { | 276 |
| 370 onmessageCalled++; | 277 |
| 371 Expect.isTrue(onopenCalled); | 278 void testW3CInterface( |
| 372 Expect.isFalse(oncloseCalled); | 279 int totalConnections, int closeStatus, String closeReason) { |
| 373 Expect.equals(WebSocket.OPEN, websocket.readyState); | 280 HttpServer server = createServer(backlog: totalConnections); |
| 374 websocket.send(event.data); | 281 |
| 375 }; | 282 // Create a web socket handler and set is as the HTTP server default |
| 376 websocket.onclose = (event) { | 283 // handler. |
| 377 Expect.isTrue(onopenCalled); | 284 int closeCount = 0; |
| 378 Expect.equals(10, onmessageCalled); | 285 WebSocketHandler wsHandler = new WebSocketHandler(); |
| 379 Expect.isFalse(oncloseCalled); | 286 wsHandler.onOpen = (WebSocketConnection conn) { |
| 380 oncloseCalled = true; | 287 String messageText = "Hello, world!"; |
| 381 Expect.isTrue(event.wasClean); | 288 int messageCount = 0; |
| 382 Expect.equals(3002, event.code); | 289 conn.onMessage = (Object message) { |
| 383 Expect.equals("Got tired", event.reason); | 290 messageCount++; |
| 384 Expect.equals(WebSocket.CLOSED, websocket.readyState); | 291 if (messageCount < 10) { |
| 385 clientDone.complete(null); | 292 Expect.equals(messageText, message); |
| 386 }; | 293 conn.send(message); |
| 387 } | 294 } else { |
| 388 | 295 conn.close(closeStatus, closeReason); |
| 389 for (int i = 0; i < totalConnections; i++) { | 296 } |
| 390 webSocketConnection(); | 297 }; |
| 391 } | 298 conn.onClosed = (status, reason) { |
| 392 return Future.wait(tasks); | 299 Expect.equals(closeStatus, status); |
| 300 Expect.equals("", reason); |
| 301 closeCount++; |
| 302 if (closeCount == totalConnections) { |
| 303 server.close(); |
| 304 } |
| 305 }; |
| 306 conn.send(messageText); |
| 307 }; |
| 308 server.defaultRequestHandler = wsHandler.onRequest; |
| 309 |
| 310 void webSocketConnection() { |
| 311 bool onopenCalled = false; |
| 312 int onmessageCalled = 0; |
| 313 bool oncloseCalled = false; |
| 314 |
| 315 var websocket = |
| 316 new WebSocket('${secure ? "wss" : "ws"}://$HOST_NAME:${server.port}'); |
| 317 Expect.equals(WebSocket.CONNECTING, websocket.readyState); |
| 318 websocket.onopen = () { |
| 319 Expect.isFalse(onopenCalled); |
| 320 Expect.equals(0, onmessageCalled); |
| 321 Expect.isFalse(oncloseCalled); |
| 322 onopenCalled = true; |
| 323 Expect.equals(WebSocket.OPEN, websocket.readyState); |
| 324 }; |
| 325 websocket.onmessage = (event) { |
| 326 onmessageCalled++; |
| 327 Expect.isTrue(onopenCalled); |
| 328 Expect.isFalse(oncloseCalled); |
| 329 Expect.equals(WebSocket.OPEN, websocket.readyState); |
| 330 websocket.send(event.data); |
| 331 }; |
| 332 websocket.onclose = (event) { |
| 333 Expect.isTrue(onopenCalled); |
| 334 Expect.equals(10, onmessageCalled); |
| 335 Expect.isFalse(oncloseCalled); |
| 336 oncloseCalled = true; |
| 337 Expect.isTrue(event.wasClean); |
| 338 Expect.equals(3002, event.code); |
| 339 Expect.equals("Got tired", event.reason); |
| 340 Expect.equals(WebSocket.CLOSED, websocket.readyState); |
| 341 }; |
| 342 } |
| 343 |
| 344 for (int i = 0; i < totalConnections; i++) { |
| 345 webSocketConnection(); |
| 346 } |
| 347 } |
| 348 |
| 349 |
| 350 runTests() { |
| 351 testRequestResponseClientCloses(2, null, null); |
| 352 testRequestResponseClientCloses(2, 3001, null); |
| 353 testRequestResponseClientCloses(2, 3002, "Got tired"); |
| 354 testRequestResponseServerCloses(2, null, null); |
| 355 testRequestResponseServerCloses(2, 3001, null); |
| 356 testRequestResponseServerCloses(2, 3002, "Got tired"); |
| 357 testMessageLength(125); |
| 358 testMessageLength(126); |
| 359 testMessageLength(127); |
| 360 testMessageLength(65535); |
| 361 testMessageLength(65536); |
| 362 testNoUpgrade(); |
| 363 testUsePOST(); |
| 364 testHashCode(2); |
| 365 testW3CInterface(2, 3002, "Got tired"); |
| 366 } |
| 393 } | 367 } |
| 394 | 368 |
| 395 | 369 |
| 396 void InitializeSSL() { | 370 void initializeSSL() { |
| 397 var testPkcertDatabase = | 371 var testPkcertDatabase = |
| 398 new Path(new Options().script).directoryPath.append("pkcert/"); | 372 new Path(new Options().script).directoryPath.append("pkcert/"); |
| 399 SecureSocket.initialize(database: testPkcertDatabase.toNativePath(), | 373 SecureSocket.initialize(database: testPkcertDatabase.toNativePath(), |
| 400 password: "dartdart"); | 374 password: "dartdart"); |
| 401 } | 375 } |
| 402 | 376 |
| 403 | 377 |
| 404 Future runTests() => | |
| 405 testRequestResponseClientCloses(2, null, null).then((_) => | |
| 406 testRequestResponseClientCloses(2, 3001, null)).then((_) => | |
| 407 testRequestResponseClientCloses(2, 3002, "Got tired")).then((_) => | |
| 408 testRequestResponseServerCloses(2, null, null)).then((_) => | |
| 409 testRequestResponseServerCloses(2, 3001, null)).then((_) => | |
| 410 testRequestResponseServerCloses(2, 3002, "Got tired")).then((_) => | |
| 411 testMessageLength(125)).then((_) => | |
| 412 testMessageLength(126)).then((_) => | |
| 413 testMessageLength(127)).then((_) => | |
| 414 testMessageLength(65535)).then((_) => | |
| 415 testMessageLength(65536)).then((_) => | |
| 416 testNoUpgrade()).then((_) => | |
| 417 testUsePOST()).then((_) => | |
| 418 testHashCode(2)).then((_) => | |
| 419 testW3CInterface(2, 3002, "Got tired")); | |
| 420 | |
| 421 | |
| 422 main() { | 378 main() { |
| 423 ReceivePort keepAlive = new ReceivePort(); | 379 new SecurityConfiguration(secure: false).runTests(); |
| 424 runTests().then((_) { | |
| 425 keepAlive.close(); | |
| 426 }); | |
| 427 } | 380 } |
| OLD | NEW |