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