-
Notifications
You must be signed in to change notification settings - Fork 3.1k
/
general.any.js
572 lines (416 loc) · 18 KB
/
general.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
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
// META: timeout=long
// META: global=window,worker
// META: script=/common/utils.js
// META: script=/common/get-host-info.sub.js
// META: script=../request/request-error.js
const BODY_METHODS = ['arrayBuffer', 'blob', 'bytes', 'formData', 'json', 'text'];
const error1 = new Error('error1');
error1.name = 'error1';
// This is used to close connections that weren't correctly closed during the tests,
// otherwise you can end up running out of HTTP connections.
let requestAbortKeys = [];
function abortRequests() {
const keys = requestAbortKeys;
requestAbortKeys = [];
return Promise.all(
keys.map(key => fetch(`../resources/stash-put.py?key=${key}&value=close`))
);
}
const hostInfo = get_host_info();
const urlHostname = hostInfo.REMOTE_HOST;
promise_test(async t => {
const controller = new AbortController();
const signal = controller.signal;
controller.abort();
const fetchPromise = fetch('../resources/data.json', { signal });
await promise_rejects_dom(t, "AbortError", fetchPromise);
}, "Aborting rejects with AbortError");
promise_test(async t => {
const controller = new AbortController();
const signal = controller.signal;
controller.abort(error1);
const fetchPromise = fetch('../resources/data.json', { signal });
await promise_rejects_exactly(t, error1, fetchPromise, 'fetch() should reject with abort reason');
}, "Aborting rejects with abort reason");
promise_test(async t => {
const controller = new AbortController();
const signal = controller.signal;
controller.abort();
const url = new URL('../resources/data.json', location);
url.hostname = urlHostname;
const fetchPromise = fetch(url, {
signal,
mode: 'no-cors'
});
await promise_rejects_dom(t, "AbortError", fetchPromise);
}, "Aborting rejects with AbortError - no-cors");
// Test that errors thrown from the request constructor take priority over abort errors.
// badRequestArgTests is from response-error.js
for (const { args, testName } of badRequestArgTests) {
promise_test(async t => {
try {
// If this doesn't throw, we'll effectively skip the test.
// It'll fail properly in ../request/request-error.html
new Request(...args);
}
catch (err) {
const controller = new AbortController();
controller.abort();
// Add signal to 2nd arg
args[1] = args[1] || {};
args[1].signal = controller.signal;
await promise_rejects_js(t, TypeError, fetch(...args));
}
}, `TypeError from request constructor takes priority - ${testName}`);
}
test(() => {
const request = new Request('');
assert_true(Boolean(request.signal), "Signal member is present & truthy");
assert_equals(request.signal.constructor, AbortSignal);
}, "Request objects have a signal property");
promise_test(async t => {
const controller = new AbortController();
const signal = controller.signal;
controller.abort();
const request = new Request('../resources/data.json', { signal });
assert_true(Boolean(request.signal), "Signal member is present & truthy");
assert_equals(request.signal.constructor, AbortSignal);
assert_not_equals(request.signal, signal, 'Request has a new signal, not a reference');
assert_true(request.signal.aborted, `Request's signal has aborted`);
const fetchPromise = fetch(request);
await promise_rejects_dom(t, "AbortError", fetchPromise);
}, "Signal on request object");
promise_test(async t => {
const controller = new AbortController();
const signal = controller.signal;
controller.abort(error1);
const request = new Request('../resources/data.json', { signal });
assert_not_equals(request.signal, signal, 'Request has a new signal, not a reference');
assert_true(request.signal.aborted, `Request's signal has aborted`);
assert_equals(request.signal.reason, error1, `Request's signal's abort reason is error1`);
const fetchPromise = fetch(request);
await promise_rejects_exactly(t, error1, fetchPromise, "fetch() should reject with abort reason");
}, "Signal on request object should also have abort reason");
promise_test(async t => {
const controller = new AbortController();
const signal = controller.signal;
controller.abort();
const request = new Request('../resources/data.json', { signal });
const requestFromRequest = new Request(request);
const fetchPromise = fetch(requestFromRequest);
await promise_rejects_dom(t, "AbortError", fetchPromise);
}, "Signal on request object created from request object");
promise_test(async t => {
const controller = new AbortController();
const signal = controller.signal;
controller.abort();
const request = new Request('../resources/data.json');
const requestFromRequest = new Request(request, { signal });
const fetchPromise = fetch(requestFromRequest);
await promise_rejects_dom(t, "AbortError", fetchPromise);
}, "Signal on request object created from request object, with signal on second request");
promise_test(async t => {
const controller = new AbortController();
const signal = controller.signal;
controller.abort();
const request = new Request('../resources/data.json', { signal: new AbortController().signal });
const requestFromRequest = new Request(request, { signal });
const fetchPromise = fetch(requestFromRequest);
await promise_rejects_dom(t, "AbortError", fetchPromise);
}, "Signal on request object created from request object, with signal on second request overriding another");
promise_test(async t => {
const controller = new AbortController();
const signal = controller.signal;
controller.abort();
const request = new Request('../resources/data.json', { signal });
const fetchPromise = fetch(request, {method: 'POST'});
await promise_rejects_dom(t, "AbortError", fetchPromise);
}, "Signal retained after unrelated properties are overridden by fetch");
promise_test(async t => {
const controller = new AbortController();
const signal = controller.signal;
controller.abort();
const request = new Request('../resources/data.json', { signal });
const data = await fetch(request, { signal: null }).then(r => r.json());
assert_equals(data.key, 'value', 'Fetch fully completes');
}, "Signal removed by setting to null");
promise_test(async t => {
const controller = new AbortController();
const signal = controller.signal;
controller.abort();
const log = [];
await Promise.all([
fetch('../resources/data.json', { signal }).then(
() => assert_unreached("Fetch must not resolve"),
() => log.push('fetch-reject')
),
Promise.resolve().then(() => log.push('next-microtask'))
]);
assert_array_equals(log, ['fetch-reject', 'next-microtask']);
}, "Already aborted signal rejects immediately");
promise_test(async t => {
const controller = new AbortController();
const signal = controller.signal;
controller.abort();
const request = new Request('../resources/data.json', {
signal,
method: 'POST',
body: 'foo',
headers: { 'Content-Type': 'text/plain' }
});
await fetch(request).catch(() => {});
assert_true(request.bodyUsed, "Body has been used");
}, "Request is still 'used' if signal is aborted before fetching");
for (const bodyMethod of BODY_METHODS) {
promise_test(async t => {
const controller = new AbortController();
const signal = controller.signal;
const log = [];
const response = await fetch('../resources/data.json', { signal });
controller.abort();
const bodyPromise = response[bodyMethod]();
await Promise.all([
bodyPromise.catch(() => log.push(`${bodyMethod}-reject`)),
Promise.resolve().then(() => log.push('next-microtask'))
]);
await promise_rejects_dom(t, "AbortError", bodyPromise);
assert_array_equals(log, [`${bodyMethod}-reject`, 'next-microtask']);
}, `response.${bodyMethod}() rejects if already aborted`);
}
promise_test(async (t) => {
const controller = new AbortController();
const signal = controller.signal;
const res = await fetch('../resources/data.json', { signal });
controller.abort();
await promise_rejects_dom(t, 'AbortError', res.text());
await promise_rejects_dom(t, 'AbortError', res.text());
}, 'Call text() twice on aborted response');
promise_test(async t => {
await abortRequests();
const controller = new AbortController();
const signal = controller.signal;
const stateKey = token();
const abortKey = token();
requestAbortKeys.push(abortKey);
controller.abort();
await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, { signal }).catch(() => {});
// I'm hoping this will give the browser enough time to (incorrectly) make the request
// above, if it intends to.
await fetch('../resources/data.json').then(r => r.json());
const response = await fetch(`../resources/stash-take.py?key=${stateKey}`);
const data = await response.json();
assert_equals(data, null, "Request hasn't been made to the server");
}, "Already aborted signal does not make request");
promise_test(async t => {
await abortRequests();
const controller = new AbortController();
const signal = controller.signal;
controller.abort();
const fetches = [];
for (let i = 0; i < 3; i++) {
const abortKey = token();
requestAbortKeys.push(abortKey);
fetches.push(
fetch(`../resources/infinite-slow-response.py?${i}&abortKey=${abortKey}`, { signal })
);
}
for (const fetchPromise of fetches) {
await promise_rejects_dom(t, "AbortError", fetchPromise);
}
}, "Already aborted signal can be used for many fetches");
promise_test(async t => {
await abortRequests();
const controller = new AbortController();
const signal = controller.signal;
await fetch('../resources/data.json', { signal }).then(r => r.json());
controller.abort();
const fetches = [];
for (let i = 0; i < 3; i++) {
const abortKey = token();
requestAbortKeys.push(abortKey);
fetches.push(
fetch(`../resources/infinite-slow-response.py?${i}&abortKey=${abortKey}`, { signal })
);
}
for (const fetchPromise of fetches) {
await promise_rejects_dom(t, "AbortError", fetchPromise);
}
}, "Signal can be used to abort other fetches, even if another fetch succeeded before aborting");
promise_test(async t => {
await abortRequests();
const controller = new AbortController();
const signal = controller.signal;
const stateKey = token();
const abortKey = token();
requestAbortKeys.push(abortKey);
await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, { signal });
const beforeAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());
assert_equals(beforeAbortResult, "open", "Connection is open");
controller.abort();
// The connection won't close immediately, but it should close at some point:
const start = Date.now();
while (true) {
// Stop spinning if 10 seconds have passed
if (Date.now() - start > 10000) throw Error('Timed out');
const afterAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());
if (afterAbortResult == 'closed') break;
}
}, "Underlying connection is closed when aborting after receiving response");
promise_test(async t => {
await abortRequests();
const controller = new AbortController();
const signal = controller.signal;
const stateKey = token();
const abortKey = token();
requestAbortKeys.push(abortKey);
const url = new URL(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, location);
url.hostname = urlHostname;
await fetch(url, {
signal,
mode: 'no-cors'
});
const stashTakeURL = new URL(`../resources/stash-take.py?key=${stateKey}`, location);
stashTakeURL.hostname = urlHostname;
const beforeAbortResult = await fetch(stashTakeURL).then(r => r.json());
assert_equals(beforeAbortResult, "open", "Connection is open");
controller.abort();
// The connection won't close immediately, but it should close at some point:
const start = Date.now();
while (true) {
// Stop spinning if 10 seconds have passed
if (Date.now() - start > 10000) throw Error('Timed out');
const afterAbortResult = await fetch(stashTakeURL).then(r => r.json());
if (afterAbortResult == 'closed') break;
}
}, "Underlying connection is closed when aborting after receiving response - no-cors");
for (const bodyMethod of BODY_METHODS) {
promise_test(async t => {
await abortRequests();
const controller = new AbortController();
const signal = controller.signal;
const stateKey = token();
const abortKey = token();
requestAbortKeys.push(abortKey);
const response = await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, { signal });
const beforeAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());
assert_equals(beforeAbortResult, "open", "Connection is open");
const bodyPromise = response[bodyMethod]();
controller.abort();
await promise_rejects_dom(t, "AbortError", bodyPromise);
const start = Date.now();
while (true) {
// Stop spinning if 10 seconds have passed
if (Date.now() - start > 10000) throw Error('Timed out');
const afterAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());
if (afterAbortResult == 'closed') break;
}
}, `Fetch aborted & connection closed when aborted after calling response.${bodyMethod}()`);
}
promise_test(async t => {
await abortRequests();
const controller = new AbortController();
const signal = controller.signal;
const stateKey = token();
const abortKey = token();
requestAbortKeys.push(abortKey);
const response = await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, { signal });
const reader = response.body.getReader();
controller.abort();
await promise_rejects_dom(t, "AbortError", reader.read());
await promise_rejects_dom(t, "AbortError", reader.closed);
// The connection won't close immediately, but it should close at some point:
const start = Date.now();
while (true) {
// Stop spinning if 10 seconds have passed
if (Date.now() - start > 10000) throw Error('Timed out');
const afterAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());
if (afterAbortResult == 'closed') break;
}
}, "Stream errors once aborted. Underlying connection closed.");
promise_test(async t => {
await abortRequests();
const controller = new AbortController();
const signal = controller.signal;
const stateKey = token();
const abortKey = token();
requestAbortKeys.push(abortKey);
const response = await fetch(`../resources/infinite-slow-response.py?stateKey=${stateKey}&abortKey=${abortKey}`, { signal });
const reader = response.body.getReader();
await reader.read();
controller.abort();
await promise_rejects_dom(t, "AbortError", reader.read());
await promise_rejects_dom(t, "AbortError", reader.closed);
// The connection won't close immediately, but it should close at some point:
const start = Date.now();
while (true) {
// Stop spinning if 10 seconds have passed
if (Date.now() - start > 10000) throw Error('Timed out');
const afterAbortResult = await fetch(`../resources/stash-take.py?key=${stateKey}`).then(r => r.json());
if (afterAbortResult == 'closed') break;
}
}, "Stream errors once aborted, after reading. Underlying connection closed.");
promise_test(async t => {
await abortRequests();
const controller = new AbortController();
const signal = controller.signal;
const response = await fetch(`../resources/empty.txt`, { signal });
// Read whole response to ensure close signal has sent.
await response.clone().text();
const reader = response.body.getReader();
controller.abort();
const item = await reader.read();
assert_true(item.done, "Stream is done");
}, "Stream will not error if body is empty. It's closed with an empty queue before it errors.");
promise_test(async t => {
const controller = new AbortController();
const signal = controller.signal;
controller.abort();
let cancelReason;
const body = new ReadableStream({
pull(controller) {
controller.enqueue(new Uint8Array([42]));
},
cancel(reason) {
cancelReason = reason;
}
});
const fetchPromise = fetch('../resources/empty.txt', {
body, signal,
method: 'POST',
duplex: 'half',
headers: {
'Content-Type': 'text/plain'
}
});
assert_true(!!cancelReason, 'Cancel called sync');
assert_equals(cancelReason.constructor, DOMException);
assert_equals(cancelReason.name, 'AbortError');
await promise_rejects_dom(t, "AbortError", fetchPromise);
const fetchErr = await fetchPromise.catch(e => e);
assert_equals(cancelReason, fetchErr, "Fetch rejects with same error instance");
}, "Readable stream synchronously cancels with AbortError if aborted before reading");
test(() => {
const controller = new AbortController();
const signal = controller.signal;
controller.abort();
const request = new Request('.', { signal });
const requestSignal = request.signal;
const clonedRequest = request.clone();
assert_equals(requestSignal, request.signal, "Original request signal the same after cloning");
assert_true(request.signal.aborted, "Original request signal aborted");
assert_not_equals(clonedRequest.signal, request.signal, "Cloned request has different signal");
assert_true(clonedRequest.signal.aborted, "Cloned request signal aborted");
}, "Signal state is cloned");
test(() => {
const controller = new AbortController();
const signal = controller.signal;
const request = new Request('.', { signal });
const clonedRequest = request.clone();
const log = [];
request.signal.addEventListener('abort', () => log.push('original-aborted'));
clonedRequest.signal.addEventListener('abort', () => log.push('clone-aborted'));
controller.abort();
assert_array_equals(log, ['original-aborted', 'clone-aborted'], "Abort events fired in correct order");
assert_true(request.signal.aborted, 'Signal aborted');
assert_true(clonedRequest.signal.aborted, 'Signal aborted');
}, "Clone aborts with original controller");