Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_coaleasing_h2_and_h3_connection.js
blob3c998ffe295190895da4bdc6d29aa3b7ce9a8cda
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 "use strict";
7 var { setTimeout } = ChromeUtils.importESModule(
8 "resource://gre/modules/Timer.sys.mjs"
9 );
11 let h2Port;
12 let h3Port;
14 add_setup(async function setup() {
15 h2Port = Services.env.get("MOZHTTP2_PORT");
16 Assert.notEqual(h2Port, null);
17 Assert.notEqual(h2Port, "");
19 h3Port = Services.env.get("MOZHTTP3_PORT");
20 Assert.notEqual(h3Port, null);
21 Assert.notEqual(h3Port, "");
23 Services.prefs.setBoolPref("network.http.http3.enable", true);
24 Services.prefs.setIntPref("network.http.speculative-parallel-limit", 6);
25 Services.prefs.setBoolPref("network.http.altsvc.oe", true);
27 // Set to allow the cert presented by our H2 server
28 do_get_profile();
30 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
31 Ci.nsIX509CertDB
33 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
34 });
36 registerCleanupFunction(async () => {
37 Services.prefs.clearUserPref("network.http.http3.enable");
38 Services.prefs.clearUserPref("network.dns.localDomains");
39 Services.prefs.clearUserPref("network.http.speculative-parallel-limit");
40 Services.prefs.clearUserPref("network.http.altsvc.oe");
41 });
43 function makeChan(url) {
44 let chan = NetUtil.newChannel({
45 uri: url,
46 loadUsingSystemPrincipal: true,
47 contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
48 }).QueryInterface(Ci.nsIHttpChannel);
49 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
50 return chan;
53 function channelOpenPromise(chan, flags) {
54 return new Promise(resolve => {
55 function finish(req, buffer) {
56 resolve([req, buffer]);
58 chan.asyncOpen(new ChannelListener(finish, null, flags));
59 });
62 add_task(async function testNotCoaleasingH2Connection() {
63 const host = "foo.example.com";
64 Services.prefs.setCharPref("network.dns.localDomains", host);
66 let server = new NodeHTTPSServer();
67 await server.start();
68 registerCleanupFunction(async () => {
69 await server.stop();
70 });
72 await server.execute(`global.h3Port = "${h3Port}";`);
73 await server.registerPathHandler("/altsvc", (req, resp) => {
74 const body = "done";
75 resp.setHeader("Content-Length", body.length);
76 resp.setHeader("Alt-Svc", `h3-29=:${global.h3Port}`);
77 resp.writeHead(200);
78 resp.write(body);
79 resp.end("");
80 });
82 let chan = makeChan(`https://${host}:${server.port()}/altsvc`);
83 let [req] = await channelOpenPromise(chan);
84 Assert.equal(req.protocolVersion, "http/1.1");
86 // Some delay to make sure the H3 speculative connection is created.
87 // eslint-disable-next-line mozilla/no-arbitrary-setTimeout
88 await new Promise(resolve => setTimeout(resolve, 1000));
90 // To clear the altsvc cache.
91 Services.obs.notifyObservers(null, "last-pb-context-exited");
93 // Add another alt-svc header to route to moz-http2.js.
94 Services.prefs.setCharPref(
95 "network.http.http3.alt-svc-mapping-for-testing",
96 `${host};h2=:${h2Port}`
99 let start = new Date().getTime();
100 chan = makeChan(`https://${host}:${server.port()}/server-timing`);
101 chan.QueryInterface(Ci.nsIHttpChannelInternal).beConservative = true;
102 [req] = await channelOpenPromise(chan);
103 Assert.equal(req.protocolVersion, "h2");
105 // The time this request takes should be way more less than the
106 // neqo idle timeout (30s).
107 let duration = (new Date().getTime() - start) / 1000;
108 Assert.less(duration, 10);