|
| 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 | +}); |
0 commit comments