summaryrefslogtreecommitdiffstats
path: root/devtools/server/socket/tests/chrome/test_websocket-server.html
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-07 19:33:14 +0000
commit36d22d82aa202bb199967e9512281e9a53db42c9 (patch)
tree105e8c98ddea1c1e4784a60a5a6410fa416be2de /devtools/server/socket/tests/chrome/test_websocket-server.html
parentInitial commit. (diff)
downloadfirefox-esr-upstream.tar.xz
firefox-esr-upstream.zip
Adding upstream version 115.7.0esr.upstream/115.7.0esrupstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'devtools/server/socket/tests/chrome/test_websocket-server.html')
-rw-r--r--devtools/server/socket/tests/chrome/test_websocket-server.html88
1 files changed, 88 insertions, 0 deletions
diff --git a/devtools/server/socket/tests/chrome/test_websocket-server.html b/devtools/server/socket/tests/chrome/test_websocket-server.html
new file mode 100644
index 0000000000..b809aca0a5
--- /dev/null
+++ b/devtools/server/socket/tests/chrome/test_websocket-server.html
@@ -0,0 +1,88 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <meta charset="utf-8">
+ <title>Mozilla Bug</title>
+ <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+ <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
+</head>
+<body>
+<script>
+"use strict";
+
+window.onload = function() {
+ const { require } = ChromeUtils.importESModule("resource://devtools/shared/loader/Loader.sys.mjs");
+ const WebSocketServer = require("devtools/server/socket/websocket-server");
+
+ const ServerSocket = Components.Constructor("@mozilla.org/network/server-socket;1",
+ "nsIServerSocket", "init");
+
+ add_task(async function() {
+ // Create a TCP server on auto-assigned port
+ const server = new ServerSocket(-1, true, -1);
+ ok(server, `Launched WebSocket server on port ${server.port}`);
+
+ let input, output;
+
+ server.asyncListen({
+ async onSocketAccepted(socket, transport) {
+ info("Accepted incoming connection");
+ input = transport.openInputStream(0, 0, 0);
+ output = transport.openOutputStream(0, 0, 0);
+
+ // Perform the WebSocket handshake
+ const webSocket = await WebSocketServer.accept(transport, input, output);
+
+ // Echo the received message back to the sender
+ webSocket.onmessage = ({ data }) => {
+ info("Server received message, echoing back");
+ webSocket.send(data);
+ };
+ },
+
+ onStopListening(socket, status) {
+ info(`Server stopped listening with status: ${status}`);
+ },
+ });
+
+ SimpleTest.registerCleanupFunction(() => {
+ server.close();
+ });
+
+ // Create client connection
+ const client = await new Promise((resolve, reject) => {
+ const socket = new WebSocket(`ws://localhost:${server.port}`);
+ socket.onopen = () => resolve(socket);
+ socket.onerror = reject;
+ });
+ ok(client, `Created WebSocket connection to port ${server.port}`);
+
+ // Create a promise that resolves when the WebSocket closes
+ const closed = new Promise(resolve => {
+ client.onclose = resolve;
+ });
+
+ // Send a message
+ const message = "hello there";
+ client.send(message);
+ info("Sent a message to server");
+ // Check that it was echoed
+ const echoedMessage = await new Promise((resolve, reject) => {
+ client.onmessage = ({ data }) => resolve(data);
+ client.onerror = reject;
+ });
+
+ is(echoedMessage, message, "Echoed message matches");
+
+ // Close the connection
+ client.close();
+ await closed;
+
+ // Prevent leaking the streams by closing them before test ends
+ input.close();
+ output.close();
+ });
+};
+</script>
+</body>
+</html>