Skip to content

Commit

Permalink
chore: bump mocha to version 10.0.0
Browse files Browse the repository at this point in the history
Related: #3710
  • Loading branch information
darrachequesne committed Jun 27, 2022
1 parent 18f3fda commit 713a6b4
Show file tree
Hide file tree
Showing 16 changed files with 3,914 additions and 3,677 deletions.
1,229 changes: 866 additions & 363 deletions package-lock.json

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@
"compile": "rimraf ./dist && tsc",
"test": "npm run format:check && npm run compile && npm run test:types && npm run test:unit",
"test:types": "tsd",
"test:unit": "nyc mocha --require ts-node/register --reporter spec --slow 200 --bail --timeout 10000 test/socket.io.ts",
"test:unit": "nyc mocha --require ts-node/register --reporter spec --slow 200 --bail --timeout 10000 test/index.ts",
"format:check": "prettier --check \"lib/**/*.ts\" \"test/**/*.ts\"",
"format:fix": "prettier --write \"lib/**/*.ts\" \"test/**/*.ts\"",
"prepack": "npm run compile"
Expand All @@ -56,7 +56,7 @@
"devDependencies": {
"@types/mocha": "^9.0.0",
"expect.js": "0.3.1",
"mocha": "^3.5.3",
"mocha": "^10.0.0",
"nyc": "^15.1.0",
"prettier": "^2.3.2",
"rimraf": "^3.0.2",
Expand Down
181 changes: 181 additions & 0 deletions test/close.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
import { createServer } from "http";
import { io as ioc } from "socket.io-client";
import { join } from "path";
import { exec } from "child_process";
import { Server } from "..";
import expect from "expect.js";
import { createClient, getPort } from "./support/util";
import request from "supertest";

// TODO: update superagent as latest release now supports promises
const eioHandshake = (httpServer): Promise<string> => {
return new Promise((resolve) => {
request(httpServer)
.get("/socket.io/")
.query({ transport: "polling", EIO: 4 })
.end((err, res) => {
const sid = JSON.parse(res.text.substring(1)).sid;
resolve(sid);
});
});
};

const eioPush = (httpServer, sid: string, body: string): Promise<void> => {
return new Promise((resolve) => {
request(httpServer)
.post("/socket.io/")
.send(body)
.query({ transport: "polling", EIO: 4, sid })
.expect(200)
.end(() => {
resolve();
});
});
};

const eioPoll = (httpServer, sid): Promise<string> => {
return new Promise((resolve) => {
request(httpServer)
.get("/socket.io/")
.query({ transport: "polling", EIO: 4, sid })
.expect(200)
.end((err, res) => {
resolve(res.text);
});
});
};

describe("close", () => {
it("should be able to close sio sending a srv", (done) => {
const httpServer = createServer().listen(0);
const io = new Server(httpServer);
const port = getPort(io);
const net = require("net");
const server = net.createServer();

const clientSocket = createClient(io, "/", { reconnection: false });

clientSocket.on("disconnect", () => {
expect(io.sockets.sockets.size).to.equal(0);
server.listen(port);
});

clientSocket.on("connect", () => {
expect(io.sockets.sockets.size).to.equal(1);
io.close();
});

server.once("listening", () => {
// PORT should be free
server.close((error) => {
expect(error).to.be(undefined);
done();
});
});
});

it("should be able to close sio sending a srv", (done) => {
const io = new Server(0);
const port = getPort(io);
const net = require("net");
const server = net.createServer();

const clientSocket = ioc("ws://0.0.0.0:" + port, {
reconnection: false,
});

clientSocket.on("disconnect", () => {
expect(io.sockets.sockets.size).to.equal(0);
server.listen(port);
});

clientSocket.on("connect", () => {
expect(io.sockets.sockets.size).to.equal(1);
io.close();
});

server.once("listening", () => {
// PORT should be free
server.close((error) => {
expect(error).to.be(undefined);
done();
});
});
});

describe("graceful close", () => {
function fixture(filename) {
return (
'"' +
process.execPath +
'" "' +
join(__dirname, "fixtures", filename) +
'"'
);
}

it("should stop socket and timers", (done) => {
exec(fixture("server-close.ts"), done);
});
});

describe("protocol violations", () => {
it("should close the connection when receiving several CONNECT packets", async () => {
const httpServer = createServer();
const io = new Server(httpServer);

httpServer.listen(0);

const sid = await eioHandshake(httpServer);
// send a first CONNECT packet
await eioPush(httpServer, sid, "40");
// send another CONNECT packet
await eioPush(httpServer, sid, "40");
// session is cleanly closed (not discarded, see 'client.close()')
// first, we receive the Socket.IO handshake response
await eioPoll(httpServer, sid);
// then a close packet
const body = await eioPoll(httpServer, sid);
expect(body).to.be("6\u001e1");

io.close();
});

it("should close the connection when receiving an EVENT packet while not connected", async () => {
const httpServer = createServer();
const io = new Server(httpServer);

httpServer.listen(0);

const sid = await eioHandshake(httpServer);
// send an EVENT packet
await eioPush(httpServer, sid, '42["some event"]');
// session is cleanly closed, we receive a close packet
const body = await eioPoll(httpServer, sid);
expect(body).to.be("6\u001e1");

io.close();
});

it("should close the connection when receiving an invalid packet", async () => {
const httpServer = createServer();
const io = new Server(httpServer);

httpServer.listen(0);

const sid = await eioHandshake(httpServer);
// send a CONNECT packet
await eioPush(httpServer, sid, "40");
// send an invalid packet
await eioPush(httpServer, sid, "4abc");
// session is cleanly closed (not discarded, see 'client.close()')
// first, we receive the Socket.IO handshake response
await eioPoll(httpServer, sid);
// then a close packet
const body = await eioPoll(httpServer, sid);
expect(body).to.be("6\u001e1");

io.close();
});
});
});
87 changes: 87 additions & 0 deletions test/handshake.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
import { Server } from "..";
import expect from "expect.js";
import { getPort, success } from "./support/util";

describe("handshake", () => {
const request = require("superagent");

it("should send the Access-Control-Allow-xxx headers on OPTIONS request", (done) => {
const io = new Server(0, {
cors: {
origin: "http://localhost:54023",
methods: ["GET", "POST"],
allowedHeaders: ["content-type"],
credentials: true,
},
});
request
.options(`http://localhost:${getPort(io)}/socket.io/default/`)
.query({ transport: "polling", EIO: 4 })
.set("Origin", "http://localhost:54023")
.end((err, res) => {
expect(res.status).to.be(204);

expect(res.headers["access-control-allow-origin"]).to.be(
"http://localhost:54023"
);
expect(res.headers["access-control-allow-methods"]).to.be("GET,POST");
expect(res.headers["access-control-allow-headers"]).to.be(
"content-type"
);
expect(res.headers["access-control-allow-credentials"]).to.be("true");
success(done, io);
});
});

it("should send the Access-Control-Allow-xxx headers on GET request", (done) => {
const io = new Server(0, {
cors: {
origin: "http://localhost:54024",
methods: ["GET", "POST"],
allowedHeaders: ["content-type"],
credentials: true,
},
});
request
.get(`http://localhost:${getPort(io)}/socket.io/default/`)
.query({ transport: "polling", EIO: 4 })
.set("Origin", "http://localhost:54024")
.end((err, res) => {
expect(res.status).to.be(200);

expect(res.headers["access-control-allow-origin"]).to.be(
"http://localhost:54024"
);
expect(res.headers["access-control-allow-credentials"]).to.be("true");
success(done, io);
});
});

it("should allow request if custom function in opts.allowRequest returns true", (done) => {
const io = new Server(0, {
allowRequest: (req, callback) => callback(null, true),
});

request
.get(`http://localhost:${getPort(io)}/socket.io/default/`)
.query({ transport: "polling", EIO: 4 })
.end((err, res) => {
expect(res.status).to.be(200);
success(done, io);
});
});

it("should disallow request if custom function in opts.allowRequest returns false", (done) => {
const io = new Server(0, {
allowRequest: (req, callback) => callback(null, false),
});
request
.get(`http://localhost:${getPort(io)}/socket.io/default/`)
.set("origin", "http://foo.example")
.query({ transport: "polling", EIO: 4 })
.end((err, res) => {
expect(res.status).to.be(403);
success(done, io);
});
});
});
23 changes: 23 additions & 0 deletions test/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
"use strict";

import expect from "expect.js";

describe("socket.io", () => {
it("should be the same version as client", () => {
const version = require("../package").version;
expect(version).to.be(require("socket.io-client/package.json").version);
});

require("./server-attachment");
require("./handshake");
require("./close");
require("./namespaces");
require("./socket");
require("./messaging-many");
require("./middleware");
require("./socket-middleware");
require("./v2-compatibility");
require("./socket-timeout");
require("./uws");
require("./utility-methods");
});

0 comments on commit 713a6b4

Please sign in to comment.