mutate-rules.https.html (3332B)
1 <!DOCTYPE html> 2 <title>Mutating speculationrules script elements</title> 3 <meta name="timeout" content="long"> 4 <script src="/resources/testharness.js"></script> 5 <script src="/resources/testharnessreport.js"></script> 6 <script src="/common/dispatcher/dispatcher.js"></script> 7 <script src="/common/utils.js"></script> 8 <script src="../resources/utils.js"></script> 9 <script src="resources/utils.sub.js"></script> 10 11 <script> 12 "use strict"; 13 14 setup(() => assertSpeculationRulesIsSupported()); 15 16 promise_test(async t => { 17 const win = await spawnWindow(t); 18 const nextURL = win.getExecutorURL({ executor: "counting-executor.py", page: 2 }); 19 20 // Prefetch the URL which will count whether or not we're prefetched. 21 await win.forceSinglePrefetch(nextURL); 22 23 // Check that the server was indeed hit with a prefetch request. 24 const requestCount = await (await fetch(nextURL + "&check")).json(); 25 assert_equals(requestCount.prefetch, 1, "prefetch count"); 26 assert_equals(requestCount.nonPrefetch, 0, "non-prefetch count"); 27 28 // Now remove the speculation rules. 29 await win.execute_script(() => { 30 document.querySelector("script[type=speculationrules]").remove(); 31 }); 32 33 // Navigating must not use the prefetched result. 34 await win.navigate(nextURL); 35 assert_not_prefetched(await win.getRequestHeaders()); 36 }, "Removing the <script type=speculationrules> must cancel the prefetch"); 37 38 promise_test(async t => { 39 const win = await spawnWindow(t); 40 const nextURL = win.getExecutorURL({ executor: "counting-executor.py", page: 2 }); 41 42 // Prefetch the URL which will count whether or not we're prefetched. 43 await win.forceSinglePrefetch(nextURL); 44 45 // Check that the server was indeed hit with a prefetch request. 46 const requestCount = await (await fetch(nextURL + "&check")).json(); 47 assert_equals(requestCount.prefetch, 1, "prefetch count"); 48 assert_equals(requestCount.nonPrefetch, 0, "non-prefetch count"); 49 50 // Now update the speculation rules to remove that rule. 51 await win.execute_script(() => { 52 document.querySelector("script[type=speculationrules]").textContent = JSON.stringify({ 53 prefetch: [] 54 }); 55 }); 56 57 // Navigating must use the prefetched result. 58 await win.navigate(nextURL); 59 assert_prefetched(await win.getRequestHeaders()); 60 }, "Removing a rule from the <script type=speculationrules> must not cancel the prefetch"); 61 62 promise_test(async t => { 63 const win = await spawnWindow(t); 64 const nextURL = win.getExecutorURL({ executor: "counting-executor.py", page: 2 }); 65 66 // Insert some empty speculation rules 67 await win.forceSpeculationRules([]); 68 69 // Now update them to include a prefetch (and wait for a bit.) 70 await win.execute_script(([nextURL]) => { 71 document.querySelector("script[type=speculationrules]").textContent = JSON.stringify({ 72 prefetch: [{ urls: [nextURL] }] 73 }); 74 }, [nextURL]); 75 new Promise(resolve => t.step_timeout(resolve, 2000)); 76 77 const requestCount = await (await fetch(nextURL + "&check")).json(); 78 assert_equals(requestCount.prefetch, 0, "prefetch count"); 79 assert_equals(requestCount.nonPrefetch, 0, "non-prefetch count"); 80 81 // Navigating must not use the prefetched result. 82 await win.navigate(nextURL); 83 assert_not_prefetched(await win.getRequestHeaders()); 84 }, "Adding a rule to the <script type=speculationrules> must not prefetch"); 85 </script>