summaryrefslogtreecommitdiffstats
path: root/devtools/server/tests/mochitest/test_websocket-server.html
diff options
context:
space:
mode:
authorMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
committerMatt A. Tobin <mattatobin@localhost.localdomain>2018-02-02 04:16:08 -0500
commit5f8de423f190bbb79a62f804151bc24824fa32d8 (patch)
tree10027f336435511475e392454359edea8e25895d /devtools/server/tests/mochitest/test_websocket-server.html
parent49ee0794b5d912db1f95dce6eb52d781dc210db5 (diff)
downloadUXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.gz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.lz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.tar.xz
UXP-5f8de423f190bbb79a62f804151bc24824fa32d8.zip
Add m-esr52 at 52.6.0
Diffstat (limited to 'devtools/server/tests/mochitest/test_websocket-server.html')
-rw-r--r--devtools/server/tests/mochitest/test_websocket-server.html82
1 files changed, 82 insertions, 0 deletions
diff --git a/devtools/server/tests/mochitest/test_websocket-server.html b/devtools/server/tests/mochitest/test_websocket-server.html
new file mode 100644
index 000000000..583d96dd9
--- /dev/null
+++ b/devtools/server/tests/mochitest/test_websocket-server.html
@@ -0,0 +1,82 @@
+<!DOCTYPE HTML>
+<html>
+<head>
+ <meta charset="utf-8">
+ <title>Mozilla Bug</title>
+ <script src="chrome://mochikit/content/tests/SimpleTest/SimpleTest.js"></script>
+ <script src="chrome://mochikit/content/tests/SimpleTest/SpawnTask.js"></script>
+ <link rel="stylesheet" type="text/css" href="chrome://mochikit/content/tests/SimpleTest/test.css">
+</head>
+<body>
+<script>
+window.onload = function() {
+ const { Constructor: CC, utils: Cu } = Components;
+ const { require } = Cu.import("resource://devtools/shared/Loader.jsm", {});
+ const { Task } = require("devtools/shared/task");
+ const WebSocketServer = require("devtools/server/websocket-server");
+
+ const ServerSocket = CC("@mozilla.org/network/server-socket;1",
+ "nsIServerSocket", "init");
+
+ add_task(function* () {
+ // Create a TCP server on auto-assigned port
+ let server = new ServerSocket(-1, true, -1);
+ ok(server, `Launched WebSocket server on port ${server.port}`);
+ server.asyncListen({
+ onSocketAccepted: Task.async(function* (socket, transport) {
+ info("Accepted incoming connection");
+ let input = transport.openInputStream(0, 0, 0);
+ let output = transport.openOutputStream(0, 0, 0);
+
+ // Perform the WebSocket handshake
+ let webSocket = yield 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
+ let client = yield new Promise((resolve, reject) => {
+ let 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
+ let closed = new Promise(resolve => {
+ client.onclose = resolve;
+ });
+
+ // Send a message
+ let message = "hello there";
+ client.send(message);
+ info("Sent a message to server");
+ // Check that it was echoed
+ let echoedMessage = yield new Promise((resolve, reject) => {
+ client.onmessage = ({ data }) => resolve(data);
+ client.onerror = reject;
+ });
+
+ is(echoedMessage, message, "Echoed message matches");
+
+ // Close the connection
+ client.close();
+ yield closed;
+ });
+}
+</script>
+</body>
+</html>