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 #import("dart:scalarlist"); | 7 #import("dart:scalarlist"); |
8 | 8 |
9 void testRequestResponseClientCloses( | 9 void testRequestResponseClientCloses( |
10 int totalConnections, int closeStatus, String closeReason) { | 10 int totalConnections, int closeStatus, String closeReason) { |
11 HttpServer server = new HttpServer(); | 11 HttpServer server = new HttpServer(); |
12 HttpClient client = new HttpClient(); | 12 HttpClient client = new HttpClient(); |
13 | 13 |
14 server.listen("127.0.0.1", 0, backlog: totalConnections); | 14 server.listen("127.0.0.1", 0, backlog: totalConnections); |
15 | 15 |
16 // Create a web socket handler and set is as the HTTP server default | 16 // Create a web socket handler and set is as the HTTP server default |
17 // handler. | 17 // handler. |
18 WebSocketHandler wsHandler = new WebSocketHandler(); | 18 WebSocketHandler wsHandler = new WebSocketHandler(); |
19 wsHandler.onOpen = (WebSocketConnection conn) { | 19 wsHandler.onOpen = (WebSocketConnection conn) { |
20 var count = 0; | 20 var count = 0; |
21 conn.onMessage = (Object message) => conn.send(message); | 21 conn.onMessage = (Object message) => conn.send(message); |
22 conn.onClosed = (status, reason) { | 22 conn.onClosed = (status, reason) { |
23 Expect.equals(closeStatus === null | 23 Expect.equals(closeStatus == null |
24 ? WebSocketStatus.NO_STATUS_RECEIVED | 24 ? WebSocketStatus.NO_STATUS_RECEIVED |
25 : closeStatus, status); | 25 : closeStatus, status); |
26 Expect.equals(closeReason === null ? "" : closeReason, reason); | 26 Expect.equals(closeReason == null ? "" : closeReason, reason); |
27 }; | 27 }; |
28 }; | 28 }; |
29 server.defaultRequestHandler = wsHandler.onRequest; | 29 server.defaultRequestHandler = wsHandler.onRequest; |
30 | 30 |
31 int closeCount = 0; | 31 int closeCount = 0; |
32 String messageText = "Hello, world!"; | 32 String messageText = "Hello, world!"; |
33 for (int i = 0; i < totalConnections; i++) { | 33 for (int i = 0; i < totalConnections; i++) { |
34 int messageCount = 0; | 34 int messageCount = 0; |
35 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | 35 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); |
36 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | 36 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); |
37 wsconn.onOpen = () => wsconn.send(messageText); | 37 wsconn.onOpen = () => wsconn.send(messageText); |
38 wsconn.onMessage = (message) { | 38 wsconn.onMessage = (message) { |
39 messageCount++; | 39 messageCount++; |
40 if (messageCount < 10) { | 40 if (messageCount < 10) { |
41 Expect.equals(messageText, message); | 41 Expect.equals(messageText, message); |
42 wsconn.send(message); | 42 wsconn.send(message); |
43 } else { | 43 } else { |
44 wsconn.close(closeStatus, closeReason); | 44 wsconn.close(closeStatus, closeReason); |
45 } | 45 } |
46 }; | 46 }; |
47 wsconn.onClosed = (status, reason) { | 47 wsconn.onClosed = (status, reason) { |
48 Expect.equals(closeStatus === null | 48 Expect.equals(closeStatus == null |
49 ? WebSocketStatus.NO_STATUS_RECEIVED | 49 ? WebSocketStatus.NO_STATUS_RECEIVED |
50 : closeStatus, status); | 50 : closeStatus, status); |
51 Expect.equals("", reason); | 51 Expect.equals("", reason); |
52 closeCount++; | 52 closeCount++; |
53 if (closeCount == totalConnections) { | 53 if (closeCount == totalConnections) { |
54 client.shutdown(); | 54 client.shutdown(); |
55 server.close(); | 55 server.close(); |
56 } | 56 } |
57 }; | 57 }; |
58 } | 58 } |
(...skipping 17 matching lines...) Expand all Loading... |
76 conn.onMessage = (Object message) { | 76 conn.onMessage = (Object message) { |
77 messageCount++; | 77 messageCount++; |
78 if (messageCount < 10) { | 78 if (messageCount < 10) { |
79 Expect.equals(messageText, message); | 79 Expect.equals(messageText, message); |
80 conn.send(message); | 80 conn.send(message); |
81 } else { | 81 } else { |
82 conn.close(closeStatus, closeReason); | 82 conn.close(closeStatus, closeReason); |
83 } | 83 } |
84 }; | 84 }; |
85 conn.onClosed = (status, reason) { | 85 conn.onClosed = (status, reason) { |
86 Expect.equals(closeStatus === null | 86 Expect.equals(closeStatus == null |
87 ? WebSocketStatus.NO_STATUS_RECEIVED | 87 ? WebSocketStatus.NO_STATUS_RECEIVED |
88 : closeStatus, status); | 88 : closeStatus, status); |
89 Expect.equals("", reason); | 89 Expect.equals("", reason); |
90 closeCount++; | 90 closeCount++; |
91 if (closeCount == totalConnections) { | 91 if (closeCount == totalConnections) { |
92 client.shutdown(); | 92 client.shutdown(); |
93 server.close(); | 93 server.close(); |
94 } | 94 } |
95 }; | 95 }; |
96 conn.send(messageText); | 96 conn.send(messageText); |
97 }; | 97 }; |
98 server.defaultRequestHandler = wsHandler.onRequest; | 98 server.defaultRequestHandler = wsHandler.onRequest; |
99 | 99 |
100 for (int i = 0; i < totalConnections; i++) { | 100 for (int i = 0; i < totalConnections; i++) { |
101 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); | 101 HttpClientConnection conn = client.get("127.0.0.1", server.port, "/"); |
102 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); | 102 WebSocketClientConnection wsconn = new WebSocketClientConnection(conn); |
103 wsconn.onMessage = (message) => wsconn.send(message); | 103 wsconn.onMessage = (message) => wsconn.send(message); |
104 wsconn.onClosed = (status, reason) { | 104 wsconn.onClosed = (status, reason) { |
105 Expect.equals(closeStatus === null | 105 Expect.equals(closeStatus == null |
106 ? WebSocketStatus.NO_STATUS_RECEIVED | 106 ? WebSocketStatus.NO_STATUS_RECEIVED |
107 : closeStatus, status); | 107 : closeStatus, status); |
108 Expect.equals(closeReason === null ? "" : closeReason, reason); | 108 Expect.equals(closeReason == null ? "" : closeReason, reason); |
109 }; | 109 }; |
110 } | 110 } |
111 } | 111 } |
112 | 112 |
113 | 113 |
114 void testMessageLength(int messageLength) { | 114 void testMessageLength(int messageLength) { |
115 HttpServer server = new HttpServer(); | 115 HttpServer server = new HttpServer(); |
116 HttpClient client = new HttpClient(); | 116 HttpClient client = new HttpClient(); |
117 | 117 |
118 server.listen("127.0.0.1", 0, backlog: 1); | 118 server.listen("127.0.0.1", 0, backlog: 1); |
(...skipping 213 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
332 testMessageLength(126); | 332 testMessageLength(126); |
333 testMessageLength(127); | 333 testMessageLength(127); |
334 testMessageLength(65535); | 334 testMessageLength(65535); |
335 testMessageLength(65536); | 335 testMessageLength(65536); |
336 testNoUpgrade(); | 336 testNoUpgrade(); |
337 testUsePOST(); | 337 testUsePOST(); |
338 testHashCode(2); | 338 testHashCode(2); |
339 | 339 |
340 testW3CInterface(2, 3002, "Got tired"); | 340 testW3CInterface(2, 3002, "Got tired"); |
341 } | 341 } |
OLD | NEW |