fune/toolkit/components/extensions/test/xpcshell/test_proxy_scripts.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

167 lines
4.8 KiB
JavaScript

"use strict";
/* eslint no-unused-vars: ["error", {"args": "none", "varsIgnorePattern": "^(FindProxyForURL)$"}] */
XPCOMUtils.defineLazyServiceGetter(this, "gProxyService",
"@mozilla.org/network/protocol-proxy-service;1",
"nsIProtocolProxyService");
function getProxyInfo() {
return new Promise((resolve, reject) => {
let channel = NetUtil.newChannel({
uri: "http://www.mozilla.org/",
loadUsingSystemPrincipal: true,
});
gProxyService.asyncResolve(channel, 0, {
onProxyAvailable(req, uri, pi, status) {
resolve(pi);
},
});
});
}
async function testProxyScript(script, expected = {}) {
let scriptData = String(script).replace(/^.*?\{([^]*)\}$/, "$1");
let extensionData = {
manifest: {
"permissions": ["proxy"],
},
background() {
// Some tests generate multiple errors, we'll just rely on the first.
let seenError = false;
browser.proxy.onProxyError.addListener(error => {
if (!seenError) {
browser.test.sendMessage("proxy-error-received", error);
seenError = true;
}
});
browser.test.onMessage.addListener(msg => {
if (msg === "unregister-proxy-script") {
browser.proxy.unregister().then(() => {
browser.test.notifyPass("proxy");
});
}
});
browser.proxy.register("proxy.js").then(() => {
browser.test.sendMessage("ready");
});
},
files: {
"proxy.js": scriptData,
},
};
let extension = ExtensionTestUtils.loadExtension(extensionData);
await extension.startup();
await extension.awaitMessage("ready");
let errorWait = extension.awaitMessage("proxy-error-received");
let proxyInfo = await getProxyInfo();
let error = await errorWait;
equal(error.message, expected.message, "Correct error message received");
if (!expected.proxyInfo) {
equal(proxyInfo, null, "no proxyInfo received");
} else {
let {host, port, type} = expected.proxyInfo;
equal(proxyInfo.host, host, `Expected proxy host to be ${host}`);
equal(proxyInfo.port, port, `Expected proxy port to be ${port}`);
equal(proxyInfo.type, type, `Expected proxy type to be ${type}`);
}
if (expected.errorInfo) {
ok(error.fileName.includes("proxy.js"), "Error should include file name");
equal(error.lineNumber, 3, "Error should include line number");
ok(error.stack.includes("proxy.js:3:7"), "Error should include stack trace");
}
extension.sendMessage("unregister-proxy-script");
await extension.awaitFinish("proxy");
await extension.unload();
}
add_task(async function test_invalid_FindProxyForURL_function() {
await testProxyScript(() => { }, {
message: "The proxy script must define FindProxyForURL as a function",
});
await testProxyScript(() => {
var FindProxyForURL = 5; // eslint-disable-line mozilla/var-only-at-top-level
}, {
message: "The proxy script must define FindProxyForURL as a function",
});
await testProxyScript(() => {
function FindProxyForURL() {
return not_defined; // eslint-disable-line no-undef
}
}, {
message: "not_defined is not defined",
errorInfo: true,
});
// The following tests will produce multiple errors.
await testProxyScript(() => {
function FindProxyForURL() {
return ";;;;;PROXY 1.2.3.4:8080";
}
}, {
message: "ProxyInfoData: Missing Proxy Rule",
});
// We take any valid proxy up to the error.
await testProxyScript(() => {
function FindProxyForURL() {
return "PROXY 1.2.3.4:8080; UNEXPECTED; SOCKS 1.2.3.4:8080";
}
}, {
message: "ProxyInfoData: Unrecognized proxy type: \"unexpected\"",
proxyInfo: {
host: "1.2.3.4",
port: "8080",
type: "http",
failoverProxy: null,
},
});
});
async function getExtension(proxyResult) {
let extensionData = {
manifest: {
"permissions": ["proxy"],
},
background() {
browser.proxy.register("proxy.js").then(() => {
browser.test.sendMessage("ready");
});
},
files: {
"proxy.js": `
function FindProxyForURL(url, host) {
return ${proxyResult};
}`,
},
};
let extension = ExtensionTestUtils.loadExtension(extensionData);
await extension.startup();
await extension.awaitMessage("ready");
return extension;
}
add_task(async function test_passthrough() {
let ext1 = await getExtension(null);
let ext2 = await getExtension("\"PROXY 1.2.3.4:8888\"");
let proxyInfo = await getProxyInfo();
equal(proxyInfo.host, "1.2.3.4", `second extension won`);
equal(proxyInfo.port, "8888", `second extension won`);
equal(proxyInfo.type, "http", `second extension won`);
await ext2.unload();
proxyInfo = await getProxyInfo();
equal(proxyInfo, null, `expected no proxy`);
await ext1.unload();
});