File: mutate-rules.https.html

package info (click to toggle)
firefox 147.0.2-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 4,683,484 kB
  • sloc: cpp: 7,607,246; javascript: 6,533,185; ansic: 3,775,227; python: 1,415,393; xml: 634,561; asm: 438,951; java: 186,241; sh: 62,752; makefile: 18,079; objc: 13,092; perl: 12,808; yacc: 4,583; cs: 3,846; pascal: 3,448; lex: 1,720; ruby: 1,003; php: 436; lisp: 258; awk: 247; sql: 66; sed: 54; csh: 10; exp: 6
file content (85 lines) | stat: -rw-r--r-- 3,332 bytes parent folder | download | duplicates (3)
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
<!DOCTYPE html>
<title>Mutating speculationrules script elements</title>
<meta name="timeout" content="long">
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/common/dispatcher/dispatcher.js"></script>
<script src="/common/utils.js"></script>
<script src="../resources/utils.js"></script>
<script src="resources/utils.sub.js"></script>

<script>
"use strict";

setup(() => assertSpeculationRulesIsSupported());

promise_test(async t => {
  const win = await spawnWindow(t);
  const nextURL = win.getExecutorURL({ executor: "counting-executor.py", page: 2 });

  // Prefetch the URL which will count whether or not we're prefetched.
  await win.forceSinglePrefetch(nextURL);

  // Check that the server was indeed hit with a prefetch request.
  const requestCount = await (await fetch(nextURL + "&check")).json();
  assert_equals(requestCount.prefetch, 1, "prefetch count");
  assert_equals(requestCount.nonPrefetch, 0, "non-prefetch count");

  // Now remove the speculation rules.
  await win.execute_script(() => {
    document.querySelector("script[type=speculationrules]").remove();
  });

  // Navigating must not use the prefetched result.
  await win.navigate(nextURL);
  assert_not_prefetched(await win.getRequestHeaders());
}, "Removing the <script type=speculationrules> must cancel the prefetch");

promise_test(async t => {
  const win = await spawnWindow(t);
  const nextURL = win.getExecutorURL({ executor: "counting-executor.py", page: 2 });

  // Prefetch the URL which will count whether or not we're prefetched.
  await win.forceSinglePrefetch(nextURL);

  // Check that the server was indeed hit with a prefetch request.
  const requestCount = await (await fetch(nextURL + "&check")).json();
  assert_equals(requestCount.prefetch, 1, "prefetch count");
  assert_equals(requestCount.nonPrefetch, 0, "non-prefetch count");

  // Now update the speculation rules to remove that rule.
  await win.execute_script(() => {
    document.querySelector("script[type=speculationrules]").textContent = JSON.stringify({
      prefetch: []
    });
  });

  // Navigating must use the prefetched result.
  await win.navigate(nextURL);
  assert_prefetched(await win.getRequestHeaders());
}, "Removing a rule from the <script type=speculationrules> must not cancel the prefetch");

promise_test(async t => {
  const win = await spawnWindow(t);
  const nextURL = win.getExecutorURL({ executor: "counting-executor.py", page: 2 });

  // Insert some empty speculation rules
  await win.forceSpeculationRules([]);

  // Now update them to include a prefetch (and wait for a bit.)
  await win.execute_script(([nextURL]) => {
    document.querySelector("script[type=speculationrules]").textContent = JSON.stringify({
      prefetch: [{ urls: [nextURL] }]
    });
  }, [nextURL]);
  new Promise(resolve => t.step_timeout(resolve, 2000));

  const requestCount = await (await fetch(nextURL + "&check")).json();
  assert_equals(requestCount.prefetch, 0, "prefetch count");
  assert_equals(requestCount.nonPrefetch, 0, "non-prefetch count");

  // Navigating must not use the prefetched result.
  await win.navigate(nextURL);
  assert_not_prefetched(await win.getRequestHeaders());
}, "Adding a rule to the <script type=speculationrules> must not prefetch");
</script>