-
Notifications
You must be signed in to change notification settings - Fork 107
/
io_test.dart
249 lines (209 loc) · 7.97 KB
/
io_test.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// 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')
library;
import 'dart:async';
import 'dart:io';
import 'package:test/test.dart';
import 'package:web_socket_channel/io.dart';
import 'package:web_socket_channel/web_socket_channel.dart';
void main() {
HttpServer server;
test('communicates using existing WebSockets', () async {
server = await HttpServer.bind('localhost', 0);
addTearDown(server.close);
server.transform(WebSocketTransformer()).listen((WebSocket webSocket) {
final channel = IOWebSocketChannel(webSocket);
channel.sink.add('hello!');
channel.stream.listen((request) {
expect(request, equals('ping'));
channel.sink.add('pong');
channel.sink.close(3678, 'raisin');
});
});
final webSocket = await WebSocket.connect('ws://localhost:${server.port}');
final channel = IOWebSocketChannel(webSocket);
expect(channel.ready, completes);
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: expectAsync0(() {
expect(channel.closeCode, equals(3678));
expect(channel.closeReason, equals('raisin'));
}));
});
test('.connect communicates immediately', () async {
server = await HttpServer.bind('localhost', 0);
addTearDown(server.close);
server.transform(WebSocketTransformer()).listen((WebSocket webSocket) {
final channel = IOWebSocketChannel(webSocket);
channel.stream.listen((request) {
expect(request, equals('ping'));
channel.sink.add('pong');
});
});
final channel = IOWebSocketChannel.connect('ws://localhost:${server.port}');
expect(channel.ready, completes);
channel.sink.add('ping');
channel.stream.listen(expectAsync1((message) {
expect(message, equals('pong'));
channel.sink.close(3678, 'raisin');
}), onDone: expectAsync0(() {}));
});
test('.connect communicates immediately using platform independent api',
() async {
server = await HttpServer.bind('localhost', 0);
addTearDown(server.close);
server.transform(WebSocketTransformer()).listen((WebSocket webSocket) {
final channel = IOWebSocketChannel(webSocket);
channel.stream.listen((request) {
expect(request, equals('ping'));
channel.sink.add('pong');
});
});
final channel =
WebSocketChannel.connect(Uri.parse('ws://localhost:${server.port}'));
expect(channel.ready, completes);
channel.sink.add('ping');
channel.stream.listen(expectAsync1((message) {
expect(message, equals('pong'));
channel.sink.close(3678, 'raisin');
}), onDone: expectAsync0(() {}));
});
test('.connect with an immediate call to close', () async {
server = await HttpServer.bind('localhost', 0);
addTearDown(server.close);
server.transform(WebSocketTransformer()).listen((WebSocket webSocket) {
expect(() async {
final channel = IOWebSocketChannel(webSocket);
await channel.stream.drain<void>();
expect(channel.closeCode, equals(3678));
expect(channel.closeReason, equals('raisin'));
}(), completes);
});
final channel = IOWebSocketChannel.connect('ws://localhost:${server.port}');
expect(channel.ready, completes);
await channel.sink.close(3678, 'raisin');
});
test('.connect wraps a connection error in WebSocketChannelException',
() async {
server = await HttpServer.bind('localhost', 0);
addTearDown(server.close);
server.listen((request) {
request.response.statusCode = 404;
request.response.close();
});
final channel = IOWebSocketChannel.connect('ws://localhost:${server.port}');
expect(channel.ready, throwsA(isA<WebSocketChannelException>()));
expect(channel.stream.drain<void>(),
throwsA(isA<WebSocketChannelException>()));
});
test('.protocols fail', () async {
const passedProtocol = 'passed-protocol';
const failedProtocol = 'failed-protocol';
String selector(List<String> receivedProtocols) => passedProtocol;
server = await HttpServer.bind('localhost', 0);
addTearDown(server.close);
server.listen((HttpRequest request) {
expect(
WebSocketTransformer.upgrade(request, protocolSelector: selector),
throwsException,
);
});
final channel = IOWebSocketChannel.connect(
'ws://localhost:${server.port}',
protocols: [failedProtocol],
);
expect(channel.ready, throwsA(isA<WebSocketChannelException>()));
expect(
channel.stream.drain<void>(),
throwsA(isA<WebSocketChannelException>()),
);
});
test('.protocols pass', () async {
const passedProtocol = 'passed-protocol';
String selector(List<String> receivedProtocols) => passedProtocol;
server = await HttpServer.bind('localhost', 0);
addTearDown(server.close);
server.listen((HttpRequest request) async {
final webSocket = await WebSocketTransformer.upgrade(
request,
protocolSelector: selector,
);
expect(webSocket.protocol, passedProtocol);
await webSocket.close();
});
final channel = IOWebSocketChannel.connect('ws://localhost:${server.port}',
protocols: [passedProtocol]);
expect(channel.ready, completes);
await channel.stream.drain<void>();
expect(channel.protocol, passedProtocol);
});
test('.connects with a timeout parameters specified', () async {
server = await HttpServer.bind('localhost', 0);
addTearDown(server.close);
server.transform(WebSocketTransformer()).listen((webSocket) {
expect(() async {
final channel = IOWebSocketChannel(webSocket);
await channel.stream.drain<void>();
expect(channel.closeCode, equals(3678));
expect(channel.closeReason, equals('raisin'));
}(), completes);
});
final channel = IOWebSocketChannel.connect(
'ws://localhost:${server.port}',
connectTimeout: const Duration(milliseconds: 1000),
);
expect(channel.ready, completes);
await channel.sink.close(3678, 'raisin');
});
test('.respects timeout parameter when trying to connect', () async {
server = await HttpServer.bind('localhost', 0);
addTearDown(server.close);
server
.transform(StreamTransformer<HttpRequest, HttpRequest>.fromHandlers(
handleData: (data, sink) {
// Wait before we handle this request, to give the timeout a chance to
// kick in. We still want to make sure that we handle the request
// afterwards to not have false positives with the timeout
Timer(const Duration(milliseconds: 800), () {
sink.add(data);
});
}))
.transform(WebSocketTransformer())
.listen((webSocket) {
final channel = IOWebSocketChannel(webSocket);
channel.stream.drain<void>();
});
final channel = IOWebSocketChannel.connect(
'ws://localhost:${server.port}',
connectTimeout: const Duration(milliseconds: 500),
);
expect(channel.ready, throwsA(isA<TimeoutException>()));
expect(channel.stream.drain<void>(), throwsA(anything));
});
test('.custom client is passed through', () async {
server = await HttpServer.bind('localhost', 0);
addTearDown(server.close);
server.transform(StreamTransformer<HttpRequest, HttpRequest>.fromHandlers(
handleData: (data, sink) {
expect(data.headers['user-agent'], ['custom']);
sink.add(data);
},
)).transform(WebSocketTransformer());
final channel = IOWebSocketChannel.connect(
'ws://localhost:${server.port}',
customClient: HttpClient()..userAgent = 'custom',
);
expect(channel.ready, completes);
});
}