summaryrefslogtreecommitdiffstats
path: root/dom/fetch/tests/browser_origin_trial_coep_credentialless_cache.js
blob: c9b72a3393283e6fbdd027f30834fdfd16a56790 (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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
const TOP_LEVEL_URL =
  getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "https://example.com"
  ) + "open_credentialless_document.sjs";

const SAME_ORIGIN = "https://example.com";
const CROSS_ORIGIN = "https://test1.example.com";

const RESOURCE_URL =
  getRootDirectory(gTestPath).replace(
    "chrome://mochitests/content",
    "https://test1.example.com"
  ) + "credentialless_resource.sjs";

async function store(storer, url, requestCredentialMode) {
  await SpecialPowers.spawn(
    storer.linkedBrowser,
    [url, requestCredentialMode],
    async function (url, requestCredentialMode) {
      const cache = await content.caches.open("v1");
      const fetchRequest = new content.Request(url, {
        mode: "no-cors",
        credentials: requestCredentialMode,
      });

      const fetchResponse = await content.fetch(fetchRequest);
      content.wrappedJSObject.console.log(fetchResponse.headers);
      await cache.put(fetchRequest, fetchResponse);
    }
  );
}

async function retrieve(retriever, resourceURL) {
  return await SpecialPowers.spawn(
    retriever.linkedBrowser,
    [resourceURL],
    async function (url) {
      const cache = await content.caches.open("v1");
      try {
        await cache.match(url);
        return "retrieved";
      } catch (error) {
        return "error";
      }
    }
  );
}

async function testCache(
  storer,
  storeRequestCredentialMode,
  resourceCOEP,
  retriever,
  expectation
) {
  const resourceURL = RESOURCE_URL + "?" + resourceCOEP;

  await store(storer, resourceURL, storeRequestCredentialMode);
  const result = await retrieve(retriever, resourceURL);

  is(result, expectation);
}

add_task(async function () {
  await SpecialPowers.pushPrefEnv({
    set: [
      ["browser.tabs.remote.coep.credentialless", false],
      ["dom.origin-trials.enabled", true],
      ["dom.origin-trials.test-key.enabled", true],
    ],
  });

  const noneTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    TOP_LEVEL_URL
  );
  const requireCorpTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    TOP_LEVEL_URL + "?requirecorp"
  );
  const credentiallessTab = await BrowserTestUtils.openNewForegroundTab(
    gBrowser,
    TOP_LEVEL_URL + "?credentialless"
  );

  await testCache(noneTab, "include", "", noneTab, "retrieved");
  await testCache(noneTab, "include", "", credentiallessTab, "error");
  await testCache(noneTab, "omit", "", credentiallessTab, "retrieved");
  await testCache(
    noneTab,
    "include",
    "corp_cross_origin",
    credentiallessTab,
    "retrieved"
  );
  await testCache(noneTab, "include", "", requireCorpTab, "error");
  await testCache(
    noneTab,
    "include",
    "corp_cross_origin",
    requireCorpTab,
    "retrieved"
  );
  await testCache(credentiallessTab, "include", "", noneTab, "retrieved");
  await testCache(
    credentiallessTab,
    "include",
    "",
    credentiallessTab,
    "retrieved"
  );
  await testCache(credentiallessTab, "include", "", requireCorpTab, "error");
  await testCache(
    requireCorpTab,
    "include",
    "corp_cross_origin",
    noneTab,
    "retrieved"
  );
  await testCache(
    requireCorpTab,
    "include",
    "corp_cross_origin",
    credentiallessTab,
    "retrieved"
  );
  await testCache(
    requireCorpTab,
    "include",
    "corp_cross_origin",
    requireCorpTab,
    "retrieved"
  );

  await BrowserTestUtils.removeTab(noneTab);
  await BrowserTestUtils.removeTab(requireCorpTab);
  await BrowserTestUtils.removeTab(credentiallessTab);
});