Skip to content

Commit

Permalink
Connect URI
Browse files Browse the repository at this point in the history
  • Loading branch information
brianquinlan committed Mar 5, 2024
1 parent 49880a3 commit 8376322
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions pkgs/web_socket/lib/src/browser_web_socket.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ class BrowserWebSocket implements WebSocket {
/// [RFC-6455 1.9](https://datatracker.ietf.org/doc/html/rfc6455#section-1.9).
static Future<BrowserWebSocket> connect(Uri url,
{Iterable<String>? protocols}) async {
if (!url.isScheme('ws') && !url.isScheme('wss')) {
throw WebSocketException("Unsupported URL scheme '${url.scheme}'");
}

final webSocket = web.WebSocket(url.toString(),
protocols?.map((e) => e.toJS).toList().toJS ?? JSArray())
..binaryType = 'arraybuffer';
Expand Down
14 changes: 14 additions & 0 deletions pkgs/web_socket_conformance_tests/lib/src/connect_uri_tests.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import 'package:test/test.dart';
import 'package:web_socket/web_socket.dart';

/// Tests that the [WebSocket] rejects invalid connection URIs.
void testConnectUri(
Future<WebSocket> Function(Uri uri, {Iterable<String>? protocols})
channelFactory) {
group('connect uri', () {
test('no protocol', () async {
await expectLater(() => channelFactory(Uri.https('www.example.com', '/')),
throwsA(isA<WebSocketException>()));
});
});
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import 'package:web_socket/web_socket.dart';
import 'src/close_local_tests.dart';
import 'src/close_remote_tests.dart';
import 'src/connect_uri_tests.dart';
import 'src/disconnect_after_upgrade_tests.dart';
import 'src/no_upgrade_tests.dart';
import 'src/payload_transfer_tests.dart';
Expand All @@ -17,6 +18,7 @@ void testAll(
webSocketFactory) {
testCloseLocal(webSocketFactory);
testCloseRemote(webSocketFactory);
testConnectUri(webSocketFactory);
testDisconnectAfterUpgrade(webSocketFactory);
testNoUpgrade(webSocketFactory);
testPayloadTransfer(webSocketFactory);
Expand Down

0 comments on commit 8376322

Please sign in to comment.