browser_speculative_connection_link_header.js (2092B)
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.http.debug-observations", true); 6 7 registerCleanupFunction(function () { 8 Services.prefs.clearUserPref("network.http.debug-observations"); 9 }); 10 11 // Test steps: 12 // 1. Load file_link_header.sjs 13 // 2.`<link rel="preconnect" href="https://localhost">` is in 14 // file_link_header.sjs, so we will create a speculative connection. 15 // 3. We use "speculative-connect-request" topic to observe whether the 16 // speculative connection is attempted. 17 // 4. Finally, we check if the observed host and partition key are the same and 18 // as the expected. 19 add_task(async function test_link_preconnect() { 20 let requestUrl = `https://example.com/browser/netwerk/test/browser/file_link_header.sjs`; 21 22 let observed = ""; 23 let observer = { 24 QueryInterface: ChromeUtils.generateQI(["nsIObserver"]), 25 observe(aSubject, aTopic, aData) { 26 if (aTopic == "speculative-connect-request") { 27 Services.obs.removeObserver(observer, "speculative-connect-request"); 28 observed = aData; 29 } 30 }, 31 }; 32 Services.obs.addObserver(observer, "speculative-connect-request"); 33 34 await BrowserTestUtils.withNewTab( 35 { 36 gBrowser, 37 url: requestUrl, 38 waitForLoad: true, 39 }, 40 async function () {} 41 ); 42 43 // The hash key should be like: 44 // ".S........[tlsflags0x00000000]localhost:443^partitionKey=%28https%2Cexample.com%29" 45 46 // Extracting "localhost:443" 47 let hostPortRegex = /\[.*\](.*?)\^/; 48 let hostPortMatch = hostPortRegex.exec(observed); 49 let hostPort = hostPortMatch ? hostPortMatch[1] : ""; 50 // Extracting "%28https%2Cexample.com%29" 51 let partitionKeyRegex = /\^partitionKey=(.*)$/; 52 let partitionKeyMatch = partitionKeyRegex.exec(observed); 53 let partitionKey = partitionKeyMatch ? partitionKeyMatch[1] : ""; 54 55 Assert.equal(hostPort, "localhost:443"); 56 Assert.equal(partitionKey, "%28https%2Cexample.com%29"); 57 });