prefetch-events.html (3711B)
1 <!DOCTYPE html> 2 <title>Ensures that prefetch respects HTTP cache semantics</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/utils.js"></script> 7 <script src="resources/prefetch-helper.js"></script> 8 <script src="/common/get-host-info.sub.js"></script> 9 <body> 10 <script> 11 const {REMOTE_ORIGIN} = get_host_info(); 12 async function prefetch(link, uid, t) { 13 link.rel = "prefetch"; 14 document.head.appendChild(link); 15 const event = new Promise(resolve => { 16 link.addEventListener("error", () => resolve("error")); 17 link.addEventListener("load", () => resolve("load")); 18 t.step_timeout(() => resolve("timeout"), 1000); 19 }); 20 return await event; 21 } 22 23 promise_test(async t => { 24 const uid = token(); 25 const link = document.createElement("link"); 26 link.href = `/preload/resources/prefetch-info.py?key=${uid}`; 27 const event = await prefetch(link, uid, t); 28 assert_equals(event, "load"); 29 }, "Prefetch should fire the load event"); 30 31 promise_test(async t => { 32 const uid = token(); 33 const link = document.createElement("link"); 34 link.href = `${REMOTE_ORIGIN}/preload/resources/prefetch-info.py?key=${uid}`; 35 const event = await prefetch(link, uid, t); 36 assert_equals(event, "load"); 37 }, "Cross-origin prefetch should fire the load event"); 38 39 promise_test(async t => { 40 const uid = token(); 41 const link = document.createElement("link"); 42 link.href = `/preload/resources/prefetch-info.py?key=${uid}&status=404`; 43 const event = await prefetch(link, uid, t); 44 assert_equals(event, "load"); 45 }, "Prefetch should fire the load event for 404"); 46 47 promise_test(async t => { 48 const uid = token(); 49 const link = document.createElement("link"); 50 link.href = `${REMOTE_ORIGIN}/preload/resources/prefetch-info.py?key=${uid}&status=404`; 51 const event = await prefetch(link, uid, t); 52 assert_equals(event, "load"); 53 }, "Prefetch should fire the load event for 404 (cross-origin)"); 54 55 promise_test(async t => { 56 const uid = token(); 57 const link = document.createElement("link"); 58 link.href = `/preload/resources/prefetch-info.py?key=${uid}&status=500`; 59 const event = await prefetch(link, uid, t); 60 assert_equals(event, "load"); 61 }, "Prefetch should fire the load event for 500"); 62 63 promise_test(async t => { 64 const uid = token(); 65 const link = document.createElement("link"); 66 link.href = `${REMOTE_ORIGIN}/preload/resources/prefetch-info.py?key=${uid}&status=500`; 67 const event = await prefetch(link, uid, t); 68 assert_equals(event, "load"); 69 }, "Cross-origin prefetch should fire the load event for 500"); 70 71 promise_test(async t => { 72 const uid = token(); 73 const link = document.createElement("link"); 74 link.crossOrigin = "anonymous"; 75 link.href = `${REMOTE_ORIGIN}/preload/resources/prefetch-info.py?key=${uid}&cors=false`; 76 const event = await prefetch(link, uid, t); 77 assert_equals(event, "error"); 78 }, "Prefetch should fire the error event for network errors"); 79 80 promise_test(async t => { 81 const uid = token(); 82 const link = document.createElement("link"); 83 link.crossOrigin = "anonymous"; 84 const event = await prefetch(link, uid, t); 85 assert_equals(event, "timeout"); 86 }, "Prefetch should do nothing with an empty href"); 87 88 promise_test(async t => { 89 const uid = token(); 90 const link = document.createElement("link"); 91 link.href = "https://example.com\u0000mozilla.org"; 92 link.crossOrigin = "anonymous"; 93 const event = await prefetch(link, uid, t); 94 assert_equals(event, "timeout"); 95 }, "Prefetch should do nothing with an invalid href"); 96 </script> 97 </body>