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