browser_103_private_window.js (2637B)
1 /* This Source Code Form is subject to the terms of the Mozilla Public 2 * License, v. 2.0. If a copy of the MPL was not distributed with this 3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 4 5 "use strict"; 6 7 Services.prefs.setBoolPref("network.early-hints.enabled", true); 8 9 registerCleanupFunction(function () { 10 Services.prefs.clearUserPref("network.early-hints.enabled"); 11 }); 12 13 // Test steps: 14 // 1. Load early_hint_asset_html.sjs with a provided uuid. 15 // 2. In early_hint_asset_html.sjs, a 103 response with 16 // a Link header<early_hint_asset.sjs> and the provided uuid is returned. 17 // 3. We use "http-on-opening-request" topic to observe whether the 18 // early hinted request is created. 19 // 4. Finally, we check if the request has the correct `isPrivate` value. 20 async function test_early_hints_load_url(usePrivateWin) { 21 let headers = new Headers(); 22 headers.append("X-Early-Hint-Count-Start", ""); 23 await fetch( 24 "https://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs", 25 { headers } 26 ); 27 28 // Open a browsing window. 29 const win = await BrowserTestUtils.openNewBrowserWindow({ 30 private: usePrivateWin, 31 }); 32 33 let id = Services.uuid.generateUUID().toString(); 34 let expectedUrl = `https://example.com/browser/netwerk/test/browser/early_hint_asset.sjs?as=fetch&uuid=${id}`; 35 let observed = {}; 36 let observer = { 37 QueryInterface: ChromeUtils.generateQI(["nsIObserver"]), 38 observe(aSubject, aTopic) { 39 if (aTopic == "http-on-opening-request") { 40 let channel = aSubject.QueryInterface(Ci.nsIHttpChannel); 41 if (channel.URI.spec === expectedUrl) { 42 observed.actrualUrl = channel.URI.spec; 43 let isPrivate = channel.QueryInterface( 44 Ci.nsIPrivateBrowsingChannel 45 ).isChannelPrivate; 46 observed.isPrivate = isPrivate; 47 Services.obs.removeObserver(observer, "http-on-opening-request"); 48 } 49 } 50 }, 51 }; 52 Services.obs.addObserver(observer, "http-on-opening-request"); 53 54 let requestUrl = `https://example.com/browser/netwerk/test/browser/early_hint_asset_html.sjs?as=fetch&hinted=1&uuid=${id}`; 55 56 const browser = win.gBrowser.selectedTab.linkedBrowser; 57 let loaded = BrowserTestUtils.browserLoaded(browser, false, requestUrl); 58 BrowserTestUtils.startLoadingURIString(browser, requestUrl); 59 await loaded; 60 61 Assert.equal(observed.actrualUrl, expectedUrl); 62 Assert.equal(observed.isPrivate, usePrivateWin); 63 64 await BrowserTestUtils.closeWindow(win); 65 } 66 67 add_task(async function test_103_private_window() { 68 await test_early_hints_load_url(true); 69 await test_early_hints_load_url(false); 70 });