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.listen("127.0.0.1", 0, totalConnections); |
| 14 |
| 15 // Create a web socket handler and set is as the HTTP server default |
| 16 // handler. |
| 17 WebSocketHandler wsHandler = new WebSocketHandler(); |
| 18 wsHandler.onOpen = (WebSocketConnection conn) { |
| 19 var count = 0; |
| 20 conn.onMessage = (Object message) => conn.send(message); |
| 21 conn.onClosed = (status, reason) { |
| 22 Expect.equals(closeStatus, status); |
| 23 Expect.equals(closeReason, reason); |
| 24 }; |
| 25 }; |
| 26 server.defaultRequestHandler = wsHandler.onRequest; |
| 27 |
| 28 int closeCount = 0; |
| 29 String messageText = "Hello, world!"; |
| 30 for (int i = 0; i < totalConnections; i++) { |
| 31 int messageCount = 0; |
| 32 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); |
| 33 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); |
| 34 wsconn.onOpen = () => wsconn.send(messageText); |
| 35 wsconn.onMessage = (message) { |
| 36 messageCount++; |
| 37 if (messageCount < 10) { |
| 38 Expect.equals(messageText, message); |
| 39 wsconn.send(message); |
| 40 } else { |
| 41 wsconn.close(closeStatus, closeReason); |
| 42 } |
| 43 }; |
| 44 wsconn.onClosed = (status, reason) { |
| 45 Expect.equals(closeStatus, status); |
| 46 Expect.isNull(reason); |
| 47 closeCount++; |
| 48 if (closeCount == totalConnections) { |
| 49 client.shutdown(); |
| 50 server.close(); |
| 51 } |
| 52 }; |
| 53 } |
| 54 } |
| 55 |
| 56 |
| 57 void testRequestResponseServerCloses( |
| 58 int totalConnections, int closeStatus, String closeReason) { |
| 59 HttpServer server = new HttpServer(); |
| 60 HttpClient client = new HttpClient(); |
| 61 |
| 62 server.listen("127.0.0.1", 0, totalConnections); |
| 63 |
| 64 // Create a web socket handler and set is as the HTTP server default |
| 65 // handler. |
| 66 int closeCount = 0; |
| 67 WebSocketHandler wsHandler = new WebSocketHandler(); |
| 68 wsHandler.onOpen = (WebSocketConnection conn) { |
| 69 String messageText = "Hello, world!"; |
| 70 int messageCount = 0; |
| 71 conn.onMessage = (Object message) { |
| 72 messageCount++; |
| 73 if (messageCount < 10) { |
| 74 Expect.equals(messageText, message); |
| 75 conn.send(message); |
| 76 } else { |
| 77 conn.close(closeStatus, closeReason); |
| 78 } |
| 79 }; |
| 80 conn.onClosed = (status, reason) { |
| 81 Expect.equals(closeStatus, status); |
| 82 Expect.isNull(reason); |
| 83 closeCount++; |
| 84 if (closeCount == totalConnections) { |
| 85 client.shutdown(); |
| 86 server.close(); |
| 87 } |
| 88 }; |
| 89 conn.send(messageText); |
| 90 }; |
| 91 server.defaultRequestHandler = wsHandler.onRequest; |
| 92 |
| 93 for (int i = 0; i < totalConnections; i++) { |
| 94 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); |
| 95 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); |
| 96 wsconn.onMessage = (message) => wsconn.send(message); |
| 97 wsconn.onClosed = (status, reason) { |
| 98 Expect.equals(closeStatus, status); |
| 99 Expect.equals(closeReason, reason); |
| 100 }; |
| 101 } |
| 102 } |
| 103 |
| 104 void testNoUpgrade() { |
| 105 HttpServer server = new HttpServer(); |
| 106 HttpClient client = new HttpClient(); |
| 107 |
| 108 server.listen("127.0.0.1", 0, 5); |
| 109 |
| 110 // Create a server which always responds with a redirect. |
| 111 server.defaultRequestHandler = (request, response) { |
| 112 response.statusCode = HttpStatus.MOVED_PERMANENTLY; |
| 113 response.outputStream.close(); |
| 114 }; |
| 115 |
| 116 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); |
| 117 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); |
| 118 wsconn.onNoUpgrade = (response) { |
| 119 Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode); |
| 120 client.shutdown(); |
| 121 server.close(); |
| 122 }; |
| 123 } |
| 124 |
| 125 void testUsePOST() { |
| 126 HttpServer server = new HttpServer(); |
| 127 HttpClient client = new HttpClient(); |
| 128 |
| 129 server.listen("127.0.0.1", 0, 5); |
| 130 |
| 131 // Create a web socket handler and set is as the HTTP server default |
| 132 // handler. |
| 133 int closeCount = 0; |
| 134 WebSocketHandler wsHandler = new WebSocketHandler(); |
| 135 wsHandler.onOpen = (WebSocketConnection conn) { |
| 136 Expect.fail("No connection expected"); |
| 137 }; |
| 138 server.defaultRequestHandler = wsHandler.onRequest; |
| 139 |
| 140 HttpClientConnection conn = client.post("127.0.0.1", server.port, "/"); |
| 141 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); |
| 142 wsconn.onNoUpgrade = (response) { |
| 143 Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode); |
| 144 client.shutdown(); |
| 145 server.close(); |
| 146 }; |
| 147 } |
| 148 |
| 149 main() { |
| 150 testRequestResponseClientCloses(2, null, null); |
| 151 testRequestResponseClientCloses(2, 3001, null); |
| 152 testRequestResponseClientCloses(2, 3002, "Got tired"); |
| 153 testRequestResponseServerCloses(1, null, null); |
| 154 testRequestResponseServerCloses(2, 3001, null); |
| 155 testRequestResponseServerCloses(2, 3002, "Got tired"); |
| 156 testNoUpgrade(); |
| 157 testUsePOST(); |
| 158 } |
OLD | NEW |