Skip to content

Commit 713a6b4

Browse files
chore: bump mocha to version 10.0.0
Related: #3710
1 parent 18f3fda commit 713a6b4

16 files changed

+3914
-3677
lines changed

package-lock.json

Lines changed: 866 additions & 363 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
"compile": "rimraf ./dist && tsc",
4141
"test": "npm run format:check && npm run compile && npm run test:types && npm run test:unit",
4242
"test:types": "tsd",
43-
"test:unit": "nyc mocha --require ts-node/register --reporter spec --slow 200 --bail --timeout 10000 test/socket.io.ts",
43+
"test:unit": "nyc mocha --require ts-node/register --reporter spec --slow 200 --bail --timeout 10000 test/index.ts",
4444
"format:check": "prettier --check \"lib/**/*.ts\" \"test/**/*.ts\"",
4545
"format:fix": "prettier --write \"lib/**/*.ts\" \"test/**/*.ts\"",
4646
"prepack": "npm run compile"
@@ -56,7 +56,7 @@
5656
"devDependencies": {
5757
"@types/mocha": "^9.0.0",
5858
"expect.js": "0.3.1",
59-
"mocha": "^3.5.3",
59+
"mocha": "^10.0.0",
6060
"nyc": "^15.1.0",
6161
"prettier": "^2.3.2",
6262
"rimraf": "^3.0.2",

test/close.ts

Lines changed: 181 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,181 @@
1+
import { createServer } from "http";
2+
import { io as ioc } from "socket.io-client";
3+
import { join } from "path";
4+
import { exec } from "child_process";
5+
import { Server } from "..";
6+
import expect from "expect.js";
7+
import { createClient, getPort } from "./support/util";
8+
import request from "supertest";
9+
10+
// TODO: update superagent as latest release now supports promises
11+
const eioHandshake = (httpServer): Promise<string> => {
12+
return new Promise((resolve) => {
13+
request(httpServer)
14+
.get("/socket.io/")
15+
.query({ transport: "polling", EIO: 4 })
16+
.end((err, res) => {
17+
const sid = JSON.parse(res.text.substring(1)).sid;
18+
resolve(sid);
19+
});
20+
});
21+
};
22+
23+
const eioPush = (httpServer, sid: string, body: string): Promise<void> => {
24+
return new Promise((resolve) => {
25+
request(httpServer)
26+
.post("/socket.io/")
27+
.send(body)
28+
.query({ transport: "polling", EIO: 4, sid })
29+
.expect(200)
30+
.end(() => {
31+
resolve();
32+
});
33+
});
34+
};
35+
36+
const eioPoll = (httpServer, sid): Promise<string> => {
37+
return new Promise((resolve) => {
38+
request(httpServer)
39+
.get("/socket.io/")
40+
.query({ transport: "polling", EIO: 4, sid })
41+
.expect(200)
42+
.end((err, res) => {
43+
resolve(res.text);
44+
});
45+
});
46+
};
47+
48+
describe("close", () => {
49+
it("should be able to close sio sending a srv", (done) => {
50+
const httpServer = createServer().listen(0);
51+
const io = new Server(httpServer);
52+
const port = getPort(io);
53+
const net = require("net");
54+
const server = net.createServer();
55+
56+
const clientSocket = createClient(io, "/", { reconnection: false });
57+
58+
clientSocket.on("disconnect", () => {
59+
expect(io.sockets.sockets.size).to.equal(0);
60+
server.listen(port);
61+
});
62+
63+
clientSocket.on("connect", () => {
64+
expect(io.sockets.sockets.size).to.equal(1);
65+
io.close();
66+
});
67+
68+
server.once("listening", () => {
69+
// PORT should be free
70+
server.close((error) => {
71+
expect(error).to.be(undefined);
72+
done();
73+
});
74+
});
75+
});
76+
77+
it("should be able to close sio sending a srv", (done) => {
78+
const io = new Server(0);
79+
const port = getPort(io);
80+
const net = require("net");
81+
const server = net.createServer();
82+
83+
const clientSocket = ioc("ws://0.0.0.0:" + port, {
84+
reconnection: false,
85+
});
86+
87+
clientSocket.on("disconnect", () => {
88+
expect(io.sockets.sockets.size).to.equal(0);
89+
server.listen(port);
90+
});
91+
92+
clientSocket.on("connect", () => {
93+
expect(io.sockets.sockets.size).to.equal(1);
94+
io.close();
95+
});
96+
97+
server.once("listening", () => {
98+
// PORT should be free
99+
server.close((error) => {
100+
expect(error).to.be(undefined);
101+
done();
102+
});
103+
});
104+
});
105+
106+
describe("graceful close", () => {
107+
function fixture(filename) {
108+
return (
109+
'"' +
110+
process.execPath +
111+
'" "' +
112+
join(__dirname, "fixtures", filename) +
113+
'"'
114+
);
115+
}
116+
117+
it("should stop socket and timers", (done) => {
118+
exec(fixture("server-close.ts"), done);
119+
});
120+
});
121+
122+
describe("protocol violations", () => {
123+
it("should close the connection when receiving several CONNECT packets", async () => {
124+
const httpServer = createServer();
125+
const io = new Server(httpServer);
126+
127+
httpServer.listen(0);
128+
129+
const sid = await eioHandshake(httpServer);
130+
// send a first CONNECT packet
131+
await eioPush(httpServer, sid, "40");
132+
// send another CONNECT packet
133+
await eioPush(httpServer, sid, "40");
134+
// session is cleanly closed (not discarded, see 'client.close()')
135+
// first, we receive the Socket.IO handshake response
136+
await eioPoll(httpServer, sid);
137+
// then a close packet
138+
const body = await eioPoll(httpServer, sid);
139+
expect(body).to.be("6\u001e1");
140+
141+
io.close();
142+
});
143+
144+
it("should close the connection when receiving an EVENT packet while not connected", async () => {
145+
const httpServer = createServer();
146+
const io = new Server(httpServer);
147+
148+
httpServer.listen(0);
149+
150+
const sid = await eioHandshake(httpServer);
151+
// send an EVENT packet
152+
await eioPush(httpServer, sid, '42["some event"]');
153+
// session is cleanly closed, we receive a close packet
154+
const body = await eioPoll(httpServer, sid);
155+
expect(body).to.be("6\u001e1");
156+
157+
io.close();
158+
});
159+
160+
it("should close the connection when receiving an invalid packet", async () => {
161+
const httpServer = createServer();
162+
const io = new Server(httpServer);
163+
164+
httpServer.listen(0);
165+
166+
const sid = await eioHandshake(httpServer);
167+
// send a CONNECT packet
168+
await eioPush(httpServer, sid, "40");
169+
// send an invalid packet
170+
await eioPush(httpServer, sid, "4abc");
171+
// session is cleanly closed (not discarded, see 'client.close()')
172+
// first, we receive the Socket.IO handshake response
173+
await eioPoll(httpServer, sid);
174+
// then a close packet
175+
const body = await eioPoll(httpServer, sid);
176+
expect(body).to.be("6\u001e1");
177+
178+
io.close();
179+
});
180+
});
181+
});

test/handshake.ts

Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
import { Server } from "..";
2+
import expect from "expect.js";
3+
import { getPort, success } from "./support/util";
4+
5+
describe("handshake", () => {
6+
const request = require("superagent");
7+
8+
it("should send the Access-Control-Allow-xxx headers on OPTIONS request", (done) => {
9+
const io = new Server(0, {
10+
cors: {
11+
origin: "http://localhost:54023",
12+
methods: ["GET", "POST"],
13+
allowedHeaders: ["content-type"],
14+
credentials: true,
15+
},
16+
});
17+
request
18+
.options(`http://localhost:${getPort(io)}/socket.io/default/`)
19+
.query({ transport: "polling", EIO: 4 })
20+
.set("Origin", "http://localhost:54023")
21+
.end((err, res) => {
22+
expect(res.status).to.be(204);
23+
24+
expect(res.headers["access-control-allow-origin"]).to.be(
25+
"http://localhost:54023"
26+
);
27+
expect(res.headers["access-control-allow-methods"]).to.be("GET,POST");
28+
expect(res.headers["access-control-allow-headers"]).to.be(
29+
"content-type"
30+
);
31+
expect(res.headers["access-control-allow-credentials"]).to.be("true");
32+
success(done, io);
33+
});
34+
});
35+
36+
it("should send the Access-Control-Allow-xxx headers on GET request", (done) => {
37+
const io = new Server(0, {
38+
cors: {
39+
origin: "http://localhost:54024",
40+
methods: ["GET", "POST"],
41+
allowedHeaders: ["content-type"],
42+
credentials: true,
43+
},
44+
});
45+
request
46+
.get(`http://localhost:${getPort(io)}/socket.io/default/`)
47+
.query({ transport: "polling", EIO: 4 })
48+
.set("Origin", "http://localhost:54024")
49+
.end((err, res) => {
50+
expect(res.status).to.be(200);
51+
52+
expect(res.headers["access-control-allow-origin"]).to.be(
53+
"http://localhost:54024"
54+
);
55+
expect(res.headers["access-control-allow-credentials"]).to.be("true");
56+
success(done, io);
57+
});
58+
});
59+
60+
it("should allow request if custom function in opts.allowRequest returns true", (done) => {
61+
const io = new Server(0, {
62+
allowRequest: (req, callback) => callback(null, true),
63+
});
64+
65+
request
66+
.get(`http://localhost:${getPort(io)}/socket.io/default/`)
67+
.query({ transport: "polling", EIO: 4 })
68+
.end((err, res) => {
69+
expect(res.status).to.be(200);
70+
success(done, io);
71+
});
72+
});
73+
74+
it("should disallow request if custom function in opts.allowRequest returns false", (done) => {
75+
const io = new Server(0, {
76+
allowRequest: (req, callback) => callback(null, false),
77+
});
78+
request
79+
.get(`http://localhost:${getPort(io)}/socket.io/default/`)
80+
.set("origin", "http://foo.example")
81+
.query({ transport: "polling", EIO: 4 })
82+
.end((err, res) => {
83+
expect(res.status).to.be(403);
84+
success(done, io);
85+
});
86+
});
87+
});

test/index.ts

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
"use strict";
2+
3+
import expect from "expect.js";
4+
5+
describe("socket.io", () => {
6+
it("should be the same version as client", () => {
7+
const version = require("../package").version;
8+
expect(version).to.be(require("socket.io-client/package.json").version);
9+
});
10+
11+
require("./server-attachment");
12+
require("./handshake");
13+
require("./close");
14+
require("./namespaces");
15+
require("./socket");
16+
require("./messaging-many");
17+
require("./middleware");
18+
require("./socket-middleware");
19+
require("./v2-compatibility");
20+
require("./socket-timeout");
21+
require("./uws");
22+
require("./utility-methods");
23+
});

0 commit comments

Comments
 (0)