<!DOCTYPE HTML>
<html>
<head>
<script src="/js-test-resources/js-test.js"></script>
<script>
description("Testing navigator.sendBeacon() within same origin.");

window.jsTestIsAsync = true;

var binary_array = new Uint32Array(10);
for (var i = 0; i < binary_array.length; i++) {
    binary_array[i] = 64 + i;
}

var blob = new Blob(["hello", " ", "world"], {type: "text/plain;from-beacon=true"});
var form = new FormData();
form.append("key", "value");
var searchParams = new URLSearchParams("a=b&c=d");

var tests = [
    "SameOrigin",
    binary_array,
    blob,
    form,
    searchParams];

var payload;
function testOne() {
    payload = tests.shift();
    if (!payload) {
        finishJSTest();
        return;
    }
    debug("Sending beacon with type: " + Object.prototype.toString.call(payload));
    shouldBeTrue('navigator.sendBeacon("resources/save-beacon.py?name=same-origin", payload);');
    var xhr = new XMLHttpRequest();
    xhr.open("GET", "resources/check-beacon.py?name=same-origin");
    xhr.onload = function () {
        var lines = xhr.responseText.split("\n");
        for (var i in lines)
            testPassed(lines[i]);
        testOne();
    };
    xhr.onerror = function () {
        testFailed("Unable to fetch beacon status");
        testOne();
    };
    xhr.send();
}

function test() {
    if (window.testRunner) {
        testRunner.dumpAsText();
        testRunner.waitUntilDone();
        //testRunner.dumpPingLoaderCallbacks();
    }
    testOne();
}
</script>
</head>
<body onload="test();">
</body>
</html>
