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