aboutsummaryrefslogtreecommitdiff
path: root/test/js/third_party/socket.io/socket.io-socket-middleware.test.ts
blob: 4eea394918d9b31cb325b0d06111a5adf0069a91 (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
// TODO: uncomment when Blob bug in isBinary is fixed

import { Server } from "socket.io";
import { describe, it, expect } from "bun:test";

import { success, fail, createClient } from "./support/util.ts";

describe("socket middleware", () => {
  it.skip("should call functions", done => {
    const io = new Server(0);
    const clientSocket = createClient(io, "/", { multiplex: false });
    const timeout = setTimeout(() => {
      fail(done, io, new Error("timeout"), clientSocket);
    }, 200);

    clientSocket.emit("join", "woot");

    let run = 0;

    io.on("connection", socket => {
      socket.use((event, next) => {
        try {
          expect(event).toStrictEqual(["join", "woot"]);
          event.unshift("wrap");
          run++;
          next();
        } catch (err) {
          clearTimeout(timeout);
          fail(done, io, err, clientSocket);
        }
      });
      socket.use((event, next) => {
        try {
          expect(event).toStrictEqual(["wrap", "join", "woot"]);
          run++;
          next();
        } catch (err) {
          clearTimeout(timeout);
          fail(done, io, err, clientSocket);
        }
      });
      socket.on("wrap", (data1, data2) => {
        try {
          clearTimeout(timeout);
          expect(data1).toBe("join");
          expect(data2).toBe("woot");
          expect(run).toBe(2);

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

  it.skip("should pass errors", done => {
    const io = new Server(0);
    const clientSocket = createClient(io, "/", { multiplex: false });
    const timeout = setTimeout(() => {
      fail(done, io, new Error("timeout"), clientSocket);
    }, 200);

    clientSocket.emit("join", "woot");

    io.on("connection", socket => {
      socket.use((event, next) => {
        next(new Error("Authentication error"));
      });
      socket.use((event, next) => {
        done(new Error("should not happen"));
      });
      socket.on("join", () => {
        done(new Error("should not happen"));
      });
      socket.on("error", err => {
        try {
          clearTimeout(timeout);
          expect(err).toBeInstanceOf(Error);
          expect(err.message).toBe("Authentication error");

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