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

Side by Side 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, 9 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 unified diff | Download patch
« lib/io.dart ('K') | « lib/web_socket_channel.dart ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(Empty)
1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file
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.
4
5 @TestOn('vm')
6
7 import 'dart:io';
8
9 import 'package:stream_channel/stream_channel.dart';
kevmoo 2016/03/01 23:24:47 unused import?
nweiz 2016/03/02 00:10:20 Done.
10 import 'package:test/test.dart';
11
12 import 'package:web_socket_channel/io.dart';
13
14 void main() {
15 var server;
16 tearDown(() async {
17 if (server != null) await server.close();
18 });
19
20 test("communicates using existing WebSockets", () async {
21 server = await HttpServer.bind("localhost", 0);
22 server.transform(new WebSocketTransformer()).listen((webSocket) {
23 var channel = new IOWebSocketChannel(webSocket);
24 channel.sink.add("hello!");
25 channel.stream.listen((request) {
26 expect(request, equals("ping"));
27 channel.sink.add("pong");
28 channel.sink.close(5678, "raisin");
29 });
30 });
31
32 var webSocket = await WebSocket.connect("ws://localhost:${server.port}");
33 var channel = new IOWebSocketChannel(webSocket);
34
35 var n = 0;
36 channel.stream.listen((message) {
37 if (n == 0) {
38 expect(message, equals("hello!"));
39 channel.sink.add("ping");
40 } else if (n == 1) {
41 expect(message, equals("pong"));
42 } else {
43 fail("Only expected two messages.");
44 }
45 n++;
46 }, onDone: expectAsync(() {
47 expect(channel.closeCode, equals(5678));
48 expect(channel.closeReason, equals("raisin"));
49 }));
50 });
51
52 test(".connect communicates immediately", () async {
53 server = await HttpServer.bind("localhost", 0);
54 server.transform(new WebSocketTransformer()).listen((webSocket) {
55 var channel = new IOWebSocketChannel(webSocket);
56 channel.stream.listen((request) {
57 expect(request, equals("ping"));
58 channel.sink.add("pong");
59 });
60 });
61
62 var channel = new IOWebSocketChannel.connect(
63 "ws://localhost:${server.port}");
64 channel.sink.add("ping");
65
66 channel.stream.listen(expectAsync((message) {
67 expect(message, equals("pong"));
68 channel.sink.close(5678, "raisin");
69 }, count: 1), onDone: expectAsync(() {}));
70 });
71
72 test(".connect with an immediate call to close", () async {
73 server = await HttpServer.bind("localhost", 0);
74 server.transform(new WebSocketTransformer()).listen((webSocket) {
75 expect(() async {
76 var channel = new IOWebSocketChannel(webSocket);
77 await channel.stream.listen(null).asFuture();
78 expect(channel.closeCode, equals(5678));
79 expect(channel.closeReason, equals("raisin"));
80 }(), completes);
81 });
82
83 var channel = new IOWebSocketChannel.connect(
84 "ws://localhost:${server.port}");
85 channel.sink.close(5678, "raisin");
86 });
87
88 test(".connect wraps a connection error in WebSocketChannelException",
89 () async {
90 server = await HttpServer.bind("localhost", 0);
91 server.listen((request) {
92 request.response.statusCode = 404;
93 request.response.close();
94 });
95
96 var channel = new IOWebSocketChannel.connect(
97 "ws://localhost:${server.port}");
98 expect(channel.stream.toList(),
99 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.
100 });
101 }
OLDNEW
« 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