Files
roytam1 ebd6e6dc19 import changes from `dev' branch of rmottola/Arctic-Fox:
- Bug 1174323 - Disable screenClientXYConst subtest of pointerlock test on OS X. rs=KWierso (2d0db6d1b)
- Bug 992096 - Implement Sub Resource Integrity [1/2]. r=baku,r=ckerschb (c30671ac0)
- Bug 992096 - Implement Sub Resource Integrity [2/2]. r=ckerschb (0afc64d88)
- Bug 1091883 - Added test, this is fixed by a fix to bug 1113438. r=sstamm CLOSED TREE (fd9a64b43)
- Bug 1196740 - Consider redirects when looking for SRI-eligibility. r=ckerschb (5c749cdc9)
- Bug 1202015 - Better document the SRI strings for translators. r=ckerschb (a7860e0fb)
- Bug 1202027 - Make SRI require CORS loads for cross-origin resources. r=ckerschb (ea451323d)
- bit of Bug 1202902 - Mass replace toplevel 'let' with 'var' (a6e8a587d)
- Bug 1208629 - Properly support data: and blob: URIs with an integrity atribute. r=ckerschb (6b2018fe4)
- Bug 1140129 - Don't clear tab title when location changes (r=Mossop) (ca1945ba8)
- Bug 1073462: Send synthetic property with Content:LocationChange message. r=felipe (1aa418acf)
- bug 1165017 - annotate content process URL on location change. r=mconley (cdca4fa75)
- Bug 1157561 - Add webRequest-like API to Firefox (r=Mossop) (546a57822)
- Bug 1163861 - Include windowID in all WebRequest notifications (r=Mossop) (c140af560)
- Bug 1171248 - Add MatchPattern support to WebRequest module (r=Mossop) (b09a05658)
2021-08-17 10:04:53 +08:00

189 lines
6.3 KiB
JavaScript

"use strict";
const { interfaces: Ci, classes: Cc, utils: Cu, results: Cr } = Components;
let {WebRequest} = Cu.import("resource://gre/modules/WebRequest.jsm", {});
const BASE = "http://example.com/browser/toolkit/modules/tests/browser";
const URL = BASE + "/file_WebRequest_page1.html";
let expected_browser;
function checkType(details)
{
let expected_type = "???";
if (details.url.indexOf("style") != -1) {
expected_type = "stylesheet";
} else if (details.url.indexOf("image") != -1) {
expected_type = "image";
} else if (details.url.indexOf("script") != -1) {
expected_type = "script";
} else if (details.url.indexOf("page1") != -1) {
expected_type = "main_frame";
} else if (details.url.indexOf("page2") != -1) {
expected_type = "sub_frame";
} else if (details.url.indexOf("xhr") != -1) {
expected_type = "xmlhttprequest";
}
is(details.type, expected_type, "resource type is correct");
}
let windowIDs = new Map();
let requested = [];
function onBeforeRequest(details)
{
info(`onBeforeRequest ${details.url}`);
if (details.url.startsWith(BASE)) {
requested.push(details.url);
is(details.browser, expected_browser, "correct <browser> element");
checkType(details);
windowIDs.set(details.url, details.windowId);
if (details.url.indexOf("page2") != -1) {
let page1id = windowIDs.get(URL);
ok(details.windowId != page1id, "sub-frame gets its own window ID");
is(details.parentWindowId, page1id, "parent window id is correct");
}
}
if (details.url.indexOf("_bad.") != -1) {
return {cancel: true};
}
}
let sendHeaders = [];
function onBeforeSendHeaders(details)
{
info(`onBeforeSendHeaders ${details.url}`);
if (details.url.startsWith(BASE)) {
sendHeaders.push(details.url);
is(details.browser, expected_browser, "correct <browser> element");
checkType(details);
let id = windowIDs.get(details.url);
is(id, details.windowId, "window ID same in onBeforeSendHeaders as onBeforeRequest");
}
if (details.url.indexOf("_redirect.") != -1) {
return {redirectUrl: details.url.replace("_redirect.", "_good.")};
}
}
let headersReceived = [];
function onResponseStarted(details)
{
if (details.url.startsWith(BASE)) {
headersReceived.push(details.url);
}
}
const expected_requested = [BASE + "/file_WebRequest_page1.html",
BASE + "/file_style_good.css",
BASE + "/file_style_bad.css",
BASE + "/file_style_redirect.css",
BASE + "/file_image_good.png",
BASE + "/file_image_bad.png",
BASE + "/file_image_redirect.png",
BASE + "/file_script_good.js",
BASE + "/file_script_bad.js",
BASE + "/file_script_redirect.js",
BASE + "/file_script_xhr.js",
BASE + "/file_WebRequest_page2.html",
BASE + "/nonexistent_script_url.js",
BASE + "/xhr_resource"];
const expected_sendHeaders = [BASE + "/file_WebRequest_page1.html",
BASE + "/file_style_good.css",
BASE + "/file_style_redirect.css",
BASE + "/file_image_good.png",
BASE + "/file_image_redirect.png",
BASE + "/file_script_good.js",
BASE + "/file_script_redirect.js",
BASE + "/file_script_xhr.js",
BASE + "/file_WebRequest_page2.html",
BASE + "/nonexistent_script_url.js",
BASE + "/xhr_resource"];
const expected_headersReceived = [BASE + "/file_WebRequest_page1.html",
BASE + "/file_style_good.css",
BASE + "/file_image_good.png",
BASE + "/file_script_good.js",
BASE + "/file_script_xhr.js",
BASE + "/file_WebRequest_page2.html",
BASE + "/nonexistent_script_url.js",
BASE + "/xhr_resource"];
function removeDupes(list)
{
let j = 0;
for (let i = 1; i < list.length; i++) {
if (list[i] != list[j]) {
j++;
if (i != j) {
list[j] = list[i];
}
}
}
list.length = j + 1;
}
function compareLists(list1, list2, kind)
{
list1.sort();
removeDupes(list1);
list2.sort();
removeDupes(list2);
is(String(list1), String(list2), `${kind} URLs correct`);
}
function* test_once()
{
WebRequest.onBeforeRequest.addListener(onBeforeRequest, null, ["blocking"]);
WebRequest.onBeforeSendHeaders.addListener(onBeforeSendHeaders, null, ["blocking"]);
WebRequest.onResponseStarted.addListener(onResponseStarted);
gBrowser.selectedTab = gBrowser.addTab();
let browser = gBrowser.selectedBrowser;
expected_browser = browser;
yield waitForLoad();
browser.messageManager.loadFrameScript(`data:,content.location = "${URL}";`, false);
yield waitForLoad();
let win = browser.contentWindow.wrappedJSObject;
is(win.success, 2, "Good script ran");
is(win.failure, undefined, "Failure script didn't run");
let style = browser.contentWindow.getComputedStyle(browser.contentDocument.getElementById("test"), null);
is(style.getPropertyValue("color"), "rgb(255, 0, 0)", "Good CSS loaded");
gBrowser.removeCurrentTab();
compareLists(requested, expected_requested, "requested");
compareLists(sendHeaders, expected_sendHeaders, "sendHeaders");
compareLists(headersReceived, expected_headersReceived, "headersReceived");
WebRequest.onBeforeRequest.removeListener(onBeforeRequest);
WebRequest.onBeforeSendHeaders.removeListener(onBeforeSendHeaders);
WebRequest.onResponseStarted.removeListener(onResponseStarted);
}
// Run the test twice to make sure it works with caching.
add_task(test_once);
add_task(test_once);
function waitForLoad(browser = gBrowser.selectedBrowser) {
return new Promise(resolve => {
browser.addEventListener("load", function listener() {
browser.removeEventListener("load", listener, true);
resolve();
}, true);
});
}