tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

browser_Capabilities.js (3049B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      3 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 "use strict";
      6 
      7 const { HttpServer } = ChromeUtils.importESModule(
      8  "resource://testing-common/httpd.sys.mjs"
      9 );
     10 
     11 // eslint-disable-next-line mozilla/no-redeclare-with-import-autofix
     12 const { ProxyConfiguration } = ChromeUtils.importESModule(
     13  "chrome://remote/content/shared/webdriver/Capabilities.sys.mjs"
     14 );
     15 
     16 add_task(async function test_global_manual_http_proxy() {
     17  await SpecialPowers.pushPrefEnv({
     18    set: [["network.proxy.allow_hijacking_localhost", true]],
     19  });
     20 
     21  const server = new HttpServer();
     22  server.start(-1);
     23  server.registerPathHandler("/", (request, response) => {
     24    response.setStatusLine(request.httpVersion, 200, "OK");
     25    response.setHeader("Content-Type", "text/plain", true);
     26    response.write("Not proxied");
     27  });
     28  const SERVER_URL = `http://localhost:${server.identity.primaryPort}`;
     29 
     30  const proxyServer = new HttpServer();
     31  proxyServer.start(-1);
     32  proxyServer.identity.add("http", "localhost", server.identity.primaryPort);
     33  proxyServer.registerPathHandler("/", (request, response) => {
     34    response.setStatusLine(request.httpVersion, 200, "OK");
     35    response.setHeader("Content-Type", "text/plain", true);
     36    response.write("Proxied");
     37  });
     38  const PROXY_URL = `localhost:${proxyServer.identity.primaryPort}`;
     39 
     40  const tab = BrowserTestUtils.addTab(gBrowser, SERVER_URL);
     41  const browser = tab.linkedBrowser;
     42 
     43  info("Verify that navigation request is not proxied");
     44  let loadedPromise = BrowserTestUtils.browserLoaded(browser);
     45  BrowserTestUtils.startLoadingURIString(browser, SERVER_URL);
     46  await loadedPromise;
     47 
     48  await SpecialPowers.spawn(browser, [], async () => {
     49    Assert.strictEqual(
     50      content.document.body.textContent,
     51      "Not proxied",
     52      "The page was not proxied"
     53    );
     54  });
     55 
     56  info("Set the global manual proxy");
     57 
     58  const globalProxy = ProxyConfiguration.fromJSON({
     59    proxyType: "manual",
     60    httpProxy: PROXY_URL,
     61  });
     62 
     63  globalProxy.init();
     64 
     65  info("Verify that navigation request is proxied");
     66  loadedPromise = BrowserTestUtils.browserLoaded(browser);
     67  BrowserTestUtils.startLoadingURIString(browser, SERVER_URL);
     68  await loadedPromise;
     69 
     70  await SpecialPowers.spawn(browser, [], async () => {
     71    Assert.strictEqual(
     72      content.document.body.textContent,
     73      "Proxied",
     74      "The page was proxied"
     75    );
     76  });
     77 
     78  info("Destroy the proxy configuration");
     79 
     80  globalProxy.destroy();
     81 
     82  info("Verify that navigation request is not proxied");
     83  loadedPromise = BrowserTestUtils.browserLoaded(browser);
     84  BrowserTestUtils.startLoadingURIString(browser, SERVER_URL);
     85  await loadedPromise;
     86 
     87  await SpecialPowers.spawn(browser, [], async () => {
     88    Assert.strictEqual(
     89      content.document.body.textContent,
     90      "Not proxied",
     91      "The page was not proxied"
     92    );
     93  });
     94 
     95  BrowserTestUtils.removeTab(tab);
     96 
     97  server.stop();
     98  proxyServer.stop();
     99 });