summaryrefslogtreecommitdiffstats
path: root/toolkit/components/webextensions/test/mochitest/test_ext_cookies_containers.html
blob: bc4994eec7dd6acdca8386405f395a5910dd7483 (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
<!DOCTYPE HTML>
<html>
<head>
  <title>WebExtension test</title>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
  <script type="text/javascript" src="chrome://mochikit/content/tests/SimpleTest/ExtensionTestUtils.js"></script>
  <script type="text/javascript" src="chrome_head.js"></script>
  <script type="text/javascript" src="head.js"></script>
  <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css"/>
</head>
<body>

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

add_task(function* setup() {
  // make sure userContext is enabled.
  return SpecialPowers.pushPrefEnv({"set": [
    ["privacy.userContext.enabled", true],
  ]});
});

add_task(function* test_cookie_containers() {
  async function background() {
    function assertExpected(expected, cookie) {
      for (let key of Object.keys(cookie)) {
        browser.test.assertTrue(key in expected, `found property ${key}`);
        browser.test.assertEq(expected[key], cookie[key], `property value for ${key} is correct`);
      }
      browser.test.assertEq(Object.keys(expected).length, Object.keys(cookie).length, "all expected properties found");
    }

    const TEST_URL = "http://example.org/";
    const THE_FUTURE = Date.now() + 5 * 60;

    let expected = {
      name: "name1",
      value: "value1",
      domain: "example.org",
      hostOnly: true,
      path: "/",
      secure: false,
      httpOnly: false,
      session: false,
      expirationDate: THE_FUTURE,
      storeId: "firefox-container-1",
    };

    let cookie = await browser.cookies.set({
      url: TEST_URL, name: "name1", value: "value1",
      expirationDate: THE_FUTURE, storeId: "firefox-container-1",
    });
    browser.test.assertEq("firefox-container-1", cookie.storeId, "the cookie has the correct storeId");

    cookie = await browser.cookies.get({url: TEST_URL, name: "name1"});
    browser.test.assertEq(null, cookie, "get() without storeId returns null");

    cookie = await browser.cookies.get({url: TEST_URL, name: "name1", storeId: "firefox-container-1"});
    assertExpected(expected, cookie);

    let cookies = await browser.cookies.getAll({storeId: "firefox-default"});
    browser.test.assertEq(0, cookies.length, "getAll() with default storeId returns an empty array");

    cookies = await browser.cookies.getAll({storeId: "firefox-container-1"});
    browser.test.assertEq(1, cookies.length, "one cookie found for matching domain");
    assertExpected(expected, cookies[0]);

    let details = await browser.cookies.remove({url: TEST_URL, name: "name1", storeId: "firefox-container-1"});
    assertExpected({url: TEST_URL, name: "name1", storeId: "firefox-container-1"}, details);

    cookie = await browser.cookies.get({url: TEST_URL, name: "name1", storeId: "firefox-container-1"});
    browser.test.assertEq(null, cookie, "removed cookie not found");

    browser.test.notifyPass("cookies");
  }

  let extension = ExtensionTestUtils.loadExtension({
    background,
    manifest: {
      permissions: ["cookies", "*://example.org/"],
    },
  });

  yield extension.startup();
  yield extension.awaitFinish("cookies");
  yield extension.unload();
});

</script>

</body>
</html>