gecko-dev/toolkit/modules/tests/browser/browser_WebRequest.js
Kris Maglione e930b89c34 Bug 1514594: Part 3 - Change ChromeUtils.import API.
***
Bug 1514594: Part 3a - Change ChromeUtils.import to return an exports object; not pollute global. r=mccr8

This changes the behavior of ChromeUtils.import() to return an exports object,
rather than a module global, in all cases except when `null` is passed as a
second argument, and changes the default behavior not to pollute the global
scope with the module's exports. Thus, the following code written for the old
model:

  ChromeUtils.import("resource://gre/modules/Services.jsm");

is approximately the same as the following, in the new model:

  var {Services} = ChromeUtils.import("resource://gre/modules/Services.jsm");

Since the two behaviors are mutually incompatible, this patch will land with a
scripted rewrite to update all existing callers to use the new model rather
than the old.
***
Bug 1514594: Part 3b - Mass rewrite all JS code to use the new ChromeUtils.import API. rs=Gijs

This was done using the followng script:

https://bitbucket.org/kmaglione/m-c-rewrites/src/tip/processors/cu-import-exports.jsm
***
Bug 1514594: Part 3c - Update ESLint plugin for ChromeUtils.import API changes. r=Standard8

Differential Revision: https://phabricator.services.mozilla.com/D16747
***
Bug 1514594: Part 3d - Remove/fix hundreds of duplicate imports from sync tests. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16748
***
Bug 1514594: Part 3e - Remove no-op ChromeUtils.import() calls. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16749
***
Bug 1514594: Part 3f.1 - Cleanup various test corner cases after mass rewrite. r=Gijs
***
Bug 1514594: Part 3f.2 - Cleanup various non-test corner cases after mass rewrite. r=Gijs

Differential Revision: https://phabricator.services.mozilla.com/D16750

--HG--
extra : rebase_source : 359574ee3064c90f33bf36c2ebe3159a24cc8895
extra : histedit_source : b93c8f42808b1599f9122d7842d2c0b3e656a594%2C64a3a4e3359dc889e2ab2b49461bab9e27fc10a7
2019-01-17 10:18:31 -08:00

209 lines
7.7 KiB
JavaScript

"use strict";
var {WebRequest} = ChromeUtils.import("resource://gre/modules/WebRequest.jsm");
const BASE = "http://example.com/browser/toolkit/modules/tests/browser";
const URL = BASE + "/file_WebRequest_page1.html";
var expected_browser;
function checkType(details) {
let expected_type = "???";
if (details.url.includes("style")) {
expected_type = "stylesheet";
} else if (details.url.includes("image")) {
expected_type = "image";
} else if (details.url.includes("script")) {
expected_type = "script";
} else if (details.url.includes("page1")) {
expected_type = "main_frame";
} else if (/page2|_redirection\.|dummy_page/.test(details.url)) {
expected_type = "sub_frame";
} else if (details.url.includes("xhr")) {
expected_type = "xmlhttprequest";
}
is(details.type, expected_type, "resource type is correct");
}
var windowIDs = new Map();
var 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.includes("page2")) {
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");
is(details.frameAncestors.length, 1, "correctly has only one ancestor");
let ancestor = details.frameAncestors[0];
ok(ancestor.url.includes("page1"), "parent window url seems correct");
is(ancestor.frameId, page1id, "parent window id is correct");
}
}
if (details.url.includes("_bad.")) {
return {cancel: true};
}
return undefined;
}
var 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.includes("_redirect.")) {
return {redirectUrl: details.url.replace("_redirect.", "_good.")};
}
return undefined;
}
var beforeRedirect = [];
function onBeforeRedirect(details) {
info(`onBeforeRedirect ${details.url} -> ${details.redirectUrl}`);
checkType(details);
if (details.url.startsWith(BASE)) {
beforeRedirect.push(details.url);
is(details.browser, expected_browser, "correct <browser> element");
checkType(details);
let expectedUrl = details.url.replace("_redirect.", "_good.").replace(/\w+_redirection\..*/, "dummy_page.html");
is(details.redirectUrl, expectedUrl, "Correct redirectUrl value");
}
let id = windowIDs.get(details.url);
is(id, details.windowId, "window ID same in onBeforeRedirect as onBeforeRequest");
// associate stored windowId with final url
windowIDs.set(details.redirectUrl, details.windowId);
return {};
}
var 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 + "/WebRequest_redirection.sjs",
BASE + "/dummy_page.html",
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 + "/WebRequest_redirection.sjs",
BASE + "/dummy_page.html",
BASE + "/xhr_resource"];
const expected_beforeRedirect = expected_sendHeaders.filter(u => /_redirect\./.test(u))
.concat(BASE + "/WebRequest_redirection.sjs");
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 + "/dummy_page.html",
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`);
}
async function test_once() {
WebRequest.onBeforeRequest.addListener(onBeforeRequest, null, ["blocking"]);
WebRequest.onBeforeSendHeaders.addListener(onBeforeSendHeaders, null, ["blocking"]);
WebRequest.onBeforeRedirect.addListener(onBeforeRedirect);
WebRequest.onResponseStarted.addListener(onResponseStarted);
await BrowserTestUtils.withNewTab({ gBrowser, url: "about:blank" },
async function(browser) {
expected_browser = browser;
BrowserTestUtils.loadURI(browser, URL);
await BrowserTestUtils.browserLoaded(expected_browser);
expected_browser = null;
await ContentTask.spawn(browser, null, function() {
let win = content.wrappedJSObject;
is(win.success, 2, "Good script ran");
is(win.failure, undefined, "Failure script didn't run");
let style =
content.getComputedStyle(content.document.getElementById("test"));
is(style.getPropertyValue("color"), "rgb(255, 0, 0)", "Good CSS loaded");
});
});
compareLists(requested, expected_requested, "requested");
compareLists(sendHeaders, expected_sendHeaders, "sendHeaders");
compareLists(beforeRedirect, expected_beforeRedirect, "beforeRedirect");
compareLists(headersReceived, expected_headersReceived, "headersReceived");
WebRequest.onBeforeRequest.removeListener(onBeforeRequest);
WebRequest.onBeforeSendHeaders.removeListener(onBeforeSendHeaders);
WebRequest.onBeforeRedirect.removeListener(onBeforeRedirect);
WebRequest.onResponseStarted.removeListener(onResponseStarted);
}
// Run the test twice to make sure it works with caching.
add_task(test_once);
add_task(test_once);