Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(5)

Unified Diff: tests/standalone/io/web_socket_test.dart

Issue 10262031: Add a web socket client (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« runtime/bin/websocket_impl.dart ('K') | « runtime/bin/websocket_impl.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: tests/standalone/io/web_socket_test.dart
diff --git a/tests/standalone/io/web_socket_test.dart b/tests/standalone/io/web_socket_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..31367e5ac21c48ff74da9b43b7eb05aed7488ccb
--- /dev/null
+++ b/tests/standalone/io/web_socket_test.dart
@@ -0,0 +1,170 @@
+// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
+// for details. All rights reserved. Use of this source code is governed by a
+// BSD-style license that can be found in the LICENSE file.
+//
+
+#import("dart:io");
+
+void testRequestResponseClientCloses(
+ int totalConnections, int closeStatus, String closeReason) {
+ HttpServer server = new HttpServer();
+ HttpClient client = new HttpClient();
+
+ server.onError = (e) => Expect.fail("Unexpected error $e");
+ server.listen("127.0.0.1", 0, totalConnections);
+
+ // Create a web socket handler and set is as the HTTP server default
+ // handler.
+ WebSocketHandler wsHandler = new WebSocketHandler();
+ wsHandler.onOpen = (WebSocketConnection conn) {
+ var count = 0;
+ conn.onMessage = (Object message) => conn.send(message);
+ conn.onClosed = (status, reason) {
+ Expect.equals(closeStatus, status);
+ Expect.equals(closeReason, reason);
+ };
+ 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.
+ };
+ server.defaultRequestHandler = wsHandler.onRequest;
+
+ int closeCount = 0;
+ String messageText = "Hello, world!";
+ for (int i = 0; i < totalConnections; i++) {
+ int messageCount = 0;
+ HttpClientConnection conn = client.get("127.0.0.1", server.port, "/");
+ WebSocketClientConnection wsconn = new WebSocketClientConnection(conn);
+ 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.
+ wsconn.send(messageText);
+ };
+ wsconn.onMessage = (message) {
+ messageCount++;
+ if (messageCount < 10) {
+ Expect.equals(messageText, message);
+ wsconn.send(message);
+ } else {
+ wsconn.close(closeStatus, closeReason);
+ }
+ };
+ wsconn.onClosed = (status, reason) {
+ Expect.equals(closeStatus, status);
+ Expect.isNull(reason);
+ closeCount++;
+ if (closeCount == totalConnections) {
+ client.shutdown();
+ server.close();
+ }
+ };
+ wsconn.onError = (e) => Expect.fail("Unexpected client error $e");
+ }
+}
+
+
+void testRequestResponseServerCloses(
+ int totalConnections, int closeStatus, String closeReason) {
+ HttpServer server = new HttpServer();
+ HttpClient client = new HttpClient();
+
+ server.onError = (e) => Expect.fail("Unexpected error $e");
+ server.listen("127.0.0.1", 0, totalConnections);
+
+ // Create a web socket handler and set is as the HTTP server default
+ // handler.
+ int closeCount = 0;
+ WebSocketHandler wsHandler = new WebSocketHandler();
+ wsHandler.onOpen = (WebSocketConnection conn) {
+ String messageText = "Hello, world!";
+ int messageCount = 0;
+ conn.onMessage = (Object message) {
+ messageCount++;
+ if (messageCount < 10) {
+ Expect.equals(messageText, message);
+ conn.send(message);
+ } else {
+ conn.close(closeStatus, closeReason);
+ }
+ };
+ conn.onClosed = (status, reason) {
+ Expect.equals(closeStatus, status);
+ Expect.isNull(reason);
+ closeCount++;
+ if (closeCount == totalConnections) {
+ client.shutdown();
+ server.close();
+ }
+ };
+ conn.onError = (e) => Expect.fail("Unexpected error $e");
+ conn.send(messageText);
+ };
+ server.defaultRequestHandler = wsHandler.onRequest;
+
+ for (int i = 0; i < totalConnections; i++) {
+ HttpClientConnection conn = client.get("127.0.0.1", server.port, "/");
+ WebSocketClientConnection wsconn = new WebSocketClientConnection(conn);
+ wsconn.onMessage = (message) => wsconn.send(message);
+ wsconn.onClosed = (status, reason) {
+ Expect.equals(closeStatus, status);
+ Expect.equals(closeReason, reason);
+ };
+ wsconn.onError = (e) => Expect.fail("Unexpected client error $e");
+ }
+}
+
+void testNoUpgrade() {
+ HttpServer server = new HttpServer();
+ HttpClient client = new HttpClient();
+
+ server.onError = (e) => Expect.fail("Unexpected error $e");
+ server.listen("127.0.0.1", 0, 5);
+
+ // Create a server which always responds with a redirect.
+ server.defaultRequestHandler = (request, response) {
+ response.statusCode = HttpStatus.MOVED_PERMANENTLY;
+ response.outputStream.close();
+ };
+
+ HttpClientConnection conn = client.get("127.0.0.1", server.port, "/");
+ WebSocketClientConnection wsconn = new WebSocketClientConnection(conn);
+ wsconn.onNoUpgrade = (response) {
+ Expect.equals(HttpStatus.MOVED_PERMANENTLY, response.statusCode);
+ client.shutdown();
+ server.close();
+ };
+ wsconn.onError = (e) => Expect.fail("Unexpected client error $e");
+}
+
+void testUsePOST() {
+ HttpServer server = new HttpServer();
+ HttpClient client = new HttpClient();
+
+ server.onError = (e) => Expect.fail("Unexpected error $e");
+ server.listen("127.0.0.1", 0, 5);
+
+ // Create a web socket handler and set is as the HTTP server default
+ // handler.
+ int closeCount = 0;
+ WebSocketHandler wsHandler = new WebSocketHandler();
+ wsHandler.onOpen = (WebSocketConnection conn) {
+ Expect.fail("No connection expected");
+ };
+ server.defaultRequestHandler = wsHandler.onRequest;
+
+ HttpClientConnection conn = client.post("127.0.0.1", server.port, "/");
+ WebSocketClientConnection wsconn = new WebSocketClientConnection(conn);
+ wsconn.onNoUpgrade = (response) {
+ Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode);
+ client.shutdown();
+ server.close();
+ };
+ wsconn.onError = (e) => Expect.fail("Unexpected client error $e");
+}
+
+main() {
+ testRequestResponseClientCloses(2, null, null);
+ testRequestResponseClientCloses(2, 3001, null);
+ testRequestResponseClientCloses(2, 3002, "Got tired");
+ testRequestResponseServerCloses(1, null, null);
+ testRequestResponseServerCloses(2, 3001, null);
+ testRequestResponseServerCloses(2, 3002, "Got tired");
+ testNoUpgrade();
+ testUsePOST();
+}
« runtime/bin/websocket_impl.dart ('K') | « runtime/bin/websocket_impl.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698