33// BSD-style license that can be found in the LICENSE file.
44
55@TestOn ('browser' )
6- @Skip (
7- "This suite requires a WebSocket server, which is currently unsupported\n "
8- "by the test package (dart-lang/test#330). It's currently set up to talk\n "
9- "to a hard-coded server on localhost:1234 that is spawned in \n "
10- "html_test_server.dart." )
116
127import 'dart:async' ;
138import 'dart:html' ;
@@ -20,13 +15,34 @@ import 'package:web_socket_channel/html.dart';
2015import 'package:web_socket_channel/web_socket_channel.dart' ;
2116
2217void main () {
23- var channel;
18+ int port;
19+ setUpAll (() async {
20+ var channel = spawnHybridCode (r"""
21+ import 'dart:io';
22+
23+ import 'package:stream_channel/stream_channel.dart';
24+
25+ hybridMain(StreamChannel channel) async {
26+ var server = await HttpServer.bind('localhost', 0);
27+ server.transform(new WebSocketTransformer()).listen((webSocket) {
28+ webSocket.listen((request) {
29+ webSocket.add(request);
30+ });
31+ });
32+ channel.sink.add(server.port);
33+ }
34+ """ , stayAlive: true );
35+
36+ port = await channel.stream.first;
37+ });
38+
39+ WebSocketChannel channel;
2440 tearDown (() {
2541 if (channel != null ) channel.sink.close ();
2642 });
2743
2844 test ("communicates using an existing WebSocket" , () async {
29- var webSocket = new WebSocket ("ws://localhost:1234 " );
45+ var webSocket = new WebSocket ("ws://localhost:$ port " );
3046 channel = new HtmlWebSocketChannel (webSocket);
3147
3248 var queue = new StreamQueue (channel.stream);
@@ -42,7 +58,7 @@ void main() {
4258 });
4359
4460 test ("communicates using an existing open WebSocket" , () async {
45- var webSocket = new WebSocket ("ws://localhost:1234 " );
61+ var webSocket = new WebSocket ("ws://localhost:$ port " );
4662 await webSocket.onOpen.first;
4763
4864 channel = new HtmlWebSocketChannel (webSocket);
@@ -53,7 +69,7 @@ void main() {
5369 });
5470
5571 test (".connect defaults to binary lists" , () async {
56- channel = new HtmlWebSocketChannel .connect ("ws://localhost:1234 " );
72+ channel = new HtmlWebSocketChannel .connect ("ws://localhost:$ port " );
5773
5874 var queue = new StreamQueue (channel.stream);
5975 channel.sink.add ("foo" );
@@ -65,7 +81,7 @@ void main() {
6581
6682 test (".connect can use blobs" , () async {
6783 channel = new HtmlWebSocketChannel .connect (
68- "ws://localhost:1234 " , binaryType: BinaryType .blob);
84+ "ws://localhost:$ port " , binaryType: BinaryType .blob);
6985
7086 var queue = new StreamQueue (channel.stream);
7187 channel.sink.add ("foo" );
@@ -77,9 +93,25 @@ void main() {
7793
7894 test (".connect wraps a connection error in WebSocketChannelException" ,
7995 () async {
96+ // Spawn a server that will immediately reject the connection.
97+ var serverChannel = spawnHybridCode (r"""
98+ import 'dart:io';
99+
100+ import 'package:stream_channel/stream_channel.dart';
101+
102+ hybridMain(StreamChannel channel) async {
103+ var server = await ServerSocket.bind('localhost', 0);
104+ server.listen((socket) {
105+ socket.close();
106+ });
107+ channel.sink.add(server.port);
108+ }
109+ """ );
110+
80111 // TODO(nweiz): Make this channel use a port number that's guaranteed to be
81112 // invalid.
82- var channel = new HtmlWebSocketChannel .connect ("ws://localhost:1235" );
113+ var channel = new HtmlWebSocketChannel .connect (
114+ "ws://localhost:${await serverChannel .stream .first }" );
83115 expect (channel.stream.toList (),
84116 throwsA (new isInstanceOf <WebSocketChannelException >()));
85117 });
0 commit comments