Backed out changeset b71c8c052463 (bug 1943846) for causing mass failures. CLOSED...
[gecko.git] / netwerk / test / unit / test_websocket_with_h3_active.js
blob75ddd002021a2687648f393a888a0d22bbe787db
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/. */
4 "use strict";
6 registerCleanupFunction(async () => {
7 http3_clear_prefs();
8 });
10 let wssUri;
11 let httpsUri;
13 add_task(async function pre_setup() {
14 let h2Port = Services.env.get("MOZHTTP2_PORT");
15 Assert.notEqual(h2Port, null);
16 Assert.notEqual(h2Port, "");
18 wssUri = "wss://foo.example.com:" + h2Port + "/websocket";
19 httpsUri = "https://foo.example.com:" + h2Port + "/";
20 Services.prefs.setBoolPref("network.http.http3.support_version1", true);
21 });
23 add_task(async function setup() {
24 await http3_setup_tests("h3", true);
25 });
27 WebSocketListener.prototype = {
28 onAcknowledge() {},
29 onBinaryMessageAvailable() {},
30 onMessageAvailable() {},
31 onServerClose() {},
32 onStart() {
33 this.finish();
35 onStop() {},
38 function makeH2Chan() {
39 let chan = NetUtil.newChannel({
40 uri: httpsUri,
41 loadUsingSystemPrincipal: true,
42 }).QueryInterface(Ci.nsIHttpChannel);
43 chan.loadFlags = Ci.nsIChannel.LOAD_INITIAL_DOCUMENT_URI;
44 return chan;
47 add_task(async function open_wss_when_h3_is_active() {
48 // Make an active connection using HTTP/3
49 let chanHttp1 = makeH2Chan(httpsUri);
50 await new Promise(resolve => {
51 chanHttp1.asyncOpen(
52 new ChannelListener(request => {
53 let httpVersion = "";
54 try {
55 httpVersion = request.protocolVersion;
56 } catch (e) {}
57 Assert.equal(httpVersion, "h3");
58 resolve();
61 });
63 // Now try to connect ot a WebSocket on the same port -> this should not loop
64 // see bug 1717360.
65 let chan = Cc["@mozilla.org/network/protocol;1?name=wss"].createInstance(
66 Ci.nsIWebSocketChannel
68 chan.initLoadInfo(
69 null, // aLoadingNode
70 Services.scriptSecurityManager.getSystemPrincipal(),
71 null, // aTriggeringPrincipal
72 Ci.nsILoadInfo.SEC_ALLOW_CROSS_ORIGIN_SEC_CONTEXT_IS_NULL,
73 Ci.nsIContentPolicy.TYPE_WEBSOCKET
76 var uri = Services.io.newURI(wssUri);
77 var wsListener = new WebSocketListener();
78 await new Promise(resolve => {
79 wsListener.finish = resolve;
80 chan.asyncOpen(uri, wssUri, {}, 0, wsListener, null);
81 });
83 // Try to use https protocol, it should sttill use HTTP/3
84 let chanHttp2 = makeH2Chan(httpsUri);
85 await new Promise(resolve => {
86 chanHttp2.asyncOpen(
87 new ChannelListener(request => {
88 let httpVersion = "";
89 try {
90 httpVersion = request.protocolVersion;
91 } catch (e) {}
92 Assert.equal(httpVersion, "h3");
93 resolve();
96 });
97 });