Bug 1931425 - Limit how often moz-label's #setStyles runs r=reusable-components-revie...
[gecko.git] / netwerk / test / unit / test_websocket_server_multiclient.js
blob33e730acbfaa8cf91b2b9b6c99756927a2617968
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 /* import-globals-from head_cache.js */
8 /* import-globals-from head_cookies.js */
9 /* import-globals-from head_channels.js */
10 /* import-globals-from head_servers.js */
11 /* import-globals-from head_websocket.js */
13 // These test should basically match the ones in test_websocket_server.js,
14 // but with multiple websocket clients making requests on the same server
16 const certOverrideService = Cc[
17 "@mozilla.org/security/certoverride;1"
18 ].getService(Ci.nsICertOverrideService);
20 // setup
21 add_setup(async function setup() {
22 // turn off cert checking for these tests
23 certOverrideService.setDisableAllSecurityChecksAndLetAttackersInterceptMyData(
24 true
26 });
28 // append cleanup to cleanup queue
29 registerCleanupFunction(async () => {
30 certOverrideService.setDisableAllSecurityChecksAndLetAttackersInterceptMyData(
31 false
33 Services.prefs.clearUserPref("network.http.http2.websockets");
34 });
36 async function spinup_and_check(proxy_kind, ws_kind) {
37 let ws_h2 = true;
38 if (ws_kind == NodeWebSocketServer) {
39 info("not h2 ws");
40 ws_h2 = false;
42 Services.prefs.setBoolPref("network.http.http2.websockets", ws_h2);
44 let proxy;
45 if (proxy_kind) {
46 proxy = new proxy_kind();
47 await proxy.start();
48 registerCleanupFunction(async () => proxy.stop());
51 let wss = new ws_kind();
52 await wss.start();
53 registerCleanupFunction(async () => wss.stop());
55 Assert.notEqual(wss.port(), null);
56 await wss.registerMessageHandler((data, ws) => {
57 ws.send(data);
58 });
59 let url = `wss://localhost:${wss.port()}`;
61 let conn1 = new WebSocketConnection();
62 await conn1.open(url);
64 let conn2 = new WebSocketConnection();
65 await conn2.open(url);
67 conn1.send("msg1");
68 conn2.send("msg2");
70 let mess2 = await conn2.receiveMessages();
71 Assert.deepEqual(mess2, ["msg2"]);
73 conn1.send("msg1 again");
74 let mess1 = [];
75 while (mess1.length < 2) {
76 // receive could return only the first or both replies.
77 mess1 = mess1.concat(await conn1.receiveMessages());
79 Assert.deepEqual(mess1, ["msg1", "msg1 again"]);
81 conn1.close();
82 conn2.close();
83 Assert.deepEqual({ status: Cr.NS_OK }, await conn1.finished());
84 Assert.deepEqual({ status: Cr.NS_OK }, await conn2.finished());
85 await wss.stop();
87 if (proxy_kind) {
88 await proxy.stop();
92 // h1.1 direct
93 async function test_h1_websocket_direct() {
94 await spinup_and_check(null, NodeWebSocketServer);
97 // h2 direct
98 async function test_h2_websocket_direct() {
99 await spinup_and_check(null, NodeWebSocketHttp2Server);
102 // ws h1.1 with secure h1.1 proxy
103 async function test_h1_ws_with_secure_h1_proxy() {
104 await spinup_and_check(NodeHTTPSProxyServer, NodeWebSocketServer);
107 // ws h1.1 with insecure h1.1 proxy
108 async function test_h1_ws_with_insecure_h1_proxy() {
109 await spinup_and_check(NodeHTTPProxyServer, NodeWebSocketServer);
112 // ws h1.1 with h2 proxy
113 async function test_h1_ws_with_h2_proxy() {
114 await spinup_and_check(NodeHTTP2ProxyServer, NodeWebSocketServer);
117 // ws h2 with insecure h1.1 proxy
118 async function test_h2_ws_with_insecure_h1_proxy() {
119 await spinup_and_check(NodeHTTPProxyServer, NodeWebSocketHttp2Server);
122 // ws h2 with secure h1 proxy
123 async function test_h2_ws_with_secure_h1_proxy() {
124 await spinup_and_check(NodeHTTPSProxyServer, NodeWebSocketHttp2Server);
127 // ws h2 with secure h2 proxy
128 async function test_h2_ws_with_h2_proxy() {
129 await spinup_and_check(NodeHTTP2ProxyServer, NodeWebSocketHttp2Server);
132 add_task(test_h1_websocket_direct);
133 add_task(test_h2_websocket_direct);
134 add_task(test_h1_ws_with_secure_h1_proxy);
135 add_task(test_h1_ws_with_insecure_h1_proxy);
136 add_task(test_h1_ws_with_h2_proxy);
138 // any multi-client test with h2 websocket and any kind of proxy will fail/hang
139 add_task(test_h2_ws_with_insecure_h1_proxy);
140 add_task(test_h2_ws_with_secure_h1_proxy);
141 add_task(test_h2_ws_with_h2_proxy);