test_bug1222633.html (4773B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1222633 5 --> 6 <head> 7 <title>Test for Bug 1222633</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=1222633">Mozilla Bug 1222633</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 1222633 */ 18 19 function testPreloadEvent(url, crossorigin, expectLoad) { 20 return new Promise((resolve) => { 21 var link = document.createElement("LINK"); 22 link.setAttribute("rel", "preload"); 23 link.setAttribute("href", url); 24 link.setAttribute("as", "fetch"); 25 if (crossorigin) { 26 link.setAttribute("crossorigin", ""); 27 } 28 29 link.addEventListener("load", () => { 30 ok(expectLoad, "not expecting load event for " + url); 31 link.remove(); 32 resolve(); 33 }); 34 link.addEventListener("error", () => { 35 ok(!expectLoad, "not expecting error event for " + url); 36 link.remove(); 37 resolve(); 38 }); 39 document.head.appendChild(link); 40 }); 41 } 42 43 function testChangePrefetchToPreload(url) { 44 return new Promise((resolve) => { 45 var preloaded = false; 46 var link = document.createElement("LINK"); 47 link.setAttribute("rel", "prefetch"); 48 link.setAttribute("href", url); 49 link.setAttribute("as", "fetch"); 50 51 link.addEventListener("load", () => { 52 ok(preloaded, "this will happen only on a preload"); 53 ok(true, "not expecting load event for " + url); 54 link.remove(); 55 resolve(); 56 }); 57 link.addEventListener("error", () => { 58 ok(false, "not expecting error event for " + url); 59 link.remove(); 60 resolve(); 61 }); 62 document.head.appendChild(link); 63 preloaded = true; 64 link.setAttribute("rel", "preload"); 65 }) 66 }; 67 68 const SJS_PATH = window.location.pathname.replace(/[^/]+$/, "file_bug1268962.sjs"); 69 const SAME_ORIGIN = "http://mochi.test:8888" + SJS_PATH; 70 const CROSS_ORIGIN = "http://example.com" + SJS_PATH; 71 72 SimpleTest.waitForExplicitFinish(); 73 74 // test same origin 75 testPreloadEvent(SAME_ORIGIN + "?statusCode=200&cacheControl=no-cache", false, true) 76 .then(() => testPreloadEvent(SAME_ORIGIN + "?statusCode=404&cacheControl=no-cache", false, false)) 77 .then(() => testPreloadEvent(SAME_ORIGIN + "?statusCode=200&cacheControl=max-age%3D120", false, true)) 78 .then(() => testPreloadEvent(SAME_ORIGIN + "?statusCode=404&cacheControl=max-age%3D120", false, false)) 79 80 // test cross origin without CORS 81 .then(() => testPreloadEvent(CROSS_ORIGIN + "?statusCode=200&cacheControl=no-cache", false, true)) 82 .then(() => testPreloadEvent(CROSS_ORIGIN + "?statusCode=404&cacheControl=no-cache", false, true)) 83 .then(() => testPreloadEvent(CROSS_ORIGIN + "?statusCode=200&cacheControl=max-age%3D120", false, true)) 84 .then(() => testPreloadEvent(CROSS_ORIGIN + "?statusCode=404&cacheControl=max-age%3D120", false, true)) 85 86 // test cross origin by redirection without CORS 87 .then(() => testPreloadEvent(SAME_ORIGIN + "?redirect=crossorigin&statusCode=200&cacheControl=no-cache", false, true)) 88 .then(() => testPreloadEvent(SAME_ORIGIN + "?redirect=crossorigin&statusCode=404&cacheControl=no-cache", false, true)) 89 .then(() => testPreloadEvent(SAME_ORIGIN + "?redirect=crossorigin&statusCode=200&cacheControl=max-age%3D120", false, true)) 90 .then(() => testPreloadEvent(SAME_ORIGIN + "?redirect=crossorigin&statusCode=404&cacheControl=max-age%3D120", false, true)) 91 92 // test cross origin with CORS request but no CORS response 93 .then(() => testPreloadEvent(CROSS_ORIGIN + "?statusCode=200&cacheControl=no-cache", true, true)) 94 .then(() => testPreloadEvent(CROSS_ORIGIN + "?statusCode=404&cacheControl=no-cache", true, true)) 95 .then(() => testPreloadEvent(CROSS_ORIGIN + "?statusCode=200&cacheControl=max-age%3D120", true, true)) 96 .then(() => testPreloadEvent(CROSS_ORIGIN + "?statusCode=404&cacheControl=max-age%3D120", true, true)) 97 98 // test cross origin with CORS request and CORS response 99 .then(() => testPreloadEvent(CROSS_ORIGIN + "?statusCode=200&cacheControl=no-cache&allowOrigin=*", true, true)) 100 .then(() => testPreloadEvent(CROSS_ORIGIN + "?statusCode=404&cacheControl=no-cache&allowOrigin=*", true, false)) 101 .then(() => testPreloadEvent(CROSS_ORIGIN + "?statusCode=200&cacheControl=max-age%3D120&allowOrigin=*", true, true)) 102 .then(() => testPreloadEvent(CROSS_ORIGIN + "?statusCode=404&cacheControl=max-age%3D120&allowOrigin=*", true, false)) 103 .then(() => testChangePrefetchToPreload(SAME_ORIGIN + "?statusCode=200&cacheControl=max-age%3D120", false, true)) 104 105 .catch((err) => ok(false, "promise rejected: " + err)) 106 .then(() => SimpleTest.finish()); 107 108 </script> 109 </body> 110 </html>