-
Notifications
You must be signed in to change notification settings - Fork 10.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
16 changed files
with
3,914 additions
and
3,677 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
}); | ||
}); | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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"); | ||
}); |
Oops, something went wrong.