Skip to content

Commit

Permalink
test: remove hardcoded ports
Browse files Browse the repository at this point in the history
Related: #3447
  • Loading branch information
darrachequesne committed Sep 9, 2021
1 parent dc81fcf commit 7a74b66
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 41 deletions.
4 changes: 3 additions & 1 deletion lib/index.ts
Expand Up @@ -254,7 +254,9 @@ export class Server<
this.adapter(opts.adapter || Adapter);
this.sockets = this.of("/");
this.opts = opts;
if (srv) this.attach(srv as http.Server);
if (srv instanceof http.Server || typeof srv === "number") {
this.attach(srv);
}
}

/**
Expand Down
74 changes: 34 additions & 40 deletions test/socket.io.ts
Expand Up @@ -39,6 +39,11 @@ const waitFor = (emitter, event) => {
});
};

const getPort = (io: Server): number => {
// @ts-ignore
return io.httpServer.address().port;
};

describe("socket.io", () => {
it("should be the same version as client", () => {
const version = require("../package").version;
Expand Down Expand Up @@ -189,29 +194,17 @@ describe("socket.io", () => {

describe("port", () => {
it("should be bound", (done) => {
const sockets = new Server(54010);
request("http://localhost:54010")
.get("/socket.io/socket.io.js")
.expect(200, done);
});
const io = new Server(0);

it("should be bound as a string", (done) => {
const sockets = new Server(54020);
request("http://localhost:54020")
request(`http://localhost:${getPort(io)}`)
.get("/socket.io/socket.io.js")
.expect(200, done);
});

it("with listen", (done) => {
const sockets = new Server().listen(54011);
request("http://localhost:54011")
.get("/socket.io/socket.io.js")
.expect(200, done);
});
const io = new Server().listen(0);

it("as a string", (done) => {
const sockets = new Server().listen(54012);
request("http://localhost:54012")
request(`http://localhost:${getPort(io)}`)
.get("/socket.io/socket.io.js")
.expect(200, done);
});
Expand All @@ -222,7 +215,7 @@ describe("socket.io", () => {
const request = require("superagent");

it("should send the Access-Control-Allow-xxx headers on OPTIONS request", (done) => {
const sockets = new Server(54013, {
const io = new Server(0, {
cors: {
origin: "http://localhost:54023",
methods: ["GET", "POST"],
Expand All @@ -231,7 +224,7 @@ describe("socket.io", () => {
},
});
request
.options("http://localhost:54013/socket.io/default/")
.options(`http://localhost:${getPort(io)}/socket.io/default/`)
.query({ transport: "polling", EIO: 4 })
.set("Origin", "http://localhost:54023")
.end((err, res) => {
Expand All @@ -250,7 +243,7 @@ describe("socket.io", () => {
});

it("should send the Access-Control-Allow-xxx headers on GET request", (done) => {
const sockets = new Server(54014, {
const io = new Server(0, {
cors: {
origin: "http://localhost:54024",
methods: ["GET", "POST"],
Expand All @@ -259,7 +252,7 @@ describe("socket.io", () => {
},
});
request
.get("http://localhost:54014/socket.io/default/")
.get(`http://localhost:${getPort(io)}/socket.io/default/`)
.query({ transport: "polling", EIO: 4 })
.set("Origin", "http://localhost:54024")
.end((err, res) => {
Expand All @@ -274,12 +267,12 @@ describe("socket.io", () => {
});

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

request
.get("http://localhost:54022/socket.io/default/")
.get(`http://localhost:${getPort(io)}/socket.io/default/`)
.query({ transport: "polling", EIO: 4 })
.end((err, res) => {
expect(res.status).to.be(200);
Expand All @@ -288,11 +281,11 @@ describe("socket.io", () => {
});

it("should disallow request if custom function in opts.allowRequest returns false", (done) => {
const sockets = new Server(createServer().listen(54023), {
const io = new Server(0, {
allowRequest: (req, callback) => callback(null, false),
});
request
.get("http://localhost:54023/socket.io/default/")
.get(`http://localhost:${getPort(io)}/socket.io/default/`)
.set("origin", "http://foo.example")
.query({ transport: "polling", EIO: 4 })
.end((err, res) => {
Expand All @@ -304,22 +297,22 @@ describe("socket.io", () => {

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

const clientSocket = client(srv, { reconnection: false });
const clientSocket = client(httpServer, { reconnection: false });

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

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

server.once("listening", () => {
Expand All @@ -331,30 +324,31 @@ describe("socket.io", () => {
});
});

it("should be able to close sio sending a port", () => {
const PORT = 54019;
const sio = new Server(PORT);
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, {
const clientSocket = ioc("ws://0.0.0.0:" + port, {
reconnection: false,
});

clientSocket.on("disconnect", () => {
expect(Object.keys(sio._nsps["/"].sockets).length).to.equal(0);
server.listen(PORT);
expect(io.sockets.sockets.size).to.equal(0);
server.listen(port);
});

clientSocket.on("connect", () => {
expect(Object.keys(sio._nsps["/"].sockets).length).to.equal(1);
sio.close();
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();
});
});
});
Expand Down

0 comments on commit 7a74b66

Please sign in to comment.