Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 // | |
| 5 | |
| 6 #import("dart:io"); | |
| 7 | |
| 8 void testRequestResponseClientCloses( | |
| 9 int totalConnections, int closeStatus, String closeReason) { | |
| 10 HttpServer server = new HttpServer(); | |
| 11 HttpClient client = new HttpClient(); | |
| 12 | |
| 13 server.onError = (e) => Expect.fail("Unexpected error $e"); | |
| 14 server.listen("127.0.0.1", 0, totalConnections); | |
| 15 | |
| 16 // Create a web socket handler and set is as the HTTP server default | |
| 17 // handler. | |
| 18 WebSocketHandler wsHandler = new WebSocketHandler(); | |
| 19 wsHandler.onOpen = (WebSocketConnection conn) { | |
| 20 var count = 0; | |
| 21 conn.onMessage = (Object message) => conn.send(message); | |
| 22 conn.onClosed = (status, reason) { | |
| 23 Expect.equals(closeStatus, status); | |
| 24 Expect.equals(closeReason, reason); | |
| 25 }; | |
| 26 conn.onError = (e) => Expect.fail("Unexpected error $e"); | |
|
Mads Ager (google)
2012/05/02 08:18:17
Since the error handler throws by default now we c
Søren Gjesse
2012/05/02 10:22:45
No, not realy - removed.
| |
| 27 }; | |
| 28 server.defaultRequestHandler = wsHandler.onRequest; | |
| 29 | |
| 30 int closeCount = 0; | |
| 31 String messageText = "Hello, world!"; | |
| 32 for (int i = 0; i < totalConnections; i++) { | |
| 33 int messageCount = 0; | |
| 34 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | |
| 35 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | |
| 36 wsconn.onOpen = () { | |
|
Mads Ager (google)
2012/05/02 08:18:17
Could be a one-liner.
Søren Gjesse
2012/05/02 10:22:45
Done.
| |
| 37 wsconn.send(messageText); | |
| 38 }; | |
| 39 wsconn.onMessage = (message) { | |
| 40 messageCount++; | |
| 41 if (messageCount < 10) { | |
| 42 Expect.equals(messageText, message); | |
| 43 wsconn.send(message); | |
| 44 } else { | |
| 45 wsconn.close(closeStatus, closeReason); | |
| 46 } | |
| 47 }; | |
| 48 wsconn.onClosed = (status, reason) { | |
| 49 Expect.equals(closeStatus, status); | |
| 50 Expect.isNull(reason); | |
| 51 closeCount++; | |
| 52 if (closeCount == totalConnections) { | |
| 53 client.shutdown(); | |
| 54 server.close(); | |
| 55 } | |
| 56 }; | |
| 57 wsconn.onError = (e) => Expect.fail("Unexpected client error $e"); | |
| 58 } | |
| 59 } | |
| 60 | |
| 61 | |
| 62 void testRequestResponseServerCloses( | |
| 63 int totalConnections, int closeStatus, String closeReason) { | |
| 64 HttpServer server = new HttpServer(); | |
| 65 HttpClient client = new HttpClient(); | |
| 66 | |
| 67 server.onError = (e) => Expect.fail("Unexpected error $e"); | |
| 68 server.listen("127.0.0.1", 0, totalConnections); | |
| 69 | |
| 70 // Create a web socket handler and set is as the HTTP server default | |
| 71 // handler. | |
| 72 int closeCount = 0; | |
| 73 WebSocketHandler wsHandler = new WebSocketHandler(); | |
| 74 wsHandler.onOpen = (WebSocketConnection conn) { | |
| 75 String messageText = "Hello, world!"; | |
| 76 int messageCount = 0; | |
| 77 conn.onMessage = (Object message) { | |
| 78 messageCount++; | |
| 79 if (messageCount < 10) { | |
| 80 Expect.equals(messageText, message); | |
| 81 conn.send(message); | |
| 82 } else { | |
| 83 conn.close(closeStatus, closeReason); | |
| 84 } | |
| 85 }; | |
| 86 conn.onClosed = (status, reason) { | |
| 87 Expect.equals(closeStatus, status); | |
| 88 Expect.isNull(reason); | |
| 89 closeCount++; | |
| 90 if (closeCount == totalConnections) { | |
| 91 client.shutdown(); | |
| 92 server.close(); | |
| 93 } | |
| 94 }; | |
| 95 conn.onError = (e) => Expect.fail("Unexpected error $e"); | |
| 96 conn.send(messageText); | |
| 97 }; | |
| 98 server.defaultRequestHandler = wsHandler.onRequest; | |
| 99 | |
| 100 for (int i = 0; i < totalConnections; i++) { | |
| 101 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | |
| 102 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | |
| 103 wsconn.onMessage = (message) => wsconn.send(message); | |
| 104 wsconn.onClosed = (status, reason) { | |
| 105 Expect.equals(closeStatus, status); | |
| 106 Expect.equals(closeReason, reason); | |
| 107 }; | |
| 108 wsconn.onError = (e) => Expect.fail("Unexpected client error $e"); | |
| 109 } | |
| 110 } | |
| 111 | |
| 112 void testNoUpgrade() { | |
| 113 HttpServer server = new HttpServer(); | |
| 114 HttpClient client = new HttpClient(); | |
| 115 | |
| 116 server.onError = (e) => Expect.fail("Unexpected error $e"); | |
| 117 server.listen("127.0.0.1", 0, 5); | |
| 118 | |
| 119 // Create a server which always responds with a redirect. | |
| 120 server.defaultRequestHandler = (request, response) { | |
| 121 response.statusCode = HttpStatus.MOVED_PERMANENTLY; | |
| 122 response.outputStream.close(); | |
| 123 }; | |
| 124 | |
| 125 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | |
| 126 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | |
| 127 wsconn.onNoUpgrade = (response) { | |
| 128 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); | |
| 129 client.shutdown(); | |
| 130 server.close(); | |
| 131 }; | |
| 132 wsconn.onError = (e) => Expect.fail("Unexpected client error $e"); | |
| 133 } | |
| 134 | |
| 135 void testUsePOST() { | |
| 136 HttpServer server = new HttpServer(); | |
| 137 HttpClient client = new HttpClient(); | |
| 138 | |
| 139 server.onError = (e) => Expect.fail("Unexpected error $e"); | |
| 140 server.listen("127.0.0.1", 0, 5); | |
| 141 | |
| 142 // Create a web socket handler and set is as the HTTP server default | |
| 143 // handler. | |
| 144 int closeCount = 0; | |
| 145 WebSocketHandler wsHandler = new WebSocketHandler(); | |
| 146 wsHandler.onOpen = (WebSocketConnection conn) { | |
| 147 Expect.fail("No connection expected"); | |
| 148 }; | |
| 149 server.defaultRequestHandler = wsHandler.onRequest; | |
| 150 | |
| 151 HttpClientConnection conn = client.post("127.0.0.1", server.port, "/"); | |
| 152 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | |
| 153 wsconn.onNoUpgrade = (response) { | |
| 154 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); | |
| 155 client.shutdown(); | |
| 156 server.close(); | |
| 157 }; | |
| 158 wsconn.onError = (e) => Expect.fail("Unexpected client error $e"); | |
| 159 } | |
| 160 | |
| 161 main() { | |
| 162 testRequestResponseClientCloses(2, null, null); | |
| 163 testRequestResponseClientCloses(2, 3001, null); | |
| 164 testRequestResponseClientCloses(2, 3002, "Got tired"); | |
| 165 testRequestResponseServerCloses(1, null, null); | |
| 166 testRequestResponseServerCloses(2, 3001, null); | |
| 167 testRequestResponseServerCloses(2, 3002, "Got tired"); | |
| 168 testNoUpgrade(); | |
| 169 testUsePOST(); | |
| 170 } | |
| OLD | NEW |