| 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:io"); | 6 #import("dart:io"); |
| 7 | 7 |
| 8 void testRequestResponseClientCloses( | 8 void testRequestResponseClientCloses( |
| 9 int totalConnections, int closeStatus, String closeReason) { | 9 int totalConnections, int closeStatus, String closeReason) { |
| 10 HttpServer server = new HttpServer(); | 10 HttpServer server = new HttpServer(); |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | 94 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); |
| 95 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | 95 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); |
| 96 wsconn.onMessage = (message) => wsconn.send(message); | 96 wsconn.onMessage = (message) => wsconn.send(message); |
| 97 wsconn.onClosed = (status, reason) { | 97 wsconn.onClosed = (status, reason) { |
| 98 Expect.equals(closeStatus, status); | 98 Expect.equals(closeStatus, status); |
| 99 Expect.equals(closeReason, reason); | 99 Expect.equals(closeReason, reason); |
| 100 }; | 100 }; |
| 101 } | 101 } |
| 102 } | 102 } |
| 103 | 103 |
| 104 |
| 104 void testNoUpgrade() { | 105 void testNoUpgrade() { |
| 105 HttpServer server = new HttpServer(); | 106 HttpServer server = new HttpServer(); |
| 106 HttpClient client = new HttpClient(); | 107 HttpClient client = new HttpClient(); |
| 107 | 108 |
| 108 server.listen("127.0.0.1", 0, 5); | 109 server.listen("127.0.0.1", 0, 5); |
| 109 | 110 |
| 110 // Create a server which always responds with a redirect. | 111 // Create a server which always responds with a redirect. |
| 111 server.defaultRequestHandler = (request, response) { | 112 server.defaultRequestHandler = (request, response) { |
| 112 response.statusCode = HttpStatus.MOVED_PERMANENTLY; | 113 response.statusCode = HttpStatus.MOVED_PERMANENTLY; |
| 113 response.outputStream.close(); | 114 response.outputStream.close(); |
| 114 }; | 115 }; |
| 115 | 116 |
| 116 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | 117 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); |
| 117 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | 118 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); |
| 118 wsconn.onNoUpgrade = (response) { | 119 wsconn.onNoUpgrade = (response) { |
| 119 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); | 120 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); |
| 120 client.shutdown(); | 121 client.shutdown(); |
| 121 server.close(); | 122 server.close(); |
| 122 }; | 123 }; |
| 123 } | 124 } |
| 124 | 125 |
| 126 |
| 125 void testUsePOST() { | 127 void testUsePOST() { |
| 126 HttpServer server = new HttpServer(); | 128 HttpServer server = new HttpServer(); |
| 127 HttpClient client = new HttpClient(); | 129 HttpClient client = new HttpClient(); |
| 128 | 130 |
| 129 server.listen("127.0.0.1", 0, 5); | 131 server.listen("127.0.0.1", 0, 5); |
| 130 | 132 |
| 131 // Create a web socket handler and set is as the HTTP server default | 133 // Create a web socket handler and set is as the HTTP server default |
| 132 // handler. | 134 // handler. |
| 133 int closeCount = 0; | 135 int closeCount = 0; |
| 134 WebSocketHandler wsHandler = new WebSocketHandler(); | 136 WebSocketHandler wsHandler = new WebSocketHandler(); |
| 135 wsHandler.onOpen = (WebSocketConnection conn) { | 137 wsHandler.onOpen = (WebSocketConnection conn) { |
| 136 Expect.fail("No connection expected"); | 138 Expect.fail("No connection expected"); |
| 137 }; | 139 }; |
| 138 server.defaultRequestHandler = wsHandler.onRequest; | 140 server.defaultRequestHandler = wsHandler.onRequest; |
| 139 | 141 |
| 140 HttpClientConnection conn = client.post("127.0.0.1", server.port, "/"); | 142 HttpClientConnection conn = client.post("127.0.0.1", server.port, "/"); |
| 141 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | 143 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); |
| 142 wsconn.onNoUpgrade = (response) { | 144 wsconn.onNoUpgrade = (response) { |
| 143 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); | 145 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); |
| 144 client.shutdown(); | 146 client.shutdown(); |
| 145 server.close(); | 147 server.close(); |
| 146 }; | 148 }; |
| 147 } | 149 } |
| 148 | 150 |
| 151 |
| 152 void testW3CInterface( |
| 153 int totalConnections, int closeStatus, String closeReason) { |
| 154 HttpServer server = new HttpServer(); |
| 155 |
| 156 server.listen("127.0.0.1", 0, totalConnections); |
| 157 |
| 158 // Create a web socket handler and set is as the HTTP server default |
| 159 // handler. |
| 160 int closeCount = 0; |
| 161 WebSocketHandler wsHandler = new WebSocketHandler(); |
| 162 wsHandler.onOpen = (WebSocketConnection conn) { |
| 163 String messageText = "Hello, world!"; |
| 164 int messageCount = 0; |
| 165 conn.onMessage = (Object message) { |
| 166 messageCount++; |
| 167 if (messageCount < 10) { |
| 168 Expect.equals(messageText, message); |
| 169 conn.send(message); |
| 170 } else { |
| 171 conn.close(closeStatus, closeReason); |
| 172 } |
| 173 }; |
| 174 conn.onClosed = (status, reason) { |
| 175 Expect.equals(closeStatus, status); |
| 176 Expect.isNull(reason); |
| 177 closeCount++; |
| 178 if (closeCount == totalConnections) { |
| 179 server.close(); |
| 180 } |
| 181 }; |
| 182 conn.send(messageText); |
| 183 }; |
| 184 server.defaultRequestHandler = wsHandler.onRequest; |
| 185 |
| 186 void webSocketConnection() { |
| 187 bool onopenCalled = false; |
| 188 int onmessageCalled = 0; |
| 189 bool oncloseCalled = false; |
| 190 |
| 191 var websocket = new WebSocket("ws://127.0.0.1:${server.port}"); |
| 192 Expect.equals(WebSocket.CONNECTING, websocket.readyState); |
| 193 websocket.onopen = () { |
| 194 Expect.isFalse(onopenCalled); |
| 195 Expect.equals(0, onmessageCalled); |
| 196 Expect.isFalse(oncloseCalled); |
| 197 onopenCalled = true; |
| 198 Expect.equals(WebSocket.OPEN, websocket.readyState); |
| 199 }; |
| 200 websocket.onmessage = (event) { |
| 201 onmessageCalled++; |
| 202 Expect.isTrue(onopenCalled); |
| 203 Expect.isFalse(oncloseCalled); |
| 204 Expect.equals(WebSocket.OPEN, websocket.readyState); |
| 205 websocket.send(event.data); |
| 206 }; |
| 207 websocket.onclose = (event) { |
| 208 Expect.isTrue(onopenCalled); |
| 209 Expect.equals(10, onmessageCalled); |
| 210 Expect.isFalse(oncloseCalled); |
| 211 oncloseCalled = true; |
| 212 Expect.isTrue(event.wasClean); |
| 213 Expect.equals(3002, event.code); |
| 214 Expect.equals("Got tired", event.reason); |
| 215 Expect.equals(WebSocket.CLOSED, websocket.readyState); |
| 216 }; |
| 217 } |
| 218 |
| 219 for (int i = 0; i < totalConnections; i++) { |
| 220 webSocketConnection(); |
| 221 } |
| 222 } |
| 223 |
| 224 |
| 149 main() { | 225 main() { |
| 150 testRequestResponseClientCloses(2, null, null); | 226 testRequestResponseClientCloses(2, null, null); |
| 151 testRequestResponseClientCloses(2, 3001, null); | 227 testRequestResponseClientCloses(2, 3001, null); |
| 152 testRequestResponseClientCloses(2, 3002, "Got tired"); | 228 testRequestResponseClientCloses(2, 3002, "Got tired"); |
| 153 testRequestResponseServerCloses(2, null, null); | 229 testRequestResponseServerCloses(2, null, null); |
| 154 testRequestResponseServerCloses(2, 3001, null); | 230 testRequestResponseServerCloses(2, 3001, null); |
| 155 testRequestResponseServerCloses(2, 3002, "Got tired"); | 231 testRequestResponseServerCloses(2, 3002, "Got tired"); |
| 156 testNoUpgrade(); | 232 testNoUpgrade(); |
| 157 testUsePOST(); | 233 testUsePOST(); |
| 234 |
| 235 testW3CInterface(2, 3002, "Got tired"); |
| 158 } | 236 } |
| OLD | NEW |