summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_ext_proxy_settings.js
blob: 33c91309f0c4e8c564cf5b2a5a0a80510bebc64a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
"use strict";

ChromeUtils.defineESModuleGetters(this, {
  HttpServer: "resource://testing-common/httpd.sys.mjs",
});

const { createAppInfo, promiseShutdownManager, promiseStartupManager } =
  AddonTestUtils;

AddonTestUtils.init(this);

createAppInfo("xpcshell@tests.mozilla.org", "XPCShell", "1", "42");

// We cannot use createHttpServer because it also messes with proxies.  We want
// httpChannel to pick up the prefs we set and use those to proxy to our server.
// If this were to fail, we would get an error about making a request out to
// the network.
const proxy = new HttpServer();
proxy.start(-1);
proxy.registerPathHandler("/fubar", (request, response) => {
  response.setStatusLine(request.httpVersion, 200, "OK");
  response.write("ok");
});
registerCleanupFunction(() => {
  return new Promise(resolve => {
    proxy.stop(resolve);
  });
});

add_task(async function test_proxy_settings() {
  async function background(host, port) {
    browser.webRequest.onBeforeRequest.addListener(
      details => {
        browser.test.assertEq(
          host,
          details.proxyInfo.host,
          "proxy host matched"
        );
        browser.test.assertEq(
          port,
          details.proxyInfo.port,
          "proxy port matched"
        );
      },
      { urls: ["http://example.com/*"] }
    );
    browser.webRequest.onCompleted.addListener(
      details => {
        browser.test.notifyPass("proxytest");
      },
      { urls: ["http://example.com/*"] }
    );
    browser.webRequest.onErrorOccurred.addListener(
      details => {
        browser.test.notifyFail("proxytest");
      },
      { urls: ["http://example.com/*"] }
    );

    // Wait for the settings before testing a request.
    await browser.proxy.settings.set({
      value: {
        proxyType: "manual",
        http: `${host}:${port}`,
      },
    });
    browser.test.sendMessage("ready");
  }

  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      browser_specific_settings: { gecko: { id: "proxy.settings@mochi.test" } },
      permissions: ["proxy", "webRequest", "<all_urls>"],
    },
    incognitoOverride: "spanning",
    useAddonManager: "temporary",
    background: `(${background})("${proxy.identity.primaryHost}", ${proxy.identity.primaryPort})`,
  });

  await promiseStartupManager();
  await extension.startup();
  await extension.awaitMessage("ready");
  equal(
    Services.prefs.getStringPref("network.proxy.http"),
    proxy.identity.primaryHost,
    "proxy address is set"
  );
  equal(
    Services.prefs.getIntPref("network.proxy.http_port"),
    proxy.identity.primaryPort,
    "proxy port is set"
  );
  let ok = extension.awaitFinish("proxytest");
  let contentPage = await ExtensionTestUtils.loadContentPage(
    "http://example.com/fubar"
  );
  await ok;

  await contentPage.close();
  await extension.unload();
  await promiseShutdownManager();
});