summaryrefslogtreecommitdiffstats
path: root/testing/web-platform/tests/speculation-rules/prerender/referrer-policy-mismatch.html
blob: 961b745e8eb80cebcb949ebc0ed040f3c4c4f2ab (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
<!DOCTYPE html>
<title>Referrer policy mismatches must be allowed between prerendering and activation</title>
<meta name="timeout" content="long">
<meta name="referrer" content="strict-origin-when-cross-origin">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/utils.js"></script>
<script src="/common/dispatcher/dispatcher.js"></script>
<script src="/html/browsers/browsing-the-web/remote-context-helper/resources/remote-context-helper.js"></script>
<script src="resources/utils.js"></script>

<script>
setup(() => assertSpeculationRulesIsSupported());

promise_test(async t => {
  const rcHelper = new RemoteContextHelper();
  const referrerRC = await rcHelper.addWindow(undefined, { features: "noopener" });
  await setReferrerPolicy(referrerRC, "strict-origin-when-cross-origin");
  const prerenderedRC = await addPrerenderRC(referrerRC);

  const referrerURL = await referrerRC.executeScript(() => location.href);

  assert_equals(await prerenderedRC.executeScript(() => document.prerendering), true);
  assert_equals(await prerenderedRC.executeScript(() => document.referrer), referrerURL);

  await activatePrerenderRC(referrerRC, prerenderedRC, url => {
    const a = document.createElement("a");
    a.href = url;
    a.referrerPolicy = "no-referrer";
    a.click();
  });

  assert_equals(await prerenderedRC.executeScript(() => document.prerendering), false);
  assert_equals(await prerenderedRC.executeScript(() => document.referrer), referrerURL);
}, 'prerendered with "strict-origin-when-cross-origin", activated with "no-referrer"');

promise_test(async t => {
  const rcHelper = new RemoteContextHelper();
  const referrerRC = await rcHelper.addWindow(undefined, { features: "noopener" });
  await setReferrerPolicy(referrerRC, "strict-origin-when-cross-origin");
  const prerenderedRC = await addPrerenderRC(referrerRC);

  const referrerURL = await referrerRC.executeScript(() => location.href);

  assert_equals(await prerenderedRC.executeScript(() => document.prerendering), true);
  assert_equals(await prerenderedRC.executeScript(() => document.referrer), referrerURL);

  await activatePrerenderRC(referrerRC, prerenderedRC, url => {
    const a = document.createElement("a");
    a.href = url;
    a.referrerPolicy = "strict-origin";
    a.click();
  });

  assert_equals(await prerenderedRC.executeScript(() => document.prerendering), false);
  assert_equals(await prerenderedRC.executeScript(() => document.referrer), referrerURL);
}, 'prerendered with "strict-origin-when-cross-origin", activated with "strict-origin"');

promise_test(async t => {
  const rcHelper = new RemoteContextHelper();
  const referrerRC = await rcHelper.addWindow(undefined, { features: "noopener" });
  await setReferrerPolicy(referrerRC, "strict-origin");
  const prerenderedRC = await addPrerenderRC(referrerRC);

  const referrerURL = await referrerRC.executeScript(() => location.href);
  const referrerOrigin = (new URL(referrerURL)).origin + "/";

  assert_equals(await prerenderedRC.executeScript(() => document.prerendering), true);
  assert_equals(await prerenderedRC.executeScript(() => document.referrer), referrerOrigin);

  await activatePrerenderRC(referrerRC, prerenderedRC, url => {
    const a = document.createElement("a");
    a.href = url;
    a.referrerPolicy = "unsafe-url";
    a.click();
  });

  assert_equals(await prerenderedRC.executeScript(() => document.prerendering), false);
  assert_equals(await prerenderedRC.executeScript(() => document.referrer), referrerOrigin);
}, 'prerendered with "strict-origin", activated with "unsafe-url"');

function setReferrerPolicy(referrerRC, referrerPolicy) {
  return referrerRC.executeScript(referrerPolicy => {
    const meta = document.createElement("meta");
    meta.name = "referrer";
    meta.content = referrerPolicy;
    document.head.append(meta);
  }, [referrerPolicy]);
}
</script>