test_bug1667316.html (4326B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=1667316 5 --> 6 <head> 7 <title>Test for Bug 1667316</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=1667316">Mozilla Bug 1667316</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 1667316 */ 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 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 70 const SAME_ORIGIN = "http://mochi.test:8888" + SJS_PATH; 71 // eslint-disable-next-line @microsoft/sdl/no-insecure-url 72 const CROSS_ORIGIN = "http://example.com" + SJS_PATH; 73 74 SimpleTest.waitForExplicitFinish(); 75 SpecialPowers.spawnChrome([], () => { 76 let window = this.browsingContext.currentWindowGlobal; 77 window.ChannelEventSink = { 78 _classDescription: "WebRequest channel event sink", 79 _classID: Components.ID("115062f8-92f1-11e5-8b7f-08001110f7ec"), 80 _contractID: "@mozilla.org/webrequest/channel-event-sink;1", 81 82 QueryInterface: ChromeUtils.generateQI(["nsIChannelEventSink", "nsIFactory"]), 83 84 init() { 85 Components.manager 86 .QueryInterface(Ci.nsIComponentRegistrar) 87 .registerFactory( 88 this._classID, 89 this._classDescription, 90 this._contractID, 91 this 92 ); 93 }, 94 95 register() { 96 Services.catMan.addCategoryEntry( 97 "net-channel-event-sinks", 98 this._contractID, 99 this._contractID, 100 false, 101 true 102 ); 103 }, 104 105 unregister() { 106 Components.manager 107 .QueryInterface(Ci.nsIComponentRegistrar) 108 .unregisterFactory(this._classID, window.ChannelEventSink); 109 Services.catMan.deleteCategoryEntry( 110 "net-channel-event-sinks", 111 this._contractID, 112 false 113 ); 114 }, 115 116 // nsIChannelEventSink implementation 117 asyncOnChannelRedirect(oldChannel, newChannel, flags, redirectCallback) { 118 // Abort the redirection. 119 redirectCallback.onRedirectVerifyCallback(Cr.NS_ERROR_NO_INTERFACE); 120 }, 121 122 // nsIFactory implementation 123 createInstance(iid) { 124 return this.QueryInterface(iid); 125 }, 126 }; 127 128 window.ChannelEventSink.init(); 129 window.ChannelEventSink.register(); 130 }) 131 132 // test cross origin by redirection without CORS 133 .then(() => testPreloadEvent(SAME_ORIGIN + "?redirect=crossorigin&statusCode=200&cacheControl=no-cache", false, false)) 134 135 .catch((err) => ok(false, "promise rejected: " + err)) 136 .then(async () => { 137 await SpecialPowers.spawnChrome([], () => { 138 let window = this.browsingContext.currentWindowGlobal; 139 window.ChannelEventSink.unregister(); 140 delete window.ChannelEventSink; 141 }) 142 }) 143 .then(() => SimpleTest.finish()); 144 145 </script> 146 </body> 147 </html>