test_bug1268962.html (4809B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1268962 5 --> 6 <head> 7 <title>Test for Bug 1268962</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css" /> 10 </head> 11 <body> 12 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1268962">Mozilla Bug 1268962</a> 13 <p id="display"></p> 14 <div id="content" style="display: none"></div> 15 <script class="testbody" type="text/javascript"> 16 17 /** Test for Bug 1268962 */ 18 19 function testPrefetchEvent(url, crossorigin, expectLoad) { 20 return new Promise((resolve) => { 21 var link = document.createElement("LINK"); 22 link.setAttribute("rel", "prefetch"); 23 link.setAttribute("href", url); 24 if (crossorigin) { 25 link.setAttribute("crossorigin", ""); 26 } 27 28 link.addEventListener("load", () => { 29 ok(expectLoad, "not expecting load event for " + url); 30 link.remove(); 31 resolve(); 32 }); 33 link.addEventListener("error", () => { 34 ok(!expectLoad, "not expecting error event for " + url); 35 link.remove(); 36 resolve(); 37 }); 38 document.head.appendChild(link); 39 }); 40 } 41 42 function testCancelPrefetchNotCrash(url) { 43 var ios = SpecialPowers.Cc["@mozilla.org/network/io-service;1"]. 44 getService(SpecialPowers.Ci.nsIIOService); 45 var prefetch = SpecialPowers.Cc["@mozilla.org/prefetch-service;1"]. 46 getService(SpecialPowers.Ci.nsIPrefetchService); 47 48 var link = document.createElement("LINK"); 49 link.setAttribute("rel", "prefetch"); 50 link.setAttribute("href", url); 51 document.head.appendChild(link); 52 53 // Not actually verifying any value, just to ensure cancelPrefetchPreload 54 // won't cause crash. 55 prefetch.cancelPrefetchPreloadURI(ios.newURI(url), link); 56 } 57 58 const SJS_PATH = window.location.pathname.replace(/[^/]+$/, "file_bug1268962.sjs"); 59 const SAME_ORIGIN = "http://mochi.test:8888" + SJS_PATH; 60 const CROSS_ORIGIN = "http://example.com" + SJS_PATH; 61 62 SimpleTest.waitForExplicitFinish(); 63 64 SpecialPowers.pushPrefEnv({"set": [["network.prefetch-next.aggressive", true]]}) 65 66 // test same origin 67 .then(() => testPrefetchEvent(SAME_ORIGIN + "?statusCode=200&cacheControl=no-cache", false, false)) 68 .then(() => testPrefetchEvent(SAME_ORIGIN + "?statusCode=404&cacheControl=no-cache", false, false)) 69 .then(() => testPrefetchEvent(SAME_ORIGIN + "?statusCode=200&cacheControl=max-age%3D120", false, true)) 70 .then(() => testPrefetchEvent(SAME_ORIGIN + "?statusCode=404&cacheControl=max-age%3D120", false, false)) 71 72 // test cross origin without CORS 73 .then(() => testPrefetchEvent(CROSS_ORIGIN + "?statusCode=200&cacheControl=no-cache", false, true)) 74 .then(() => testPrefetchEvent(CROSS_ORIGIN + "?statusCode=404&cacheControl=no-cache", false, true)) 75 .then(() => testPrefetchEvent(CROSS_ORIGIN + "?statusCode=200&cacheControl=max-age%3D120", false, true)) 76 .then(() => testPrefetchEvent(CROSS_ORIGIN + "?statusCode=404&cacheControl=max-age%3D120", false, true)) 77 78 // test cross origin by redirection without CORS 79 .then(() => testPrefetchEvent(SAME_ORIGIN + "?redirect=crossorigin&statusCode=200&cacheControl=no-cache", false, true)) 80 .then(() => testPrefetchEvent(SAME_ORIGIN + "?redirect=crossorigin&statusCode=404&cacheControl=no-cache", false, true)) 81 .then(() => testPrefetchEvent(SAME_ORIGIN + "?redirect=crossorigin&statusCode=200&cacheControl=max-age%3D120", false, true)) 82 .then(() => testPrefetchEvent(SAME_ORIGIN + "?redirect=crossorigin&statusCode=404&cacheControl=max-age%3D120", false, true)) 83 84 // test cross origin with CORS request but no CORS response 85 .then(() => testPrefetchEvent(CROSS_ORIGIN + "?statusCode=200&cacheControl=no-cache", true, true)) 86 .then(() => testPrefetchEvent(CROSS_ORIGIN + "?statusCode=404&cacheControl=no-cache", true, true)) 87 .then(() => testPrefetchEvent(CROSS_ORIGIN + "?statusCode=200&cacheControl=max-age%3D120", true, true)) 88 .then(() => testPrefetchEvent(CROSS_ORIGIN + "?statusCode=404&cacheControl=max-age%3D120", true, true)) 89 90 // test cross origin with CORS request and CORS response 91 .then(() => testPrefetchEvent(CROSS_ORIGIN + "?statusCode=200&cacheControl=no-cache&allowOrigin=*", true, false)) 92 .then(() => testPrefetchEvent(CROSS_ORIGIN + "?statusCode=404&cacheControl=no-cache&allowOrigin=*", true, false)) 93 .then(() => testPrefetchEvent(CROSS_ORIGIN + "?statusCode=200&cacheControl=max-age%3D120&allowOrigin=*", true, true)) 94 .then(() => testPrefetchEvent(CROSS_ORIGIN + "?statusCode=404&cacheControl=max-age%3D120&allowOrigin=*", true, false)) 95 96 // test the crash issue: https://bugzilla.mozilla.org/show_bug.cgi?id=1294159 97 .then(() => testCancelPrefetchNotCrash(SAME_ORIGIN + "?statusCode=200&cacheControl=max-age%3D120")) 98 99 .catch((err) => ok(false, "promise rejected: " + err)) 100 .then(() => SimpleTest.finish()); 101 102 </script> 103 </body> 104 </html>