-
Notifications
You must be signed in to change notification settings - Fork 54
/
close.any.js
184 lines (163 loc) · 6.96 KB
/
close.any.js
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
// META: script=../websocket.sub.js
// META: script=resources/url-constants.js
// META: global=window,worker
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
wss.close({code: 3456, reason: 'pizza'});
const { code, reason } = await wss.closed;
assert_equals(code, 3456, 'code should match');
assert_equals(reason, 'pizza', 'reason should match');
}, 'close code should be sent to server and reflected back');
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
wss.close();
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
assert_equals(reason, '', 'reason should be empty');
}, 'no close argument should send empty Close frame');
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
wss.close({});
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
assert_equals(reason, '', 'reason should be empty');
}, 'unspecified close code should send empty Close frame');
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
wss.close({reason: ''});
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
assert_equals(reason, '', 'reason should be empty');
}, 'unspecified close code with empty reason should send empty Close frame');
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
wss.close({reason: 'non-empty'});
const { code, reason } = await wss.closed;
assert_equals(code, 1000, 'code should be set');
assert_equals(reason, 'non-empty', 'reason should match');
}, 'unspecified close code with non-empty reason should set code to 1000');
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
assert_throws_js(TypeError, () => wss.close(true),
'close should throw a TypeError');
}, 'close(true) should throw a TypeError');
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
const reason = '.'.repeat(124);
assert_throws_dom('SyntaxError', () => wss.close({ reason }),
'close should throw a TypeError');
}, 'close() with an overlong reason should throw');
promise_test(t => {
const wss = new WebSocketStream(ECHOURL);
wss.close();
return Promise.all([
promise_rejects_dom(t, 'NetworkError', wss.connection,
'connection promise should reject'),
promise_rejects_dom(t, 'NetworkError', wss.closed,
'closed promise should reject')]);
}, 'close during handshake should work');
for (const invalidCode of [999, 1001, 2999, 5000]) {
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
await wss.connection;
assert_throws_dom('InvalidAccessError', () => wss.close({ code: invalidCode }),
'close should throw a TypeError');
}, `close() with invalid code ${invalidCode} should throw`);
}
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const { writable } = await wss.connection;
writable.getWriter().close();
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
assert_equals(reason, '', 'reason should be empty');
}, 'closing the writable should result in a clean close');
promise_test(async () => {
const wss = new WebSocketStream(`${BASEURL}/delayed-passive-close`);
const { writable } = await wss.connection;
const startTime = performance.now();
await writable.getWriter().close();
const elapsed = performance.now() - startTime;
const jitterAllowance = 100;
assert_greater_than_equal(elapsed, 1000 - jitterAllowance,
'one second should have elapsed');
}, 'writer close() promise should not resolve until handshake completes');
const abortOrCancel = [
{
method: 'abort',
voweling: 'aborting',
stream: 'writable',
},
{
method: 'cancel',
voweling: 'canceling',
stream: 'readable',
},
];
for (const { method, voweling, stream } of abortOrCancel) {
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const info = await wss.connection;
info[stream][method]();
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
assert_equals(reason, '', 'reason should be empty');
}, `${voweling} the ${stream} should result in a clean close`);
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const info = await wss.connection;
info[stream][method]({ code: 3333 });
const { code, reason } = await wss.closed;
assert_equals(code, 3333, 'code should be used');
assert_equals(reason, '', 'reason should be empty');
}, `${voweling} the ${stream} with a code should send that code`);
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const info = await wss.connection;
info[stream][method]({ code: 3456, reason: 'set' });
const { code, reason } = await wss.closed;
assert_equals(code, 3456, 'code should be used');
assert_equals(reason, 'set', 'reason should be used');
}, `${voweling} the ${stream} with a code and reason should use them`);
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const info = await wss.connection;
info[stream][method]({ reason: 'specified' });
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
assert_equals(reason, '', 'reason should be empty');
}, `${voweling} the ${stream} with a reason but no code should be ignored`);
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const info = await wss.connection;
info[stream][method]({ code: 999 });
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
assert_equals(reason, '', 'reason should be empty');
}, `${voweling} the ${stream} with an invalid code should be ignored`);
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const info = await wss.connection;
info[stream][method]({ code: 1000, reason: 'x'.repeat(128) });
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
assert_equals(reason, '', 'reason should be empty');
}, `${voweling} the ${stream} with an invalid reason should be ignored`);
// DOMExceptions are only ignored because the |code| attribute is too small to
// be a valid WebSocket close code.
promise_test(async () => {
const wss = new WebSocketStream(ECHOURL);
const info = await wss.connection;
info[stream][method](new DOMException('yes', 'DataCloneError'));
const { code, reason } = await wss.closed;
assert_equals(code, 1005, 'code should be unset');
assert_equals(reason, '', 'reason should be empty');
}, `${voweling} the ${stream} with a DOMException should be ignored`);
}