From dcb07a5e2e96934dc5717676f3ae11dfbe8e8863 Mon Sep 17 00:00:00 2001 From: Your Name Date: Mon, 17 Feb 2025 18:01:33 -0800 Subject: [PATCH] update tests --- test/backend-test/test-websocket.js | 157 ++++++++++++++++------------ 1 file changed, 89 insertions(+), 68 deletions(-) diff --git a/test/backend-test/test-websocket.js b/test/backend-test/test-websocket.js index 363406428..d698a6184 100644 --- a/test/backend-test/test-websocket.js +++ b/test/backend-test/test-websocket.js @@ -1,76 +1,97 @@ -const test = require("node:test"); +const { describe, test } = require("node:test"); const assert = require("node:assert"); const { websocket } = require("../../server/monitor-types/websocket"); const { UP, DOWN, PENDING } = require("../../src/util"); -test("Test Websocket TLS", async () => { - const websocketMonitor = new websocket(); +describe("Websocket Test", { +}, () => { + test("Non Websocket Server", async () => { + const websocketMonitor = new websocket(); - const monitor = { - wsurl: "wss://echo.websocket.org", - wsIgnoreHeaders: false, - }; + const monitor = { + wsurl: "wss://example.org", + wsIgnoreHeaders: false, + }; - const heartbeat = { - msg: "", - status: PENDING, - }; + const heartbeat = { + msg: "", + status: PENDING, + }; - await websocketMonitor.check(monitor, heartbeat, {}); - assert.strictEqual(heartbeat.status, UP); - assert.strictEqual(heartbeat.msg, "101 - OK"); -}); - -test("Test Websocket non TLS", async () => { - const websocketMonitor = new websocket(); - - const monitor = { - wsurl: "ws://ws.ifelse.io", - wsIgnoreHeaders: false, - }; - - const heartbeat = { - msg: "", - status: PENDING, - }; - - await websocketMonitor.check(monitor, heartbeat, {}); - assert.strictEqual(heartbeat.status, UP); - assert.strictEqual(heartbeat.msg, "101 - OK"); -}); - -test("Test a non compliant WS server without ignore", async () => { - const websocketMonitor = new websocket(); - - const monitor = { - wsurl: "wss://c.img-cdn.net/yE4s7KehTFyj/", - wsIgnoreHeaders: false, - }; - - const heartbeat = { - msg: "", - status: PENDING, - }; - - await websocketMonitor.check(monitor, heartbeat, {}); - assert.strictEqual(heartbeat.status, DOWN); - assert.strictEqual(heartbeat.msg, "Invalid Sec-WebSocket-Accept header"); -}); - -test("Test a non compliant WS server with ignore", async () => { - const websocketMonitor = new websocket(); - - const monitor = { - wsurl: "wss://c.img-cdn.net/yE4s7KehTFyj/", - wsIgnoreHeaders: true, - }; - - const heartbeat = { - msg: "", - status: PENDING, - }; - - await websocketMonitor.check(monitor, heartbeat, {}); - assert.strictEqual(heartbeat.status, UP); - assert.strictEqual(heartbeat.msg, "101 - OK"); + await websocketMonitor.check(monitor, heartbeat, {}); + assert.strictEqual(heartbeat.status, DOWN); + assert.strictEqual(heartbeat.msg, "Unexpected server response: 200"); + }); + + test("Secure Websocket", async () => { + const websocketMonitor = new websocket(); + + const monitor = { + wsurl: "wss://echo.websocket.org", + wsIgnoreHeaders: false, + }; + + const heartbeat = { + msg: "", + status: PENDING, + }; + + await websocketMonitor.check(monitor, heartbeat, {}); + assert.strictEqual(heartbeat.status, UP); + assert.strictEqual(heartbeat.msg, "101 - OK"); + }); + + test("Insecure Websocket", async () => { + const websocketMonitor = new websocket(); + + const monitor = { + wsurl: "ws://ws.ifelse.io", + wsIgnoreHeaders: false, + }; + + const heartbeat = { + msg: "", + status: PENDING, + }; + + await websocketMonitor.check(monitor, heartbeat, {}); + assert.strictEqual(heartbeat.status, UP); + assert.strictEqual(heartbeat.msg, "101 - OK"); + }); + + test("Test a non compliant WS server without ignore", async () => { + const websocketMonitor = new websocket(); + + const monitor = { + wsurl: "wss://c.img-cdn.net/yE4s7KehTFyj/", + wsIgnoreHeaders: false, + }; + + const heartbeat = { + msg: "", + status: PENDING, + }; + + await websocketMonitor.check(monitor, heartbeat, {}); + assert.strictEqual(heartbeat.status, DOWN); + assert.strictEqual(heartbeat.msg, "Invalid Sec-WebSocket-Accept header"); + }); + + test("Test a non compliant WS server with ignore", async () => { + const websocketMonitor = new websocket(); + + const monitor = { + wsurl: "wss://c.img-cdn.net/yE4s7KehTFyj/", + wsIgnoreHeaders: true, + }; + + const heartbeat = { + msg: "", + status: PENDING, + }; + + await websocketMonitor.check(monitor, heartbeat, {}); + assert.strictEqual(heartbeat.status, UP); + assert.strictEqual(heartbeat.msg, "101 - OK"); + }); });