summaryrefslogtreecommitdiffstats
path: root/netwerk/cookie/test/mochitest/cookiesHelper.js
blob: 41b0065d418c29d3f44c73bdfab6e3a98e0ed925 (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
const ALLOWED = 0;
const BLOCKED = 1;

async function cleanupData() {
  await new Promise(resolve => {
    const chromeScript = SpecialPowers.loadChromeScript(_ => {
      // eslint-disable-next-line no-undef
      addMessageListener("go", __ => {
        const { Services } = ChromeUtils.import(
          "resource://gre/modules/Services.jsm"
        );
        Services.clearData.deleteData(
          Services.clearData.CLEAR_COOKIES |
            Services.clearData.CLEAR_ALL_CACHES |
            Services.clearData.CLEAR_DOM_STORAGES,
          ___ => {
            // eslint-disable-next-line no-undef
            sendAsyncMessage("done");
          }
        );
      });
    });

    chromeScript.addMessageListener("done", _ => {
      chromeScript.destroy();
      resolve();
    });

    chromeScript.sendAsyncMessage("go");
  });
}

async function checkLastRequest(type, state) {
  let json = await fetch("cookie.sjs?last&" + Math.random()).then(r =>
    r.json()
  );
  is(json.type, type, "Type: " + type);
  is(json.hasCookie, state == ALLOWED, "Fetch has cookies");
}

async function runTests(currentTest) {
  await cleanupData();
  await SpecialPowers.pushPrefEnv({
    set: [["network.cookie.cookieBehavior", 2]],
  });
  let windowBlocked = window.open("cookie.sjs?window&" + Math.random());
  await new Promise(resolve => {
    windowBlocked.onload = resolve;
  });
  await currentTest(windowBlocked, BLOCKED);
  windowBlocked.close();

  await cleanupData();
  await SpecialPowers.pushPrefEnv({
    set: [["network.cookie.cookieBehavior", 1]],
  });
  let windowAllowed = window.open("cookie.sjs?window&" + Math.random());
  await new Promise(resolve => {
    windowAllowed.onload = resolve;
  });
  await currentTest(windowAllowed, ALLOWED);
  windowAllowed.close();

  SimpleTest.finish();
}

SimpleTest.waitForExplicitFinish();