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

Unified Diff: pkg/http_parser/test/web_socket_test.dart

Issue 250073002: Re-apply "Add a non-dart:io WebSocket implementation to http_parser." (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 6 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
« no previous file with comments | « pkg/http_parser/pubspec.yaml ('k') | pkg/pkg.status » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: pkg/http_parser/test/web_socket_test.dart
diff --git a/pkg/http_parser/test/web_socket_test.dart b/pkg/http_parser/test/web_socket_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..5eadbff65c4c95a9575ba4a7ed884302da509b8c
--- /dev/null
+++ b/pkg/http_parser/test/web_socket_test.dart
@@ -0,0 +1,95 @@
+// Copyright (c) 2014, 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.
+
+library http_parser.web_socket_test;
+
+import 'dart:io';
+
+import 'package:http_parser/http_parser.dart';
+import 'package:unittest/unittest.dart';
+
+void main() {
+ test("a client can communicate with a WebSocket server", () {
+ return HttpServer.bind("localhost", 0).then((server) {
+ server.transform(new WebSocketTransformer()).listen((webSocket) {
+ webSocket.add("hello!");
+ webSocket.first.then((request) {
+ expect(request, equals("ping"));
+ webSocket.add("pong");
+ webSocket.close();
+ });
+ });
+
+ var client = new HttpClient();
+ return client.openUrl("GET", Uri.parse("http://localhost:${server.port}"))
+ .then((request) {
+ request.headers
+ ..set("Connection", "Upgrade")
+ ..set("Upgrade", "websocket")
+ ..set("Sec-WebSocket-Key", "x3JJHMbDL1EzLkh9GBhXDw==")
+ ..set("Sec-WebSocket-Version", "13");
+ return request.close();
+ }).then((response) => response.detachSocket()).then((socket) {
+ var webSocket = new CompatibleWebSocket(socket, serverSide: false);
+
+ var n = 0;
+ return webSocket.listen((message) {
+ if (n == 0) {
+ expect(message, equals("hello!"));
+ webSocket.add("ping");
+ } else if (n == 1) {
+ expect(message, equals("pong"));
+ webSocket.close();
+ server.close();
+ } else {
+ expect(false, reason: "Only expected two messages.");
+ }
+ n++;
+ }).asFuture();
+ });
+ });
+ });
+
+ test("a server can communicate with a WebSocket client", () {
+ return HttpServer.bind("localhost", 0).then((server) {
+ server.listen((request) {
+ var response = request.response;
+ response.statusCode = 101;
+ response.headers
+ ..set("Connection", "Upgrade")
+ ..set("Upgrade", "websocket")
+ ..set("Sec-WebSocket-Accept", CompatibleWebSocket.signKey(
+ request.headers.value('Sec-WebSocket-Key')));
+ response.contentLength = 0;
+ response.detachSocket().then((socket) {
+ var webSocket = new CompatibleWebSocket(socket);
+ webSocket.add("hello!");
+ webSocket.first.then((request) {
+ expect(request, equals("ping"));
+ webSocket.add("pong");
+ webSocket.close();
+ });
+ });
+ });
+
+ return WebSocket.connect('ws://localhost:${server.port}')
+ .then((webSocket) {
+ var n = 0;
+ return webSocket.listen((message) {
+ if (n == 0) {
+ expect(message, equals("hello!"));
+ webSocket.add("ping");
+ } else if (n == 1) {
+ expect(message, equals("pong"));
+ webSocket.close();
+ server.close();
+ } else {
+ expect(false, reason: "Only expected two messages.");
+ }
+ n++;
+ }).asFuture();
+ });
+ });
+ });
+}
« no previous file with comments | « pkg/http_parser/pubspec.yaml ('k') | pkg/pkg.status » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698