Bug 1796551 [wpt PR 36570] - WebKit export of https://bugs.webkit.org/show_bug.cgi...
[gecko.git] / netwerk / test / unit / test_altsvc_pref.js
blobf9c524d57f24f90996a769860ee1e030123d5d15
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   let env = Cc["@mozilla.org/process/environment;1"].getService(
31     Ci.nsIEnvironment
32   );
33   h3Port = env.get("MOZHTTP3_PORT");
34   Assert.notEqual(h3Port, null);
35   Assert.notEqual(h3Port, "");
36   h3AltSvc = ":" + h3Port;
38   h3Route = "foo.example.com:" + h3Port;
39   do_get_profile();
40   prefs = Services.prefs;
42   prefs.setBoolPref("network.http.http3.enable", true);
43   prefs.setCharPref("network.dns.localDomains", "foo.example.com");
44   prefs.setBoolPref("network.dns.disableIPv6", true);
46   // The certificate for the http3server server is for foo.example.com and
47   // is signed by http2-ca.pem so add that cert to the trust list as a
48   // signing cert.
49   let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
50     Ci.nsIX509CertDB
51   );
52   addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
53   httpsOrigin = "https://foo.example.com/";
55   run_next_test();
58 let Http3CheckListener = function() {};
60 Http3CheckListener.prototype = {
61   expectedRoute: "",
62   expectedStatus: Cr.NS_OK,
64   onStartRequest: function testOnStartRequest(request) {
65     Assert.ok(request instanceof Ci.nsIHttpChannel);
66     Assert.equal(request.status, this.expectedStatus);
67     if (Components.isSuccessCode(this.expectedStatus)) {
68       Assert.equal(request.responseStatus, 200);
69     }
70   },
72   onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
73     read_stream(stream, cnt);
74   },
76   onStopRequest: function testOnStopRequest(request, status) {
77     Assert.equal(status, this.expectedStatus);
78     if (Components.isSuccessCode(this.expectedStatus)) {
79       Assert.equal(request.responseStatus, 200);
80       let routed = "NA";
81       try {
82         routed = request.getRequestHeader("Alt-Used");
83       } catch (e) {}
84       dump("routed is " + routed + "\n");
86       Assert.equal(routed, this.expectedRoute);
88       let httpVersion = "";
89       try {
90         httpVersion = request.protocolVersion;
91       } catch (e) {}
92       Assert.equal(httpVersion, "h3");
93     }
95     do_test_finished();
96   },
99 function makeChan(uri) {
100   let chan = NetUtil.newChannel({
101     uri,
102     loadUsingSystemPrincipal: true,
103   }).QueryInterface(Ci.nsIHttpChannel);
104   chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
105   return chan;
108 function test_no_altsvc_pref() {
109   dump("test_no_altsvc_pref");
110   do_test_pending();
112   let chan = makeChan(httpsOrigin + "http3-test");
113   let listener = new Http3CheckListener();
114   listener.expectedStatus = Cr.NS_ERROR_CONNECTION_REFUSED;
115   chan.asyncOpen(listener);
118 function test_altsvc_pref() {
119   dump("test_altsvc_pref");
120   do_test_pending();
122   prefs.setCharPref(
123     "network.http.http3.alt-svc-mapping-for-testing",
124     "foo.example.com;h3-29=" + h3AltSvc
125   );
127   let chan = makeChan(httpsOrigin + "http3-test");
128   let listener = new Http3CheckListener();
129   listener.expectedRoute = h3Route;
130   chan.asyncOpen(listener);
133 function testsDone() {
134   prefs.clearUserPref("network.http.http3.enable");
135   prefs.clearUserPref("network.dns.localDomains");
136   prefs.clearUserPref("network.dns.disableIPv6");
137   prefs.clearUserPref("network.http.http3.alt-svc-mapping-for-testing");
138   dump("testDone\n");