Revision 08c935c755a5f9cc0ef8a7b9f56091e5d2b040d9 authored by Kent Tamura on 13 April 2018, 04:39:21 UTC, committed by Blink WPT Bot on 13 April 2018, 05:02:00 UTC
Intent-to-implement-and-ship: https://groups.google.com/a/chromium.org/forum/#!topic/blink-dev/zCQe7UkR07w

Bug: 819482
Change-Id: I5645d5711d7fe1323992533e6c73593ba59d1bca
Reviewed-on: https://chromium-review.googlesource.com/1009450
Reviewed-by: Hayato Ito <hayato@chromium.org>
Commit-Queue: Kent Tamura <tkent@chromium.org>
Cr-Commit-Position: refs/heads/master@{#550513}
1 parent bdcafae
Raw File
beacon-error.window.js
// META: script=/common/utils.js
// META: script=beacon-common.sub.js

"use strict";

test(function() {
    // Payload that should cause sendBeacon to return false because it exceeds the maximum payload size.
    var exceedPayload = Array(maxPayloadSize + 1).fill('z').join("");

    var success = navigator.sendBeacon("http://doesnotmatter", exceedPayload);
    assert_false(success, "calling 'navigator.sendBeacon()' with payload size exceeding the maximum size must fail");
}, "Verify calling 'navigator.sendBeacon()' with a large payload returns 'false'.");

test(function() {
    var invalidUrl = "http://invalid:url";
    assert_throws(new TypeError(), function() { navigator.sendBeacon(invalidUrl, smallPayload); },
        `calling 'navigator.sendBeacon()' with an invalid URL '${invalidUrl}' must throw a TypeError`);
}, "Verify calling 'navigator.sendBeacon()' with an invalid URL throws an exception.");

test(function() {
    var invalidUrl = "nothttp://invalid.url";
    assert_throws(new TypeError(), function() { navigator.sendBeacon(invalidUrl, smallPayload); },
         `calling 'navigator.sendBeacon()' with a non-http(s) URL '${invalidUrl}' must throw a TypeError`);
}, "Verify calling 'navigator.sendBeacon()' with a URL that is not a http(s) scheme throws an exception.");

// 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.
promise_test(async () => {
    function wait(ms) {
        return new Promise(res => step_timeout(res, ms));
    }
    const url = '/fetch/api/resources/trickle.py?count=1&ms=0';
    assert_true(navigator.sendBeacon(url, maxPayload),
                "calling 'navigator.sendBeacon()' with our max payload size should succeed.");

    // Now we'll send just one character.
    assert_false(navigator.sendBeacon(url, '1'),
                 "calling 'navigator.sendBeacon()' with just one char should fail while our Quota is used up.");

    for (let i = 0; i < 20; ++i) {
        await wait(100);
        if (navigator.sendBeacon(url, maxPayload)) {
           return;
        }
    }
    assert_unreached('The quota should recover after fetching.');
}, "Verify the behavior after the quota is exhausted.");

done();
back to top