3
3
// BSD-style license that can be found in the LICENSE file.
4
4
5
5
@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." )
11
6
12
7
import 'dart:async' ;
13
8
import 'dart:html' ;
@@ -20,13 +15,34 @@ import 'package:web_socket_channel/html.dart';
20
15
import 'package:web_socket_channel/web_socket_channel.dart' ;
21
16
22
17
void 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;
24
40
tearDown (() {
25
41
if (channel != null ) channel.sink.close ();
26
42
});
27
43
28
44
test ("communicates using an existing WebSocket" , () async {
29
- var webSocket = new WebSocket ("ws://localhost:1234 " );
45
+ var webSocket = new WebSocket ("ws://localhost:$ port " );
30
46
channel = new HtmlWebSocketChannel (webSocket);
31
47
32
48
var queue = new StreamQueue (channel.stream);
@@ -42,7 +58,7 @@ void main() {
42
58
});
43
59
44
60
test ("communicates using an existing open WebSocket" , () async {
45
- var webSocket = new WebSocket ("ws://localhost:1234 " );
61
+ var webSocket = new WebSocket ("ws://localhost:$ port " );
46
62
await webSocket.onOpen.first;
47
63
48
64
channel = new HtmlWebSocketChannel (webSocket);
@@ -53,7 +69,7 @@ void main() {
53
69
});
54
70
55
71
test (".connect defaults to binary lists" , () async {
56
- channel = new HtmlWebSocketChannel .connect ("ws://localhost:1234 " );
72
+ channel = new HtmlWebSocketChannel .connect ("ws://localhost:$ port " );
57
73
58
74
var queue = new StreamQueue (channel.stream);
59
75
channel.sink.add ("foo" );
@@ -65,7 +81,7 @@ void main() {
65
81
66
82
test (".connect can use blobs" , () async {
67
83
channel = new HtmlWebSocketChannel .connect (
68
- "ws://localhost:1234 " , binaryType: BinaryType .blob);
84
+ "ws://localhost:$ port " , binaryType: BinaryType .blob);
69
85
70
86
var queue = new StreamQueue (channel.stream);
71
87
channel.sink.add ("foo" );
@@ -77,9 +93,25 @@ void main() {
77
93
78
94
test (".connect wraps a connection error in WebSocketChannelException" ,
79
95
() 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
+
80
111
// TODO(nweiz): Make this channel use a port number that's guaranteed to be
81
112
// invalid.
82
- var channel = new HtmlWebSocketChannel .connect ("ws://localhost:1235" );
113
+ var channel = new HtmlWebSocketChannel .connect (
114
+ "ws://localhost:${await serverChannel .stream .first }" );
83
115
expect (channel.stream.toList (),
84
116
throwsA (new isInstanceOf <WebSocketChannelException >()));
85
117
});
0 commit comments