Bug 1467523 xpcshell tests for host header setting, r=aswan
authorShane Caraveo <scaraveo@mozilla.com>
Wed, 05 Dec 2018 09:02:34 +0000
changeset 510073 a13bd50f3cd449db176f17e6faecfe74c0238fc8
parent 510072 29429b3bef3eda44f50a2efd8361987286c3276a
child 510074 a36388b88fcc72e534ba79d282ca1741744602d9
push id10547
push userffxbld-merge
push dateMon, 21 Jan 2019 13:03:58 +0000
treeherdermozilla-beta@24ec1916bffe [default view] [failures only]
perfherder[talos] [build metrics] [platform microbench] (compared to previous push)
reviewersaswan
bugs1467523
milestone66.0a1
first release with
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
last release without
nightly linux32
nightly linux64
nightly mac
nightly win32
nightly win64
Bug 1467523 xpcshell tests for host header setting, r=aswan Depends on D7750 Differential Revision: https://phabricator.services.mozilla.com/D7751
toolkit/components/extensions/test/xpcshell/test_ext_webRequest_host.js
toolkit/components/extensions/test/xpcshell/xpcshell-common.ini
new file mode 100644
--- /dev/null
+++ b/toolkit/components/extensions/test/xpcshell/test_ext_webRequest_host.js
@@ -0,0 +1,86 @@
+"use strict";
+
+PromiseTestUtils.whitelistRejectionsGlobally(/Message manager disconnected/);
+
+const HOSTS = new Set([
+  "example.com",
+  "example.org",
+]);
+
+const server = createHttpServer({hosts: HOSTS});
+
+const BASE_URL = "http://example.com";
+const FETCH_ORIGIN = "http://example.com/dummy";
+
+server.registerPathHandler("/return_headers.sjs", (request, response) => {
+  response.setHeader("Content-Type", "text/plain", false);
+
+  let headers = {};
+  for (let {data: header} of request.headers) {
+    headers[header] = request.getHeader(header);
+  }
+
+  response.write(JSON.stringify(headers));
+});
+
+server.registerPathHandler("/dummy", (request, response) => {
+  response.setStatusLine(request.httpVersion, 200, "OK");
+  response.write("ok");
+});
+
+function getExtension(permission = "<all_urls>") {
+  return ExtensionTestUtils.loadExtension({
+    manifest: {
+      permissions: [
+        "webRequest",
+        "webRequestBlocking",
+        permission,
+      ],
+    },
+    background() {
+      browser.webRequest.onBeforeSendHeaders.addListener(details => {
+        details.requestHeaders.push({name: "Host", value: "example.org"});
+        return {requestHeaders: details.requestHeaders};
+      }, {urls: ["<all_urls>"]}, ["blocking", "requestHeaders"]);
+    },
+  });
+}
+
+add_task(async function test_host_header_accepted() {
+  let extension = getExtension();
+  await extension.startup();
+  let headers = JSON.parse(await ExtensionTestUtils.fetch(FETCH_ORIGIN, `${BASE_URL}/return_headers.sjs`));
+
+  equal(headers.host, "example.org", "Host header was set on request");
+
+  await extension.unload();
+});
+
+add_task(async function test_host_header_denied() {
+  let extension = getExtension(`${BASE_URL}/`);
+
+  await extension.startup();
+
+  let headers = JSON.parse(await ExtensionTestUtils.fetch(FETCH_ORIGIN, `${BASE_URL}/return_headers.sjs`));
+
+  equal(headers.host, "example.com", "Host header was not set on request");
+
+  await extension.unload();
+});
+
+add_task(async function test_host_header_restricted() {
+  Services.prefs.setCharPref("extensions.webextensions.restrictedDomains", "example.org");
+  registerCleanupFunction(() => {
+    Services.prefs.clearUserPref("extensions.webextensions.restrictedDomains");
+  });
+
+  let extension = getExtension();
+
+  await extension.startup();
+
+  let headers = JSON.parse(await ExtensionTestUtils.fetch(FETCH_ORIGIN, `${BASE_URL}/return_headers.sjs`));
+
+  equal(headers.host, "example.com", "Host header was not set on request");
+
+  await extension.unload();
+});
--- a/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini
+++ b/toolkit/components/extensions/test/xpcshell/xpcshell-common.ini
@@ -134,16 +134,18 @@ skip-if = true # Too frequent intermitte
 [test_ext_userScripts.js]
 [test_ext_userScripts_telemetry.js]
 [test_ext_webRequest_auth.js]
 skip-if = os == "android" && debug
 [test_ext_webRequest_filterResponseData.js]
 skip-if = os == "android" && debug
 [test_ext_webRequest_incognito.js]
 skip-if = os == "android" && debug
+[test_ext_webRequest_host.js]
+skip-if = os == "android" && debug
 [test_ext_webRequest_permission.js]
 skip-if = os == "android" && debug
 [test_ext_webRequest_responseBody.js]
 skip-if = os == "android" && debug
 [test_ext_webRequest_set_cookie.js]
 skip-if = appname == "thunderbird" || (os == "android" && debug)
 [test_ext_webRequest_startup.js]
 skip-if = os == "android" && debug