Backed out 2 changesets (bug 1881078, bug 1879806) for causing dt failures @ devtools...
[gecko.git] / netwerk / test / browser / browser_103_preconnect.js
blobdcdcc1b1382318f431c4927e7725de4ba5f7f008
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/. */
5 Services.prefs.setBoolPref("network.early-hints.enabled", true);
6 Services.prefs.setBoolPref("network.early-hints.preconnect.enabled", true);
7 Services.prefs.setBoolPref("network.http.debug-observations", true);
8 Services.prefs.setIntPref("network.early-hints.preconnect.max_connections", 10);
10 registerCleanupFunction(function () {
11   Services.prefs.clearUserPref("network.early-hints.enabled");
12   Services.prefs.clearUserPref("network.early-hints.preconnect.enabled");
13   Services.prefs.clearUserPref("network.http.debug-observations");
14   Services.prefs.clearUserPref(
15     "network.early-hints.preconnect.max_connections"
16   );
17 });
19 // Test steps:
20 // 1. Load early_hint_preconnect_html.sjs
21 // 2. In early_hint_preconnect_html.sjs, a 103 response with
22 //    "rel=preconnect" is returned.
23 // 3. We use "speculative-connect-request" topic to observe whether the
24 //    speculative connection is attempted.
25 // 4. Finally, we check if the observed URL is the same as the expected.
26 async function test_hint_preconnect(href, crossOrigin) {
27   let requestUrl = `https://example.com/browser/netwerk/test/browser/early_hint_preconnect_html.sjs?href=${href}&crossOrigin=${crossOrigin}`;
29   let observed = "";
30   let observer = {
31     QueryInterface: ChromeUtils.generateQI(["nsIObserver"]),
32     observe(aSubject, aTopic, aData) {
33       if (aTopic == "speculative-connect-request") {
34         Services.obs.removeObserver(observer, "speculative-connect-request");
35         observed = aData;
36       }
37     },
38   };
39   Services.obs.addObserver(observer, "speculative-connect-request");
41   await BrowserTestUtils.withNewTab(
42     {
43       gBrowser,
44       url: requestUrl,
45       waitForLoad: true,
46     },
47     async function () {}
48   );
50   // Extracting "localhost:443"
51   let hostPortRegex = /\[.*\](.*?)\^/;
52   let hostPortMatch = hostPortRegex.exec(observed);
53   let hostPort = hostPortMatch ? hostPortMatch[1] : "";
54   // Extracting "%28https%2Cexample.com%29"
55   let partitionKeyRegex = /\^partitionKey=(.*)$/;
56   let partitionKeyMatch = partitionKeyRegex.exec(observed);
57   let partitionKey = partitionKeyMatch ? partitionKeyMatch[1] : "";
58   // See nsHttpConnectionInfo::BuildHashKey, the second character is A if this
59   // is an anonymous connection.
60   let anonymousFlag = observed[2];
62   Assert.equal(anonymousFlag, crossOrigin === "use-credentials" ? "." : "A");
63   Assert.equal(hostPort, "localhost:443");
64   Assert.equal(partitionKey, "%28https%2Cexample.com%29");
67 add_task(async function test_103_preconnect() {
68   await test_hint_preconnect("https://localhost", "use-credentials");
69   await test_hint_preconnect("https://localhost", "");
70   await test_hint_preconnect("https://localhost", "anonymous");
71 });