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

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

Issue 12340072: dart:io | Enable tests of secure websocket connections. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Remove indentation FOR REVIEW ONLY. Intentation is restored in version to commit. Created 7 years, 10 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
Index: tests/standalone/io/web_socket_no_secure_test.dart
diff --git a/tests/standalone/io/web_socket_no_secure_test.dart b/tests/standalone/io/web_socket_no_secure_test.dart
index 46e1d032677b13df9f342823fd3ff299652fd860..dfeea63f0ec1dabedc2428e7ece4796e64c0afb4 100644
--- a/tests/standalone/io/web_socket_no_secure_test.dart
+++ b/tests/standalone/io/web_socket_no_secure_test.dart
@@ -10,14 +10,40 @@
// TODO(7157): Remove this test once the bug is fixed.
// This is a copy of web_socket_test.dart with the secure connection
// tests disabled, so it does not crash on Windows.
+
import "dart:io";
import "dart:isolate";
import "dart:scalarlist";
-
-void testRequestResponseClientCloses(
- int totalConnections, int closeStatus, String closeReason) {
- HttpServer.bind().then((server) {
-
+import "dart:uri";
+
+const String CERT_NAME = 'localhost_cert';
+const String SERVER_ADDRESS = '127.0.0.1';
+const String HOST_NAME = 'localhost';
+
+/**
+ * A SecurityConfiguration lets us run the tests over HTTP or HTTPS.
+ */
+class SecurityConfiguration {
+ final bool secure;
+
+ SecurityConfiguration({bool this.secure});
+
+ Future<HttpServer> createServer({int backlog: 0}) =>
+ secure ? HttpServer.bindSecure(SERVER_ADDRESS,
+ 0,
+ backlog: backlog,
+ certificateName: CERT_NAME)
+ : HttpServer.bind(SERVER_ADDRESS,
+ 0,
+ backlog);
+
+ Future<WebSocket> createClient(int port) =>
+ WebSocket.connect('${secure ? "wss" : "ws"}://$HOST_NAME:$port/');
+
+void testRequestResponseClientCloses(int totalConnections,
+ int closeStatus,
+ String closeReason) {
+ createServer().then((server) {
server.transform(new WebSocketTransformer()).listen((webSocket) {
webSocket.listen((event) {
if (event is MessageEvent) {
@@ -35,40 +61,37 @@ void testRequestResponseClientCloses(
String messageText = "Hello, world!";
for (int i = 0; i < totalConnections; i++) {
int messageCount = 0;
- WebSocket.connect("ws://127.0.0.1:${server.port}/")
- .then((webSocket) {
- webSocket.send(messageText);
- webSocket.listen((event) {
- if (event is MessageEvent) {
- messageCount++;
- if (messageCount < 1 ) {
- Expect.equals(messageText, event.data);
- webSocket.send(event.data);
- } else {
- webSocket.close(closeStatus, closeReason);
- }
- } else if (event is CloseEvent) {
- Expect.equals(closeStatus == null
- ? WebSocketStatus.NO_STATUS_RECEIVED
- : closeStatus, event.code);
- Expect.equals("", event.reason);
- closeCount++;
- if (closeCount == totalConnections) {
- server.close();
- }
+ createClient(server.port).then((webSocket) {
+ webSocket.send(messageText);
Bill Hesse 2013/02/26 14:56:43 This is also just an indentation change, from 65 t
Søren Gjesse 2013/02/26 15:28:51 Thanks.
+ webSocket.listen((event) {
+ if (event is MessageEvent) {
+ messageCount++;
+ if (messageCount < 1 ) {
+ Expect.equals(messageText, event.data);
+ webSocket.send(event.data);
+ } else {
+ webSocket.close(closeStatus, closeReason);
}
- });
+ } else if (event is CloseEvent) {
+ Expect.equals(closeStatus == null
+ ? WebSocketStatus.NO_STATUS_RECEIVED
+ : closeStatus, event.code);
+ Expect.equals("", event.reason);
+ closeCount++;
+ if (closeCount == totalConnections) {
+ server.close();
+ }
+ }
});
+ });
}
-
});
}
-
-void testRequestResponseServerCloses(
- int totalConnections, int closeStatus, String closeReason) {
- HttpServer.bind().then((server) {
-
+void testRequestResponseServerCloses(int totalConnections,
+ int closeStatus,
+ String closeReason) {
+ createServer().then((server) {
int closeCount = 0;
server.transform(new WebSocketTransformer()).listen((webSocket) {
String messageText = "Hello, world!";
@@ -97,8 +120,7 @@ void testRequestResponseServerCloses(
});
for (int i = 0; i < totalConnections; i++) {
- WebSocket.connect("ws://127.0.0.1:${server.port}/")
- .then((webSocket) {
+ createClient(server.port).then((webSocket) {
webSocket.listen((event) {
if (event is MessageEvent) {
webSocket.send(event.data);
@@ -118,8 +140,7 @@ void testRequestResponseServerCloses(
void testMessageLength(int messageLength) {
- HttpServer.bind().then((server) {
-
+ createServer().then((server) {
Uint8List originalMessage = new Uint8List(messageLength);
server.transform(new WebSocketTransformer()).listen((webSocket) {
webSocket.listen((event) {
@@ -131,8 +152,7 @@ void testMessageLength(int messageLength) {
});
});
- WebSocket.connect("ws://127.0.0.1:${server.port}/")
- .then((webSocket) {
+ createClient(server.port).then((webSocket) {
webSocket.listen((event) {
if (event is MessageEvent) {
Expect.listEquals(originalMessage, event.data);
@@ -143,31 +163,27 @@ void testMessageLength(int messageLength) {
});
webSocket.send(originalMessage);
});
-
});
}
void testNoUpgrade() {
- HttpServer.bind().then((server) {
-
+ createServer().then((server) {
// Create a server which always responds with NOT_FOUND.
server.listen((request) {
request.response.statusCode = HttpStatus.NOT_FOUND;
request.response.close();
});
- WebSocket.connect("ws://127.0.0.1:${server.port}/").catchError((error) {
+ createClient(server.port).catchError((error) {
server.close();
});
-
});
}
void testUsePOST() {
- HttpServer.bind().then((server) {
-
+ createServer().then((server) {
var errorPort = new ReceivePort();
server.transform(new WebSocketTransformer()).listen((webSocket) {
Expect.fail("No connection expected");
@@ -176,27 +192,21 @@ void testUsePOST() {
});
HttpClient client = new HttpClient();
- client.post("127.0.0.1", server.port, "/")
+ client.postUrl(Uri.parse(
+ "${secure ? 'https:' : 'http:'}//$HOST_NAME:${server.port}/"))
.then((request) => request.close())
.then((response) {
Expect.equals(HttpStatus.BAD_REQUEST, response.statusCode);
client.close();
server.close();
});
-
});
}
-
-class WebSocketInfo {
- int messageCount = 0;
-}
-
-
-void testW3CInterface(
- int totalConnections, int closeStatus, String closeReason) {
- HttpServer.bind().then((server) {
-
+void testW3CInterface(int totalConnections,
Søren Gjesse 2013/02/26 15:28:51 Do we need this separate test anymore now that thi
Bill Hesse 2013/02/26 16:09:50 It is written differently, and tests different thi
+ int closeStatus,
+ String closeReason) {
+ createServer().then((server) {
int closeCount = 0;
server.transform(new WebSocketTransformer()).listen((webSocket) {
String messageText = "Hello, world!";
@@ -227,14 +237,14 @@ void testW3CInterface(
int onmessageCalled = 0;
bool oncloseCalled = false;
- WebSocket.connect("ws://127.0.0.1:${server.port}").then((webSocket) {
+ createClient(server.port).then((webSocket) {
Expect.isFalse(onopenCalled);
Expect.equals(0, onmessageCalled);
Expect.isFalse(oncloseCalled);
onopenCalled = true;
Expect.equals(WebSocket.OPEN, webSocket.readyState);
webSocket.listen((event) {
- if (event is MessageEvent) {
+ if (event is MessageEvent) {
onmessageCalled++;
Expect.isTrue(onopenCalled);
Expect.isFalse(oncloseCalled);
@@ -257,25 +267,28 @@ void testW3CInterface(
for (int i = 0; i < totalConnections; i++) {
webSocketConnection();
}
-
});
}
+ void runTests() {
+ testRequestResponseClientCloses(2, null, null);
+ testRequestResponseClientCloses(2, 3001, null);
+ testRequestResponseClientCloses(2, 3002, "Got tired");
+ testRequestResponseServerCloses(2, null, null);
+ testRequestResponseServerCloses(2, 3001, null);
+ testRequestResponseServerCloses(2, 3002, "Got tired");
+ testMessageLength(125);
+ testMessageLength(126);
+ testMessageLength(127);
+ testMessageLength(65535);
+ testMessageLength(65536);
+ testNoUpgrade();
+ testUsePOST();
+ testW3CInterface(2, 3002, "Got tired");
+ }
+}
+
main() {
- testRequestResponseClientCloses(2, null, null);
- testRequestResponseClientCloses(2, 3001, null);
- testRequestResponseClientCloses(2, 3002, "Got tired");
- testRequestResponseServerCloses(2, null, null);
- testRequestResponseServerCloses(2, 3001, null);
- testRequestResponseServerCloses(2, 3002, "Got tired");
- testMessageLength(125);
- testMessageLength(126);
- testMessageLength(127);
- testMessageLength(65535);
- testMessageLength(65536);
- testNoUpgrade();
- testUsePOST();
-
- testW3CInterface(2, 3002, "Got tired");
+ new SecurityConfiguration(secure: false).runTests();
}

Powered by Google App Engine
This is Rietveld 408576698