browser_103_assets_extension.js (2317B)
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 Services.prefs.setBoolPref("network.early-hints.enabled", true); 6 7 registerCleanupFunction(function () { 8 Services.prefs.clearUserPref("network.early-hints.enabled"); 9 }); 10 11 const { request_count_checking } = ChromeUtils.importESModule( 12 "resource://testing-common/early_hint_preload_test_helper.sys.mjs" 13 ); 14 15 // Test steps: 16 // 1. Install an extension to observe the tabId. 17 // 2. Load early_hint_asset_html.sjs?as=image, so we should see a hinted 18 // request to early_hint_asset.sjs?as=image. 19 // 3. Check if the hinted request has the same tabId as 20 // early_hint_asset_html.sys. 21 add_task(async function test_103_asset_with_extension() { 22 let extension = ExtensionTestUtils.loadExtension({ 23 manifest: { 24 permissions: [ 25 "webRequest", 26 "webRequestBlocking", 27 "https://example.com/*", 28 ], 29 }, 30 background() { 31 let { browser } = this; 32 browser.webRequest.onBeforeRequest.addListener( 33 details => { 34 browser.test.sendMessage("request", { 35 url: details.url, 36 tabId: details.tabId, 37 }); 38 return undefined; 39 }, 40 { urls: ["https://example.com/*"] }, 41 ["blocking"] 42 ); 43 }, 44 }); 45 46 await extension.startup(); 47 48 // reset the count 49 let headers = new Headers(); 50 headers.append("X-Early-Hint-Count-Start", ""); 51 await fetch( 52 "https://example.com/browser/netwerk/test/browser/early_hint_pixel_count.sjs", 53 { headers } 54 ); 55 56 let baseUrl = "https://example.com/browser/netwerk/test/browser/"; 57 let requestUrl = `${baseUrl}early_hint_asset_html.sjs?as=image&hinted=1`; 58 let assetUrl = `${baseUrl}early_hint_asset.sjs?as=image`; 59 60 await BrowserTestUtils.withNewTab( 61 { 62 gBrowser, 63 url: requestUrl, 64 waitForLoad: true, 65 }, 66 async function () {} 67 ); 68 69 const result1 = await extension.awaitMessage("request"); 70 Assert.equal(result1.url, requestUrl); 71 let tabId = result1.tabId; 72 73 const result2 = await extension.awaitMessage("request"); 74 Assert.equal(result2.url, assetUrl); 75 Assert.equal(result2.tabId, tabId); 76 77 await extension.unload(); 78 });