Bug 1557386 - Testing CORS content and errors for beacon. r=ckerschb

Differential Revision: https://phabricator.services.mozilla.com/D75804
This commit is contained in:
Jonathan Kingston 2020-05-21 10:17:44 +00:00
Родитель 3686adfd13
Коммит 04df574987
4 изменённых файлов: 124 добавлений и 0 удалений

Просмотреть файл

@ -55,6 +55,13 @@ function handleRequest(request, response) {
response.setHeader("Content-Type", "application/json", false); response.setHeader("Content-Type", "application/json", false);
switch (request.queryString) { switch (request.queryString) {
case "getLastBeacon": case "getLastBeacon":
// Allow CORS responses of the last beacon
var originHeader = request.getHeader("origin");
response.setHeader("Access-Control-Allow-Headers", "content-type", false);
response.setHeader("Access-Control-Allow-Methods", "POST, GET", false);
response.setHeader("Access-Control-Allow-Origin", originHeader, false);
response.setHeader("Access-Control-Allow-Credentials", "true", false);
var state = getOurState(); var state = getOurState();
if (state === "unblocked") { if (state === "unblocked") {
finishControlResponse(response); finishControlResponse(response);

Просмотреть файл

@ -5,3 +5,6 @@ skip-if = os == 'android'
skip-if = (verify && !debug && (os == 'win')) skip-if = (verify && !debug && (os == 'win'))
support-files = beacon-set-cookie.sjs support-files = beacon-set-cookie.sjs
file_beaconCookies.html file_beaconCookies.html
[test_beaconWithSafelistedContentType.html]
support-files = beacon-handler.sjs
file_beaconSafelist.html

Просмотреть файл

@ -0,0 +1,11 @@
<!DOCTYPE HTML>
<script class="testbody" type="text/javascript">
var beaconUrl = "http://example.com/tests/dom/tests/mochitest/beacon/beacon-handler.sjs?beacon";
var value = ["text"];
var blob = new Blob(value, {type: "application/x-www-form-urlencoded"});
navigator.sendBeacon(beaconUrl, blob);
var intervalID = null;
</script>

Просмотреть файл

@ -0,0 +1,103 @@
<!DOCTYPE HTML>
<html>
<!--
https://bugzilla.mozilla.org/show_bug.cgi?id=1557386
-->
<head>
<title>Test for Bug 1557386</title>
<script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
<link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css" />
</head>
<body>
<a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1557386">Mozilla Bug 1557386</a>
<p id="display"></p>
<div id="content" style="display: none">
</div>
<pre id="test">
<script class="testbody" type="text/javascript">
// not enabled by default yet.
SimpleTest.waitForExplicitFinish();
SpecialPowers.pushPrefEnv({'set': [["beacon.enabled", true]]}, beginTest);
const {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");
const {BrowserTestUtils} = ChromeUtils.import("resource://testing-common/BrowserTestUtils.jsm");
const { clearInterval, setInterval } = ChromeUtils.import(
"resource://gre/modules/Timer.jsm"
);
function whenDelayedStartupFinished(aWindow, aCallback) {
Services.obs.addObserver(function observer(aSubject, aTopic) {
if (aWindow == aSubject) {
Services.obs.removeObserver(observer, aTopic);
setTimeout(aCallback, 0);
}
}, "browser-delayed-startup-finished");
}
let baseURL = "http://mochi.test:8888/chrome/dom/tests/mochitest/beacon/";
let windowsToClose = [];
let listener = msg => {
if (!(msg instanceof Ci.nsIConsoleMessage)) {
return;
}
if (msg.message.match("Cross-Origin Request Blocked:")) {
ok(false, "CORS response console warning found");
}
};
function beginTest() {
Services.console.registerListener(listener);
let mainWindow = window.docShell.rootTreeItem.domWindow;
let win = mainWindow.OpenBrowserWindow({});
windowsToClose.push(win);
whenDelayedStartupFinished(win, async function() {
Services.obs.addObserver(function waitCookie() {
Services.obs.removeObserver(waitCookie, "cookie-changed");
ok(true, "cookie set by beacon request in normal window");
testOnPrivateWindow();
}, "cookie-changed");
let testURL = baseURL + "file_beaconSafelist.html";
await BrowserTestUtils.loadURI(win.gBrowser.selectedBrowser, testURL);
await BrowserTestUtils.browserLoaded(win.gBrowser.selectedBrowser, false, testURL);
await SpecialPowers.spawn(win.gBrowser.selectedBrowser, [], async () => {
await new Promise((resolve, reject) => {
let intervalId;
function queryIfBeaconSucceeded() {
content.clearInterval(intervalId);
let xhr = new content.XMLHttpRequest();
let baseURL = "http://example.com/tests/dom/tests/mochitest/beacon/";
let checkUrl = baseURL + "beacon-handler.sjs?getLastBeacon";
xhr.open("GET", checkUrl, true);
xhr.onload = function() {
let res = JSON.parse(xhr.responseText);
is(res.data, "text", "Got correct data");
resolve();
};
xhr.onerror = function() {
ok(false, "Error getting last beacon");
reject();
};
xhr.send();
}
intervalID = content.setInterval(queryIfBeaconSucceeded, 2000);
});
});
cleanup();
});
}
function cleanup() {
Services.console.unregisterListener(listener);
for (let i = 0; i < windowsToClose.length; ++i) {
windowsToClose[i].close();
}
SimpleTest.finish();
}
</script>
</pre>
</body>
</html>