2025-02-18 17:26:52 -08:00
|
|
|
const { WebSocketServer } = require("ws");
|
2025-02-17 18:01:33 -08:00
|
|
|
const { describe, test } = require("node:test");
|
2025-02-17 17:04:58 -08:00
|
|
|
const assert = require("node:assert");
|
2025-02-18 16:03:32 -08:00
|
|
|
const { WebSocketMonitorType } = require("../../server/monitor-types/websocket-upgrade");
|
2025-02-17 17:04:58 -08:00
|
|
|
const { UP, DOWN, PENDING } = require("../../src/util");
|
|
|
|
|
2025-02-17 18:01:33 -08:00
|
|
|
describe("Websocket Test", {
|
|
|
|
}, () => {
|
2025-02-17 19:06:10 -08:00
|
|
|
test("Non Websocket Server", {}, async () => {
|
2025-02-18 16:03:32 -08:00
|
|
|
const websocketMonitor = new WebSocketMonitorType();
|
2025-02-17 18:01:33 -08:00
|
|
|
|
|
|
|
const monitor = {
|
|
|
|
wsurl: "wss://example.org",
|
|
|
|
wsIgnoreHeaders: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const heartbeat = {
|
|
|
|
msg: "",
|
|
|
|
status: PENDING,
|
|
|
|
};
|
|
|
|
|
2025-02-18 16:29:19 -08:00
|
|
|
const expected = {
|
|
|
|
msg: "Unexpected server response: 200",
|
|
|
|
status: DOWN,
|
|
|
|
};
|
|
|
|
|
2025-02-17 18:01:33 -08:00
|
|
|
await websocketMonitor.check(monitor, heartbeat, {});
|
2025-02-18 16:29:19 -08:00
|
|
|
assert.deepStrictEqual(heartbeat, expected);
|
2025-02-17 18:01:33 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("Secure Websocket", async () => {
|
2025-02-18 16:03:32 -08:00
|
|
|
const websocketMonitor = new WebSocketMonitorType();
|
2025-02-17 18:01:33 -08:00
|
|
|
|
|
|
|
const monitor = {
|
|
|
|
wsurl: "wss://echo.websocket.org",
|
|
|
|
wsIgnoreHeaders: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const heartbeat = {
|
|
|
|
msg: "",
|
|
|
|
status: PENDING,
|
|
|
|
};
|
|
|
|
|
2025-02-18 16:29:19 -08:00
|
|
|
const expected = {
|
|
|
|
msg: "101 - OK",
|
|
|
|
status: UP,
|
|
|
|
};
|
|
|
|
|
2025-02-17 18:01:33 -08:00
|
|
|
await websocketMonitor.check(monitor, heartbeat, {});
|
2025-02-18 16:29:19 -08:00
|
|
|
assert.deepStrictEqual(heartbeat, expected);
|
2025-02-17 18:01:33 -08:00
|
|
|
});
|
|
|
|
|
2025-02-18 17:26:52 -08:00
|
|
|
test("Insecure Websocket", async (t) => {
|
|
|
|
t.after(() => wss.close());
|
2025-02-18 16:03:32 -08:00
|
|
|
const websocketMonitor = new WebSocketMonitorType();
|
2025-02-18 17:26:52 -08:00
|
|
|
const wss = new WebSocketServer({ port: 8080 });
|
2025-02-17 18:01:33 -08:00
|
|
|
|
|
|
|
const monitor = {
|
2025-02-18 17:26:52 -08:00
|
|
|
wsurl: "ws://localhost:8080",
|
2025-02-17 18:01:33 -08:00
|
|
|
wsIgnoreHeaders: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const heartbeat = {
|
|
|
|
msg: "",
|
|
|
|
status: PENDING,
|
|
|
|
};
|
|
|
|
|
2025-02-18 16:29:19 -08:00
|
|
|
const expected = {
|
|
|
|
msg: "101 - OK",
|
|
|
|
status: UP,
|
|
|
|
};
|
|
|
|
|
2025-02-17 18:01:33 -08:00
|
|
|
await websocketMonitor.check(monitor, heartbeat, {});
|
2025-02-18 16:29:19 -08:00
|
|
|
assert.deepStrictEqual(heartbeat, expected);
|
2025-02-17 18:01:33 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("Test a non compliant WS server without ignore", async () => {
|
2025-02-18 16:03:32 -08:00
|
|
|
const websocketMonitor = new WebSocketMonitorType();
|
2025-02-17 18:01:33 -08:00
|
|
|
|
|
|
|
const monitor = {
|
|
|
|
wsurl: "wss://c.img-cdn.net/yE4s7KehTFyj/",
|
|
|
|
wsIgnoreHeaders: false,
|
|
|
|
};
|
|
|
|
|
|
|
|
const heartbeat = {
|
|
|
|
msg: "",
|
|
|
|
status: PENDING,
|
|
|
|
};
|
|
|
|
|
2025-02-18 16:29:19 -08:00
|
|
|
const expected = {
|
|
|
|
msg: "Invalid Sec-WebSocket-Accept header",
|
|
|
|
status: DOWN,
|
|
|
|
};
|
|
|
|
|
2025-02-17 18:01:33 -08:00
|
|
|
await websocketMonitor.check(monitor, heartbeat, {});
|
2025-02-18 16:29:19 -08:00
|
|
|
assert.deepStrictEqual(heartbeat, expected);
|
2025-02-17 18:01:33 -08:00
|
|
|
});
|
|
|
|
|
|
|
|
test("Test a non compliant WS server with ignore", async () => {
|
2025-02-18 16:03:32 -08:00
|
|
|
const websocketMonitor = new WebSocketMonitorType();
|
2025-02-17 18:01:33 -08:00
|
|
|
|
|
|
|
const monitor = {
|
|
|
|
wsurl: "wss://c.img-cdn.net/yE4s7KehTFyj/",
|
|
|
|
wsIgnoreHeaders: true,
|
|
|
|
};
|
|
|
|
|
|
|
|
const heartbeat = {
|
|
|
|
msg: "",
|
|
|
|
status: PENDING,
|
|
|
|
};
|
|
|
|
|
2025-02-18 16:29:19 -08:00
|
|
|
const expected = {
|
|
|
|
msg: "101 - OK",
|
|
|
|
status: UP,
|
|
|
|
};
|
|
|
|
|
2025-02-17 18:01:33 -08:00
|
|
|
await websocketMonitor.check(monitor, heartbeat, {});
|
2025-02-18 16:29:19 -08:00
|
|
|
assert.deepStrictEqual(heartbeat, expected);
|
2025-02-17 18:01:33 -08:00
|
|
|
});
|
2025-02-17 17:04:58 -08:00
|
|
|
});
|