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

async function cleanupData() {
  await new Promise(resolve => {
    const chromeScript = SpecialPowers.loadChromeScript(_ => {
      /* eslint-env mozilla/chrome-script */
      addMessageListener("go", __ => {
        Services.clearData.deleteData(
          Services.clearData.CLEAR_COOKIES |
            Services.clearData.CLEAR_ALL_CACHES |
            Services.clearData.CLEAR_DOM_STORAGES,
          ___ => {
            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();