Skip to content

Commit 0937562

Browse files
authored
Allow 1000 as a WebSocket close code (#1211)
Fixes #1203
1 parent f361779 commit 0937562

File tree

9 files changed

+45
-10
lines changed

9 files changed

+45
-10
lines changed

pkgs/cupertino_http/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 1.5.1-wip
2+
3+
* Allow `1000` as a `code` argument in `CupertinoWebSocket.close`.
4+
15
## 1.5.0
26

37
* Add integration to the

pkgs/cupertino_http/lib/src/cupertino_web_socket.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -185,8 +185,9 @@ class CupertinoWebSocket implements WebSocket {
185185
throw WebSocketConnectionClosed();
186186
}
187187

188-
if (code != null) {
189-
RangeError.checkValueInInterval(code, 3000, 4999, 'code');
188+
if (code != null && code != 1000 && !(code >= 3000 && code <= 4999)) {
189+
throw ArgumentError('Invalid argument: $code, close code must be 1000 or '
190+
'in the range 3000-4999');
190191
}
191192
if (reason != null && utf8.encode(reason).length > 123) {
192193
throw ArgumentError.value(reason, 'reason',

pkgs/cupertino_http/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name: cupertino_http
2-
version: 1.5.0
2+
version: 1.5.1-wip
33
description: >-
44
A macOS/iOS Flutter plugin that provides access to the Foundation URL
55
Loading System.

pkgs/web_socket/CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
## 0.1.5
2+
3+
- Allow `1000` as a close code.
4+
15
## 0.1.4
26

37
- Add a `fakes` function that returns a pair of `WebSocket`s useful in

pkgs/web_socket/lib/src/utils.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ import 'dart:convert';
66

77
/// Throw if the given close code is not valid.
88
void checkCloseCode(int? code) {
9-
if (code != null) {
10-
RangeError.checkValueInInterval(code, 3000, 4999, 'code');
9+
if (code != null && code != 1000 && !(code >= 3000 && code <= 4999)) {
10+
throw ArgumentError('Invalid argument: $code, close code must be 1000 or '
11+
'in the range 3000-4999');
1112
}
1213
}
1314

pkgs/web_socket/lib/src/web_socket.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -150,7 +150,7 @@ abstract interface class WebSocket {
150150
/// [code] is set then the peer will see a 1005 status code. If no [reason]
151151
/// is set then the peer will not receive a reason string.
152152
///
153-
/// Throws a [RangeError] if [code] is not in the range 3000-4999.
153+
/// Throws an [ArgumentError] if [code] is not 1000 or in the range 3000-4999.
154154
///
155155
/// Throws an [ArgumentError] if [reason] is longer than 123 bytes when
156156
/// encoded as UTF-8

pkgs/web_socket/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ description: >-
33
Any easy-to-use library for communicating with WebSockets
44
that has multiple implementations.
55
repository: https://github.com/dart-lang/http/tree/master/pkgs/web_socket
6-
version: 0.1.4
6+
version: 0.1.5
77

88
environment:
99
sdk: ^3.3.0

pkgs/web_socket/test/fake_web_socket_test.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ void proxy(WebSocket from, WebSocket to) {
1616
case BinaryDataReceived(:final data):
1717
to.sendBytes(data);
1818
case CloseReceived(:var code, :final reason):
19-
if (code != null && (code < 3000 || code > 4999)) {
19+
if (code != null && code != 1000 && (code < 3000 || code > 4999)) {
2020
code = null;
2121
}
2222
to.close(code, reason);

pkgs/web_socket_conformance_tests/lib/src/close_local_tests.dart

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,22 @@ void testCloseLocal(
5555
httpServerChannel.sink.add(null);
5656
});
5757

58-
test('reserved close code', () async {
58+
test('reserved close code: 1004', () async {
5959
final channel = await channelFactory(uri);
60-
await expectLater(() => channel.close(1004), throwsA(isA<RangeError>()));
60+
await expectLater(
61+
() => channel.close(1004), throwsA(isA<ArgumentError>()));
62+
});
63+
64+
test('reserved close code: 2999', () async {
65+
final channel = await channelFactory(uri);
66+
await expectLater(
67+
() => channel.close(2999), throwsA(isA<ArgumentError>()));
68+
});
69+
70+
test('reserved close code: 5000', () async {
71+
final channel = await channelFactory(uri);
72+
await expectLater(
73+
() => channel.close(5000), throwsA(isA<ArgumentError>()));
6174
});
6275

6376
test('too long close reason', () async {
@@ -78,6 +91,18 @@ void testCloseLocal(
7891
expect(await channel.events.isEmpty, true);
7992
});
8093

94+
test('close with 1000', () async {
95+
final channel = await channelFactory(uri);
96+
97+
await channel.close(1000);
98+
final closeCode = await httpServerQueue.next as int?;
99+
final closeReason = await httpServerQueue.next as String?;
100+
101+
expect(closeCode, 1000);
102+
expect(closeReason, '');
103+
expect(await channel.events.isEmpty, true);
104+
});
105+
81106
test('with code 3000', () async {
82107
final channel = await channelFactory(uri);
83108

0 commit comments

Comments
 (0)