8 let tests = [test_https_alt_svc, testsDone];
12 function run_next_test() {
13 if (current_test < tests.length) {
14 dump("starting test number " + current_test + "\n");
15 tests[current_test]();
21 let env = Cc["@mozilla.org/process/environment;1"].getService(
24 let h2Port = env.get("MOZHTTP2_PORT");
25 Assert.notEqual(h2Port, null);
26 Assert.notEqual(h2Port, "");
27 let h3Port = env.get("MOZHTTP3_PORT");
28 Assert.notEqual(h3Port, null);
29 Assert.notEqual(h3Port, "");
30 h3AltSvc = ":" + h3Port;
32 h3Route = "foo.example.com:" + h3Port;
34 prefs = Services.prefs;
36 prefs.setBoolPref("network.http.http3.enable", true);
37 prefs.setCharPref("network.dns.localDomains", "foo.example.com");
38 // We always resolve elements of localDomains as it's hardcoded without the
40 prefs.setBoolPref("network.proxy.allow_hijacking_localhost", true);
42 // The certificate for the http3server server is for foo.example.com and
43 // is signed by http2-ca.pem so add that cert to the trust list as a
45 let certdb = Cc["@mozilla.org/security/x509certdb;1"].getService(
48 addCertFromFile(certdb, "http2-ca.pem", "CTu,u,u");
49 httpsOrigin = "https://foo.example.com:" + h2Port + "/";
54 function createPrincipal(url) {
55 var ssm = Services.scriptSecurityManager;
57 return ssm.createContentPrincipal(Services.io.newURI(url), {});
63 function makeChan(uri) {
64 let chan = NetUtil.newChannel({
66 loadingPrincipal: createPrincipal(uri),
67 securityFlags: Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_INHERITS_SEC_CONTEXT,
68 contentPolicyType: Ci.nsIContentPolicy.TYPE_DOCUMENT,
69 }).QueryInterface(Ci.nsIHttpChannel);
70 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
74 let WaitForHttp3Listener = function() {};
76 WaitForHttp3Listener.prototype = {
77 onDataAvailableFired: false,
80 onStartRequest: function testOnStartRequest(request) {
81 Assert.ok(request instanceof Ci.nsIHttpChannel);
82 Assert.equal(request.responseStatus, 200);
85 onDataAvailable: function testOnDataAvailable(request, stream, off, cnt) {
86 this.onDataAvailableFired = true;
87 read_stream(stream, cnt);
90 onStopRequest: function testOnStopRequest(request, status) {
93 routed = request.getRequestHeader("Alt-Used");
95 dump("routed is " + routed + "\n");
97 if (routed == this.expectedRoute) {
100 httpVersion = request.protocolVersion;
102 Assert.equal(httpVersion, "h3-29");
105 dump("poll later for alt svc mapping\n");
107 do_timeout(500, () => {
108 doTest(this.uri, this.expectedRoute, this.h3AltSvc);
116 function doTest(uri, expectedRoute, altSvc) {
117 let chan = makeChan(uri);
118 let listener = new WaitForHttp3Listener();
120 listener.expectedRoute = expectedRoute;
121 listener.h3AltSvc = altSvc;
122 chan.setRequestHeader("x-altsvc", altSvc, false);
123 chan.asyncOpen(listener);
126 // Test Alt-Svc for http3.
127 // H2 server returns alt-svc=h2=foo2.example.com:8000,h3-29=:h3port,h3-30=foo2.example.com:8443
128 function test_https_alt_svc() {
129 dump("test_https_alt_svc()\n");
131 doTest(httpsOrigin + "http3-test2", h3Route, h3AltSvc);
134 function testsDone() {
135 prefs.clearUserPref("network.http.http3.enable");
136 prefs.clearUserPref("network.dns.localDomains");
137 prefs.clearUserPref("network.proxy.allow_hijacking_localhost");