
Security News
Attackers Are Hunting High-Impact Node.js Maintainers in a Coordinated Social Engineering Campaign
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.
Socket.io endpoints testing made easy.
The motivation with this module is to provide a high-level abstraction for testing Websocket endpoints.
Install WsReq as a development dependency:
npm i -D wsreq
or
yarn add -D wsreq
Once you installed, it can now be referenced by simply calling require('wsreq') or using import statements.
When you call the wsrequest function it returns an array with all the websocket connections requested in the options.
You may pass a reference of the http.Server or an URI to the wsrequest function.
If you pass a http.Server It will bound the server to a ephemeral port so you don't need to keep track of ports.
WsReq works with any test framework, here are some examples with jest:
import { wsrequest } from "wsreq";
import app from "../server/path";
test("should be able to connect.", async () => {
const [conn] = await wsrequest(app, { config: { path: "/ws/path" } });
expect(conn.connection.connected).toEqual(true);
expect(conn.connection.id).toBeDefined();
conn.close();
});
test("should respond with msg.", async () => {
const [conn] = await wsrequest(app, { config: { path } });
const res = await conn.emit("ping", data).on("pong");
expect(res).toEqual(data);
conn.close();
});
test("should respond with msg (using http). ", async () => {
// method: "get" | "post" | "put" | "delete";
const [conn] = await wsrequest(app, { config: { path } });
const { ws: ws_response, http: http_response } = await conn.onWithHttp(
"ws-event",
{
url: "/endpoint/url",
method: "post",
body: { ...someData },
headers: { ...someHeaders },
}
);
expect(ws_response).toEquals({ ...compareData1 });
expect(http_response).toEquals({ ...compareData2 });
conn.close();
});
test("should respond with msg. (SEND)", async () => {
const [conn] = await wsrequest(app, { config: { path } });
const r = await conn.send(data);
expect(r).toEqual(someData);
conn.close();
});
test("should respond with msg. (MULTIPLE)", async () => {
const [conn] = await wsrequest(app, { config: { path: "/ws/path" } });
const r = await conn.multiple(async ({ http, ws }) => {
const hget = await http.get("/get/");
const hpost = await http.post("/post/", {
body: { ...data },
});
const hdelete = await http.delete("/delete/", {
body: { ...data },
});
const hput = await http.put("/put/", {
body: { ...data },
});
const wr = await ws.emit("ping", data).on("pong");
return {
hget,
hpost,
hdelete,
hput,
wr,
};
});
expect(r.wr).toEqual(someData);
expect(r.hget).toEqual(someData);
expect(r.hpost).toEqual(someData);
expect(r.hdelete).toEqual(someData);
expect(r.hput).toEqual(someData);
conn.close();
});
test("should respond with msg.", async () => {
const [conn1, conn2] = await wsrequest(app, { clients: 2, config: { path } });
const res1 = await conn1.emit("ping", data).on("pong");
const res2 = await conn2.emit("ping", data).on("pong");
expect(res1).toEqual(data);
expect(res2).toEqual(data);
conn2.close();
conn2.close();
});
test("should fail with status code 404.", async () => {
// method: "get" | "post" | "put" | "delete";
const [conn] = await wsrequest(app, { config: { path } });
const res = await conn
.onWithHttp("ws-event", {
url: "/invalid/endpoint/url",
method: "get",
headers: { ...someHeaders },
})
.catch((e: Error) => {
return {
msg: e.message,
};
});
expect(res).toEquals({ msg: "Request failed with status code 404" });
conn.close();
});
test("should fail with invalid ws event.", async () => {
const [conn] = await wsrequest(app, { config: { path } });
conn.emit("ping", data);
const res = await conn.on("no-pong").catch((e: Error) => {
return {
msg: e.message,
};
});
expect(res).toEquals({ msg: "Invalid WS event." });
con.close();
});
If you wanna test a remote server, you should use the remote function instead of local and pass the uri as a parameter.
const { wsrequest } = require("wsreq");
const uri = "http://example.com";
test("should be able to connect.", async () => {
const [conn] = await wsrequest(uri, { config: { path: "/ws/path" } });
expect(conn.connection.connected).toEqual(true);
expect(conn.connection.id).toBeDefined();
conn.close();
});
Everything else works just the same.
http server.async ({ http, ws }) => Promise<data>
httpServer is set to true, it closes the http.Server too (local only).httpServer is set to true for default, change if you only need to close the websocket.I just wrote it because I need it. ;)
Inspired by SuperTest.
MIT
FAQs
Socket.io endpoints testing made easy.
We found that wsreq demonstrated a not healthy version release cadence and project activity because the last version was released a year ago. It has 1 open source maintainer collaborating on the project.
Did you know?

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Security News
Multiple high-impact npm maintainers confirm they have been targeted in the same social engineering campaign that compromised Axios.

Security News
Axios compromise traced to social engineering, showing how attacks on maintainers can bypass controls and expose the broader software supply chain.

Security News
Node.js has paused its bug bounty program after funding ended, removing payouts for vulnerability reports but keeping its security process unchanged.