Bug 1691109 [wpt PR 27513] - Increase timeout duration for wpt/fetch/api/basic/keepal...
[gecko.git] / testing / web-platform / tests / beacon / beacon-error.sub.window.js
blob499fa3b27213f2b5958c0c90cd76eaa382bb0bee
1 // META: script=/common/utils.js
2 // META: script=beacon-common.sub.js
4 "use strict";
6 test(function() {
7     // Payload that should cause sendBeacon to return false because it exceeds the maximum payload size.
8     var exceedPayload = Array(maxPayloadSize + 1).fill('z').join("");
10     var success = navigator.sendBeacon("http://{{hosts[][nonexistent]}}", exceedPayload);
11     assert_false(success, "calling 'navigator.sendBeacon()' with payload size exceeding the maximum size must fail");
12 }, "Verify calling 'navigator.sendBeacon()' with a large payload returns 'false'.");
14 test(function() {
15     var invalidUrl = "http://invalid:url";
16     assert_throws_js(TypeError, function() { navigator.sendBeacon(invalidUrl, smallPayload); },
17         `calling 'navigator.sendBeacon()' with an invalid URL '${invalidUrl}' must throw a TypeError`);
18 }, "Verify calling 'navigator.sendBeacon()' with an invalid URL throws an exception.");
20 test(function() {
21     var invalidUrl = "nothttp://invalid.url";
22     assert_throws_js(TypeError, function() { navigator.sendBeacon(invalidUrl, smallPayload); },
23          `calling 'navigator.sendBeacon()' with a non-http(s) URL '${invalidUrl}' must throw a TypeError`);
24 }, "Verify calling 'navigator.sendBeacon()' with a URL that is not a http(s) scheme throws an exception.");
26 // We'll validate that we can send one beacon that uses our entire Quota and then fail to send one that is just one char.
27 promise_test(async () => {
28     function wait(ms) {
29         return new Promise(res => step_timeout(res, ms));
30     }
31     const url = '/fetch/api/resources/trickle.py?count=1&ms=0';
32     assert_true(navigator.sendBeacon(url, maxPayload),
33                 "calling 'navigator.sendBeacon()' with our max payload size should succeed.");
35     // Now we'll send just one character.
36     assert_false(navigator.sendBeacon(url, '1'),
37                  "calling 'navigator.sendBeacon()' with just one char should fail while our Quota is used up.");
39     for (let i = 0; i < 20; ++i) {
40         await wait(100);
41         if (navigator.sendBeacon(url, maxPayload)) {
42            return;
43         }
44     }
45     assert_unreached('The quota should recover after fetching.');
46 }, "Verify the behavior after the quota is exhausted.");
48 done();