Bug 1857386 [wpt PR 42383] - Update wpt metadata, a=testonly
[gecko.git] / netwerk / test / unit / test_altsvc_pref.js
blob3e6d3289f4eee5b370c5ad7722962193d388a466
1 "use strict";
3 let h3Port;
4 let h3Route;
5 let h3AltSvc;
6 let prefs;
7 let httpsOrigin;
9 let tests = [
10   // The altSvc storage may not be up imediately, therefore run test_no_altsvc_pref
11   // for a couple times to wait for the storage.
12   test_no_altsvc_pref,
13   test_no_altsvc_pref,
14   test_no_altsvc_pref,
15   test_altsvc_pref,
16   testsDone,
19 let current_test = 0;
21 function run_next_test() {
22   if (current_test < tests.length) {
23     dump("starting test number " + current_test + "\n");
24     tests[current_test]();
25     current_test++;
26   }
29 function run_test() {
30   h3Port = Services.env.get("MOZHTTP3_PORT");
31   Assert.notEqual(h3Port, null);
32   Assert.notEqual(h3Port, "");
33   h3AltSvc = ":" + h3Port;
35   h3Route = "foo.example.com:" + h3Port;
36   do_get_profile();
37   prefs = Services.prefs;
39   prefs.setBoolPref("network.http.http3.enable", true);
40   prefs.setCharPref("network.dns.localDomains", "foo.example.com");
41   prefs.setBoolPref("network.dns.disableIPv6", true);
43   // The certificate for the http3server server is for foo.example.com and
44   // is signed by http2-ca.pem so add that cert to the trust list as a
45   // signing cert.
46   let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
47     Ci.nsIX509CertDB
48   );
49   addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
50   httpsOrigin = "https://foo.example.com/";
52   run_next_test();
55 let Http3CheckListener = function () {};
57 Http3CheckListener.prototype = {
58   expectedRoute: "",
59   expectedStatus: Cr.NS_OK,
61   onStartRequest: function testOnStartRequest(request) {
62     Assert.ok(request instanceof Ci.nsIHttpChannel);
63     Assert.equal(request.status, this.expectedStatus);
64     if (Components.isSuccessCode(this.expectedStatus)) {
65       Assert.equal(request.responseStatus, 200);
66     }
67   },
69   onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
70     read_stream(stream, cnt);
71   },
73   onStopRequest: function testOnStopRequest(request, status) {
74     Assert.equal(status, this.expectedStatus);
75     if (Components.isSuccessCode(this.expectedStatus)) {
76       Assert.equal(request.responseStatus, 200);
77       let routed = "NA";
78       try {
79         routed = request.getRequestHeader("Alt-Used");
80       } catch (e) {}
81       dump("routed is " + routed + "\n");
83       Assert.equal(routed, this.expectedRoute);
85       let httpVersion = "";
86       try {
87         httpVersion = request.protocolVersion;
88       } catch (e) {}
89       Assert.equal(httpVersion, "h3");
90     }
92     do_test_finished();
93   },
96 function makeChan(uri) {
97   let chan = NetUtil.newChannel({
98     uri,
99     loadUsingSystemPrincipal: true,
100   }).QueryInterface(Ci.nsIHttpChannel);
101   chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
102   return chan;
105 function test_no_altsvc_pref() {
106   dump("test_no_altsvc_pref");
107   do_test_pending();
109   let chan = makeChan(httpsOrigin + "http3-test");
110   let listener = new Http3CheckListener();
111   listener.expectedStatus = Cr.NS_ERROR_CONNECTION_REFUSED;
112   chan.asyncOpen(listener);
115 function test_altsvc_pref() {
116   dump("test_altsvc_pref");
117   do_test_pending();
119   prefs.setCharPref(
120     "network.http.http3.alt-svc-mapping-for-testing",
121     "foo.example.com;h3-29=" + h3AltSvc
122   );
124   let chan = makeChan(httpsOrigin + "http3-test");
125   let listener = new Http3CheckListener();
126   listener.expectedRoute = h3Route;
127   chan.asyncOpen(listener);
130 function testsDone() {
131   prefs.clearUserPref("network.http.http3.enable");
132   prefs.clearUserPref("network.dns.localDomains");
133   prefs.clearUserPref("network.dns.disableIPv6");
134   prefs.clearUserPref("network.http.http3.alt-svc-mapping-for-testing");
135   dump("testDone\n");