summaryrefslogtreecommitdiffstats
path: root/toolkit/components/webextensions/test/mochitest/test_ext_contentscript_incognito.html
blob: a2f38dce655c038871f86a5fb885a5fa174d6d8b (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
<!DOCTYPE HTML>
<html>
<head>
  <title>Test for content script private browsing ID</title>
  <script type="text/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
</head>
<body>

<script type="text/javascript">
"use strict";

add_task(function* test_contentscript_incognito() {
  let extension = ExtensionTestUtils.loadExtension({
    manifest: {
      content_scripts: [
        {
          "matches": ["http://mochi.test/*/file_sample.html"],
          "js": ["content_script.js"],
        },
      ],
    },

    background() {
      let windowId;

      browser.test.onMessage.addListener(([msg, url]) => {
        if (msg === "open-window") {
          browser.windows.create({url, incognito: true}).then(window => {
            windowId = window.id;
          });
        } else if (msg === "close-window") {
          browser.windows.remove(windowId).then(() => {
            browser.test.sendMessage("done");
          });
        }
      });
    },

    files: {
      "content_script.js": async () => {
        const COOKIE = "foo=florgheralzps";
        document.cookie = COOKIE;

        let url = new URL("return_headers.sjs", location.href);

        let responses = [
          new Promise(resolve => {
            let xhr = new XMLHttpRequest();
            xhr.open("GET", url);
            xhr.onload = () => resolve(JSON.parse(xhr.responseText));
            xhr.send();
          }),

          fetch(url, {credentials: "include"}).then(body => body.json()),
        ];

        try {
          for (let response of await Promise.all(responses)) {
            browser.test.assertEq(COOKIE, response.cookie, "Got expected cookie header");
          }
          browser.test.notifyPass("cookies");
        } catch (e) {
          browser.test.fail(`Error: ${e}`);
          browser.test.notifyFail("cookies");
        }
      },
    },
  });

  yield extension.startup();

  extension.sendMessage(["open-window", SimpleTest.getTestFileURL("file_sample.html")]);

  yield extension.awaitFinish("cookies");

  extension.sendMessage(["close-window"]);
  yield extension.awaitMessage("done");

  yield extension.unload();
});
</script>

</body>
</html>