aboutsummaryrefslogtreecommitdiff
path: root/test/js/third_party/socket.io/socket.io-handshake.test.ts
blob: e2980808043bcb4d1cfcda84e8333ccce11a9234 (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
import { Server } from "socket.io";
import { describe, it, expect } from "bun:test";
import { getPort, success, fail } from "./support/util.ts";

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,
      },
    });

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

    request
      .options(`http://localhost:${getPort(io)}/socket.io/default/`)
      .query({ transport: "polling", EIO: 4 })
      .set("Origin", "http://localhost:54023")
      .end((err, res) => {
        try {
          clearTimeout(timeout);
          expect(res.status).toBe(204);

          expect(res.headers["access-control-allow-origin"]).toBe("http://localhost:54023");
          expect(res.headers["access-control-allow-methods"]).toBe("GET,POST");
          expect(res.headers["access-control-allow-headers"]).toBe("content-type");
          expect(res.headers["access-control-allow-credentials"]).toBe("true");
          success(done, io);
        } catch (err) {
          fail(done, io, err);
        }
      });
  });

  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,
      },
    });

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

    request
      .get(`http://localhost:${getPort(io)}/socket.io/default/`)
      .query({ transport: "polling", EIO: 4 })
      .set("Origin", "http://localhost:54024")
      .end((err, res) => {
        clearTimeout(timeout);
        try {
          expect(res.status).toBe(200);

          expect(res.headers["access-control-allow-origin"]).toBe("http://localhost:54024");
          expect(res.headers["access-control-allow-credentials"]).toBe("true");
          success(done, io);
        } catch (err) {
          fail(done, io, err);
        }
      });
  });

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

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

    request
      .get(`http://localhost:${getPort(io)}/socket.io/default/`)
      .query({ transport: "polling", EIO: 4 })
      .end((err, res) => {
        try {
          clearTimeout(timeout);
          expect(res.status).toBe(200);
          success(done, io);
        } catch (err) {
          fail(done, io, err);
        }
      });
  });

  it("should disallow request if custom function in opts.allowRequest returns false", done => {
    const io = new Server(0, {
      allowRequest: (req, callback) => callback(null, false),
    });
    const timeout = setTimeout(() => {
      fail(done, io, new Error("timeout"));
    }, 200);
    request
      .get(`http://localhost:${getPort(io)}/socket.io/default/`)
      .set("origin", "http://foo.example")
      .query({ transport: "polling", EIO: 4 })
      .end((err, res) => {
        try {
          clearTimeout(timeout);
          expect(res.status).toBe(403);
          success(done, io);
        } catch (err) {
          fail(done, io, err);
        }
      });
  });
});