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

Unified Diff: test/io_test.dart

Issue 1756613002: Add an IO implementation of WebSocketChannel. (Closed) Base URL: git@github.com:dart-lang/web_socket_channel.git@master
Patch Set: Created 4 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
« lib/io.dart ('K') | « lib/web_socket_channel.dart ('k') | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: test/io_test.dart
diff --git a/test/io_test.dart b/test/io_test.dart
new file mode 100644
index 0000000000000000000000000000000000000000..31ddaba937e7c1e7d9fad6e01f6bd18efdbe9fcd
--- /dev/null
+++ b/test/io_test.dart
@@ -0,0 +1,101 @@
+// Copyright (c) 2016, 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.
+
+@TestOn('vm')
+
+import 'dart:io';
+
+import 'package:stream_channel/stream_channel.dart';
kevmoo 2016/03/01 23:24:47 unused import?
nweiz 2016/03/02 00:10:20 Done.
+import 'package:test/test.dart';
+
+import 'package:web_socket_channel/io.dart';
+
+void main() {
+ var server;
+ tearDown(() async {
+ if (server != null) await server.close();
+ });
+
+ test("communicates using existing WebSockets", () async {
+ server = await HttpServer.bind("localhost", 0);
+ server.transform(new WebSocketTransformer()).listen((webSocket) {
+ var channel = new IOWebSocketChannel(webSocket);
+ channel.sink.add("hello!");
+ channel.stream.listen((request) {
+ expect(request, equals("ping"));
+ channel.sink.add("pong");
+ channel.sink.close(5678, "raisin");
+ });
+ });
+
+ var webSocket = await WebSocket.connect("ws://localhost:${server.port}");
+ var channel = new IOWebSocketChannel(webSocket);
+
+ var n = 0;
+ channel.stream.listen((message) {
+ if (n == 0) {
+ expect(message, equals("hello!"));
+ channel.sink.add("ping");
+ } else if (n == 1) {
+ expect(message, equals("pong"));
+ } else {
+ fail("Only expected two messages.");
+ }
+ n++;
+ }, onDone: expectAsync(() {
+ expect(channel.closeCode, equals(5678));
+ expect(channel.closeReason, equals("raisin"));
+ }));
+ });
+
+ test(".connect communicates immediately", () async {
+ server = await HttpServer.bind("localhost", 0);
+ server.transform(new WebSocketTransformer()).listen((webSocket) {
+ var channel = new IOWebSocketChannel(webSocket);
+ channel.stream.listen((request) {
+ expect(request, equals("ping"));
+ channel.sink.add("pong");
+ });
+ });
+
+ var channel = new IOWebSocketChannel.connect(
+ "ws://localhost:${server.port}");
+ channel.sink.add("ping");
+
+ channel.stream.listen(expectAsync((message) {
+ expect(message, equals("pong"));
+ channel.sink.close(5678, "raisin");
+ }, count: 1), onDone: expectAsync(() {}));
+ });
+
+ test(".connect with an immediate call to close", () async {
+ server = await HttpServer.bind("localhost", 0);
+ server.transform(new WebSocketTransformer()).listen((webSocket) {
+ expect(() async {
+ var channel = new IOWebSocketChannel(webSocket);
+ await channel.stream.listen(null).asFuture();
+ expect(channel.closeCode, equals(5678));
+ expect(channel.closeReason, equals("raisin"));
+ }(), completes);
+ });
+
+ var channel = new IOWebSocketChannel.connect(
+ "ws://localhost:${server.port}");
+ channel.sink.close(5678, "raisin");
+ });
+
+ test(".connect wraps a connection error in WebSocketChannelException",
+ () async {
+ server = await HttpServer.bind("localhost", 0);
+ server.listen((request) {
+ request.response.statusCode = 404;
+ request.response.close();
+ });
+
+ var channel = new IOWebSocketChannel.connect(
+ "ws://localhost:${server.port}");
+ expect(channel.stream.toList(),
+ throwsA(new isInstanceOf<WebSocketChannelException>()));
kevmoo 2016/03/01 23:24:46 missing import of lib w/ WebSocketChannelException
nweiz 2016/03/02 00:10:20 Done.
+ });
+}
« lib/io.dart ('K') | « lib/web_socket_channel.dart ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698