WS test status code support + improved error handing + subprotocol input

This commit is contained in:
PoleTransformer
2025-12-30 17:49:09 -08:00
parent 271a2d3b35
commit 0bee90ea31
4 changed files with 167 additions and 117 deletions
+86 -8
View File
@@ -6,7 +6,7 @@ const { UP, PENDING } = require("../../src/util");
describe("Websocket Test", {
}, () => {
test("Non Websocket Server", {}, async () => {
test("Non WS Server", {}, async () => {
const websocketMonitor = new WebSocketMonitorType();
const monitor = {
@@ -25,12 +25,13 @@ describe("Websocket Test", {
);
});
test("Secure Websocket", async () => {
test("Secure WS", async () => {
const websocketMonitor = new WebSocketMonitorType();
const monitor = {
url: "wss://echo.websocket.org",
wsIgnoreSecWebsocketAcceptHeader: false,
accepted_statuscodes_json: JSON.stringify([ "1000" ]),
};
const heartbeat = {
@@ -39,7 +40,7 @@ describe("Websocket Test", {
};
const expected = {
msg: "101 - OK",
msg: "1000 - OK",
status: UP,
};
@@ -47,7 +48,7 @@ describe("Websocket Test", {
assert.deepStrictEqual(heartbeat, expected);
});
test("Insecure Websocket", async (t) => {
test("Insecure WS", async (t) => {
t.after(() => wss.close());
const websocketMonitor = new WebSocketMonitorType();
const wss = new WebSocketServer({ port: 8080 });
@@ -55,6 +56,7 @@ describe("Websocket Test", {
const monitor = {
url: "ws://localhost:8080",
wsIgnoreSecWebsocketAcceptHeader: false,
accepted_statuscodes_json: JSON.stringify([ "1000" ]),
};
const heartbeat = {
@@ -63,7 +65,7 @@ describe("Websocket Test", {
};
const expected = {
msg: "101 - OK",
msg: "1000 - OK",
status: UP,
};
@@ -71,12 +73,33 @@ describe("Websocket Test", {
assert.deepStrictEqual(heartbeat, expected);
});
test("Non compliant WS Server wrong status code", async () => {
const websocketMonitor = new WebSocketMonitorType();
const monitor = {
url: "wss://echo.websocket.org",
wsIgnoreSecWebsocketAcceptHeader: false,
accepted_statuscodes_json: JSON.stringify([ "1001" ]),
};
const heartbeat = {
msg: "",
status: PENDING,
};
await assert.rejects(
websocketMonitor.check(monitor, heartbeat, {}),
new Error("Unexpected status code: 1000")
);
});
test("Non compliant WS server without IgnoreSecWebsocket", async () => {
const websocketMonitor = new WebSocketMonitorType();
const monitor = {
url: "wss://c.img-cdn.net/yE4s7KehTFyj/",
wsIgnoreSecWebsocketAcceptHeader: false,
accepted_statuscodes_json: JSON.stringify([ "1000" ]),
};
const heartbeat = {
@@ -96,6 +119,7 @@ describe("Websocket Test", {
const monitor = {
url: "wss://c.img-cdn.net/yE4s7KehTFyj/",
wsIgnoreSecWebsocketAcceptHeader: true,
accepted_statuscodes_json: JSON.stringify([ "1000" ]),
};
const heartbeat = {
@@ -104,7 +128,7 @@ describe("Websocket Test", {
};
const expected = {
msg: "101 - OK",
msg: "1000 - OK",
status: UP,
};
@@ -118,6 +142,7 @@ describe("Websocket Test", {
const monitor = {
url: "wss://echo.websocket.org",
wsIgnoreSecWebsocketAcceptHeader: true,
accepted_statuscodes_json: JSON.stringify([ "1000" ]),
};
const heartbeat = {
@@ -126,7 +151,7 @@ describe("Websocket Test", {
};
const expected = {
msg: "101 - OK",
msg: "1000 - OK",
status: UP,
};
@@ -140,6 +165,7 @@ describe("Websocket Test", {
const monitor = {
url: "wss://example.org",
wsIgnoreSecWebsocketAcceptHeader: true,
accepted_statuscodes_json: JSON.stringify([ "1000" ]),
};
const heartbeat = {
@@ -153,13 +179,14 @@ describe("Websocket Test", {
);
});
test("Secure Websocket with Subprotocol", async () => {
test("Secure WS no support one subprotocol", async () => {
const websocketMonitor = new WebSocketMonitorType();
const monitor = {
url: "wss://echo.websocket.org",
wsIgnoreSecWebsocketAcceptHeader: false,
wsSubprotocol: "ocpp1.6",
accepted_statuscodes_json: JSON.stringify([ "1000" ]),
};
const heartbeat = {
@@ -172,4 +199,55 @@ describe("Websocket Test", {
new Error("Server sent no subprotocol")
);
});
test("Multiple subprotocols bad input", async () => {
const websocketMonitor = new WebSocketMonitorType();
const monitor = {
url: "wss://echo.websocket.org",
wsIgnoreSecWebsocketAcceptHeader: false,
wsSubprotocol: " ocpp2.0 , ocpp1.6 , ",
accepted_statuscodes_json: JSON.stringify([ "1000" ]),
};
const heartbeat = {
msg: "",
status: PENDING,
};
await assert.rejects(
websocketMonitor.check(monitor, heartbeat, {}),
new SyntaxError("An invalid or duplicated subprotocol was specified")
);
});
test("Insecure WS support one subprotocol", async (t) => {
t.after(() => wss.close());
const websocketMonitor = new WebSocketMonitorType();
const wss = new WebSocketServer({ port: 8080,
handleProtocols: (protocols) => {
return Array.from(protocols).includes("test") ? "test" : null;
}
});
const monitor = {
url: "ws://localhost:8080",
wsIgnoreSecWebsocketAcceptHeader: false,
wsSubprotocol: "invalid,test",
accepted_statuscodes_json: JSON.stringify([ "1000" ]),
};
const heartbeat = {
msg: "",
status: PENDING,
};
const expected = {
msg: "1000 - OK",
status: UP,
};
await websocketMonitor.check(monitor, heartbeat, {});
assert.deepStrictEqual(heartbeat, expected);
});
});