aboutsummaryrefslogtreecommitdiff
path: root/test/js/third_party/socket.io/socket.io-close.test.ts
blob: feac0fbad1c97b0dff0ca532267da23770e067bb (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
import { describe, it, expect } from "bun:test";
import { io as ioc } from "socket.io-client";
import { join } from "path";
import { createServer } from "http";
import { createClient, getPort, success, fail, eioHandshake, eioPoll, eioPush } from "./support/util.ts";
import { Server } from "socket.io";
import { exec, ChildProcess } from "child_process";

// Hanging tests are disabled because they cause the test suite to hang
describe("close", () => {
  it.skip("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 });
    let timeout = setTimeout(() => {
      fail(done, io, new Error("timeout"), clientSocket);
    }, 200);

    clientSocket.on("disconnect", () => {
      try {
        expect(io.sockets.sockets.size).toBe(0);
      } catch (err) {
        fail(done, io, err, clientSocket);
      }
      server.listen(port);
    });

    clientSocket.on("connect", () => {
      try {
        expect(io.sockets.sockets.size).toBe(1);
        io.close();
      } catch (err) {
        fail(done, io, err, clientSocket);
      }
    });

    server.once("listening", () => {
      // PORT should be free
      server.close((error: any) => {
        clearTimeout(timeout);
        try {
          expect(error).toBe(undefined);
          success(done, io, clientSocket);
        } catch (err) {
          fail(done, io, err, clientSocket);
        }
      });
    });
  });

  it.skip("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,
    });
    let timeout = setTimeout(() => {
      fail(done, io, new Error("timeout"), clientSocket);
    }, 200);

    clientSocket.on("disconnect", () => {
      try {
        expect(io.sockets.sockets.size).toBe(0);
      } catch (err) {
        fail(done, io, err, clientSocket);
      }
      server.listen(port);
    });

    clientSocket.on("connect", () => {
      try {
        expect(io.sockets.sockets.size).toBe(1);
        io.close();
      } catch (err) {
        fail(done, io, err, clientSocket);
      }
    });

    server.once("listening", () => {
      // PORT should be free
      server.close((error: any) => {
        clearTimeout(timeout);
        try {
          expect(error).toBe(undefined);
          success(done, io, clientSocket);
        } catch (err) {
          fail(done, io, err, clientSocket);
        }
      });
    });
  });

  describe("graceful close", () => {
    function fixture(filename: string) {
      return '"' + process.execPath + '" "' + join(__dirname, "fixtures", filename) + '"';
    }
    // TODO failing on macOS
    it.skip("should stop socket and timers", done => {
      let process: ChildProcess;
      const timeout = setTimeout(() => {
        process?.kill();
        done(new Error("timeout"));
      }, 3000);

      process = exec(fixture("server-close.ts"), err => {
        clearTimeout(timeout);
        done(err);
      });
    });
  });

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

      httpServer.listen(0);

      let timeout = setTimeout(() => {
        fail(done, io, new Error("timeout"));
      }, 1500);

      (async () => {
        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
        return await eioPoll(httpServer, sid);
      })().then(body => {
        clearTimeout(timeout);
        try {
          expect(body).toBe("6\u001e1");

          io.close();
          success(done, io);
        } catch (err) {
          fail(done, io, err);
        }
      });
    });

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

      httpServer.listen(0);
      let timeout = setTimeout(() => {
        fail(done, io, new Error("timeout"));
      }, 1500);

      (async () => {
        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
        return await eioPoll(httpServer, sid);
      })().then(body => {
        clearTimeout(timeout);
        try {
          expect(body).toBe("6\u001e1");

          io.close();
          success(done, io);
        } catch (err) {
          fail(done, io, err);
        }
      });
    });

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

      httpServer.listen(0);
      let timeout = setTimeout(() => {
        fail(done, io, new Error("timeout"));
      }, 1500);

      (async () => {
        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
        return await eioPoll(httpServer, sid);
      })().then(body => {
        clearTimeout(timeout);
        try {
          expect(body).toBe("6\u001e1");

          io.close();
          success(done, io);
        } catch (err) {
          fail(done, io, err);
        }
      });
    });
  });
});