OLD | NEW |
---|---|
(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('browser') | |
6 @Skip( | |
7 "This suite requires a WebSocket server, which is currently unsupported\n" | |
kevmoo
2016/03/02 06:40:01
...I remember opening the feature request for pkg/
nweiz
2016/03/02 19:24:45
I'll add an issue reference.
| |
8 "by the test package. It's currently set up to talk to a hard-coded\n" | |
9 "server on localhost:1234 that is spawned in html_test_server.dart.") | |
10 | |
11 import 'dart:async'; | |
12 import 'dart:html'; | |
13 import 'dart:typed_data'; | |
14 | |
15 import 'package:async/async.dart'; | |
16 import 'package:test/test.dart'; | |
17 | |
18 import 'package:web_socket_channel/html.dart'; | |
19 | |
20 void main() { | |
21 var channel; | |
22 tearDown(() { | |
23 if (channel != null) channel.sink.close(); | |
24 }); | |
25 | |
26 test("communicates using an existing WebSocket", () async { | |
27 var webSocket = new WebSocket("ws://localhost:1234"); | |
28 channel = new HtmlWebSocketChannel(webSocket); | |
29 | |
30 var queue = new StreamQueue(channel.stream); | |
31 channel.sink.add("foo"); | |
32 expect(await queue.next, equals("foo")); | |
33 | |
34 channel.sink.add(new Uint8List.fromList([1, 2, 3, 4, 5])); | |
35 expect(await _decodeBlob(await queue.next), equals([1, 2, 3, 4, 5])); | |
36 | |
37 webSocket.binaryType = "arraybuffer"; | |
38 channel.sink.add(new Uint8List.fromList([1, 2, 3, 4, 5])); | |
39 expect(await queue.next, equals([1, 2, 3, 4, 5])); | |
40 }); | |
41 | |
42 test("communicates using an existing open WebSocket", () async { | |
43 var webSocket = new WebSocket("ws://localhost:1234"); | |
44 await webSocket.onOpen.first; | |
45 | |
46 channel = new HtmlWebSocketChannel(webSocket); | |
47 | |
48 var queue = new StreamQueue(channel.stream); | |
49 channel.sink.add("foo"); | |
50 expect(await queue.next, equals("foo")); | |
51 }); | |
52 | |
53 test(".connect defaults to binary lists", () async { | |
54 channel = new HtmlWebSocketChannel.connect("ws://localhost:1234"); | |
55 | |
56 var queue = new StreamQueue(channel.stream); | |
57 channel.sink.add("foo"); | |
58 expect(await queue.next, equals("foo")); | |
59 | |
60 channel.sink.add(new Uint8List.fromList([1, 2, 3, 4, 5])); | |
61 expect(await queue.next, equals([1, 2, 3, 4, 5])); | |
62 }); | |
63 | |
64 test(".connect can use blobs", () async { | |
65 channel = new HtmlWebSocketChannel.connect( | |
66 "ws://localhost:1234", binaryType: BinaryType.blob); | |
67 | |
68 var queue = new StreamQueue(channel.stream); | |
69 channel.sink.add("foo"); | |
70 expect(await queue.next, equals("foo")); | |
71 | |
72 channel.sink.add(new Uint8List.fromList([1, 2, 3, 4, 5])); | |
73 expect(await _decodeBlob(await queue.next), equals([1, 2, 3, 4, 5])); | |
74 }); | |
75 | |
76 test(".connect wraps a connection error in WebSocketChannelException", | |
77 () async { | |
78 // TODO(nweiz): Make this channel use a port number that's guaranteed to be | |
79 // invalid. | |
80 var channel = new HtmlWebSocketChannel.connect("ws://localhost:1235"); | |
81 expect(channel.stream.toList(), | |
82 throwsA(new isInstanceOf<WebSocketChannelException>())); | |
83 }); | |
84 } | |
85 | |
86 Future<List<int>> _decodeBlob(Blob blob) async { | |
87 var reader = new FileReader(); | |
88 reader.readAsArrayBuffer(blob); | |
89 await reader.onLoad.first; | |
90 return reader.result; | |
91 } | |
OLD | NEW |