1 // META: script=/common/utils.js
2 // META: script=beacon-common.sub.js
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'.");
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.");
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 () => {
29 return new Promise(res => step_timeout(res, ms));
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) {
41 if (navigator.sendBeacon(url, maxPayload)) {
45 assert_unreached('The quota should recover after fetching.');
46 }, "Verify the behavior after the quota is exhausted.");