Bug 1444680: Part 1a: Move proxy filter host stubs to head.js. r?mixedpuppy
MozReview-Commit-ID: 8XxxJlRgTZL
--- a/toolkit/components/extensions/test/xpcshell/head.js
+++ b/toolkit/components/extensions/test/xpcshell/head.js
@@ -15,16 +15,20 @@ XPCOMUtils.defineLazyModuleGetters(this,
ExtensionParent: "resource://gre/modules/ExtensionParent.jsm",
ExtensionTestUtils: "resource://testing-common/ExtensionXPCShellUtils.jsm",
FileUtils: "resource://gre/modules/FileUtils.jsm",
HttpServer: "resource://testing-common/httpd.js",
NetUtil: "resource://gre/modules/NetUtil.jsm",
Schemas: "resource://gre/modules/Schemas.jsm",
});
+XPCOMUtils.defineLazyServiceGetter(this, "proxyService",
+ "@mozilla.org/network/protocol-proxy-service;1",
+ "nsIProtocolProxyService");
+
// These values may be changed in later head files and tested in check_remote
// below.
Services.prefs.setBoolPref("extensions.webextensions.remote", false);
const testEnv = {
expectRemote: false,
};
add_task(function check_remote() {
@@ -34,26 +38,56 @@ add_task(function check_remote() {
ExtensionTestUtils.init(this);
/**
* Creates a new HttpServer for testing, and begins listening on the
* specified port. Automatically shuts down the server when the test
* unit ends.
*
- * @param {integer} [port]
+ * @param {object} [options = {}]
+ * @param {integer} [options.port = -1]
* The port to listen on. If omitted, listen on a random
* port. The latter is the preferred behavior.
+ * @param {Set?} [options.hosts = null]
+ * A set of hosts to accept connections to. Support for this is
+ * implemented using a proxy filter.
*
* @returns {HttpServer}
*/
-function createHttpServer(port = -1) {
+function createHttpServer({port = -1, hosts} = {}) {
let server = new HttpServer();
server.start(port);
+ if (hosts) {
+ const serverHost = "localhost";
+ const serverPort = server.identity.primaryPort;
+
+ for (let host of hosts) {
+ server.identity.add("http", host, 80);
+ }
+
+ const proxyFilter = {
+ proxyInfo: proxyService.newProxyInfo("http", serverHost, serverPort, 0, 4096, null),
+
+ applyFilter(service, channel, defaultProxyInfo, callback) {
+ if (hosts.has(channel.URI.host)) {
+ callback.onProxyFilterResult(this.proxyInfo);
+ } else {
+ callback.onProxyFilterResult(defaultProxyInfo);
+ }
+ },
+ };
+
+ proxyService.registerChannelFilter(proxyFilter, 0);
+ registerCleanupFunction(() => {
+ proxyService.unregisterChannelFilter(proxyFilter);
+ });
+ }
+
registerCleanupFunction(() => {
return new Promise(resolve => {
server.stop(resolve);
});
});
return server;
}
--- a/toolkit/components/extensions/test/xpcshell/test_ext_webRequest_filterResponseData.js
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_webRequest_filterResponseData.js
@@ -1,44 +1,17 @@
"use strict";
-XPCOMUtils.defineLazyServiceGetter(this, "proxyService",
- "@mozilla.org/network/protocol-proxy-service;1",
- "nsIProtocolProxyService");
-
-const server = createHttpServer();
-const gHost = "localhost";
-const gPort = server.identity.primaryPort;
-
const HOSTS = new Set([
"example.com",
"example.org",
"example.net",
]);
-for (let host of HOSTS) {
- server.identity.add("http", host, 80);
-}
-
-const proxyFilter = {
- proxyInfo: proxyService.newProxyInfo("http", gHost, gPort, 0, 4096, null),
-
- applyFilter(service, channel, defaultProxyInfo, callback) {
- if (HOSTS.has(channel.URI.host)) {
- callback.onProxyFilterResult(this.proxyInfo);
- } else {
- callback.onProxyFilterResult(defaultProxyInfo);
- }
- },
-};
-
-proxyService.registerChannelFilter(proxyFilter, 0);
-registerCleanupFunction(() => {
- proxyService.unregisterChannelFilter(proxyFilter);
-});
+const server = createHttpServer({hosts: HOSTS});
server.registerPathHandler("/redirect", (request, response) => {
let params = new URLSearchParams(request.queryString);
response.setStatusLine(request.httpVersion, 302, "Moved Temporarily");
response.setHeader("Location", params.get("redirect_uri"));
response.setHeader("Access-Control-Allow-Origin", "*");
});