summaryrefslogtreecommitdiffstats
path: root/toolkit/components/extensions/test/xpcshell/test_proxy_incognito.js
blob: a37996c22104870de909fd5ef882ae21b344e1b7 (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
"use strict";

/* eslint no-unused-vars: ["error", {"args": "none", "varsIgnorePattern": "^(FindProxyForURL)$"}] */

const server = createHttpServer({ hosts: ["example.com"] });

server.registerPathHandler("/dummy", (request, response) => {
  response.setStatusLine(request.httpVersion, 200, "OK");
  response.setHeader("Content-Type", "text/html", false);
  response.write("<!DOCTYPE html><html></html>");
});

add_task(async function test_incognito_proxy_onRequest_access() {
  // This extension will fail if it gets a private request.
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      permissions: ["proxy", "<all_urls>"],
    },
    async background() {
      browser.proxy.onRequest.addListener(
        async details => {
          browser.test.assertFalse(
            details.incognito,
            "incognito flag is not set"
          );
          browser.test.notifyPass("proxy.onRequest");
        },
        { urls: ["<all_urls>"], types: ["main_frame"] }
      );

      // Actual call arguments do not matter here.
      await browser.test.assertRejects(
        browser.proxy.settings.set({
          value: {
            proxyType: "none",
          },
        }),
        /proxy.settings requires private browsing permission/,
        "proxy.settings requires private browsing permission."
      );

      browser.test.sendMessage("ready");
    },
  });
  await extension.startup();
  await extension.awaitMessage("ready");

  let pextension = ExtensionTestUtils.loadExtension({
    incognitoOverride: "spanning",
    manifest: {
      permissions: ["proxy", "<all_urls>"],
    },
    background() {
      browser.proxy.onRequest.addListener(
        async details => {
          browser.test.assertTrue(
            details.incognito,
            "incognito flag is set with filter"
          );
          browser.test.sendMessage("proxy.onRequest.private");
        },
        { urls: ["<all_urls>"], types: ["main_frame"], incognito: true }
      );

      browser.proxy.onRequest.addListener(
        async details => {
          browser.test.assertFalse(
            details.incognito,
            "incognito flag is not set with filter"
          );
          browser.test.notifyPass("proxy.onRequest.spanning");
        },
        { urls: ["<all_urls>"], types: ["main_frame"], incognito: false }
      );
    },
  });
  await pextension.startup();

  let contentPage = await ExtensionTestUtils.loadContentPage(
    "https://example.com/dummy",
    { privateBrowsing: true }
  );
  await pextension.awaitMessage("proxy.onRequest.private");
  await contentPage.close();

  contentPage = await ExtensionTestUtils.loadContentPage(
    "https://example.com/dummy"
  );
  await extension.awaitFinish("proxy.onRequest");
  await pextension.awaitFinish("proxy.onRequest.spanning");
  await contentPage.close();

  await pextension.unload();
  await extension.unload();
});