/
socket.ts
284 lines (233 loc) · 7.39 KB
/
socket.ts
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
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
import * as expect from "expect.js";
import { io } from "..";
describe("socket", function () {
this.timeout(70000);
it("should have an accessible socket id equal to the server-side socket id (default namespace)", (done) => {
const socket = io({ forceNew: true });
socket.emit("getId", (id) => {
expect(socket.id).to.be.ok();
expect(socket.id).to.be.eql(id);
expect(socket.id).to.not.eql(socket.io.engine.id);
socket.disconnect();
done();
});
});
it("should have an accessible socket id equal to the server-side socket id (custom namespace)", (done) => {
const socket = io("/foo", { forceNew: true });
socket.emit("getId", (id) => {
expect(socket.id).to.be.ok();
expect(socket.id).to.be.eql(id);
expect(socket.id).to.not.eql(socket.io.engine.id);
socket.disconnect();
done();
});
});
it("clears socket.id upon disconnection", (done) => {
const socket = io({ forceNew: true });
socket.on("connect", () => {
socket.on("disconnect", () => {
expect(socket.id).to.not.be.ok();
done();
});
socket.disconnect();
});
});
it("doesn't fire a connect_error if we force disconnect in opening state", (done) => {
const socket = io({ forceNew: true, timeout: 100 });
socket.disconnect();
socket.io.on("connect_error", () => {
throw new Error("Unexpected");
});
setTimeout(() => {
done();
}, 300);
});
it("should change socket.id upon reconnection", (done) => {
const socket = io({ forceNew: true });
socket.on("connect", () => {
const id = socket.id;
socket.io.on("reconnect_attempt", () => {
expect(socket.id).to.not.be.ok();
});
socket.io.on("reconnect", () => {
expect(socket.id).to.not.eql(id);
socket.disconnect();
done();
});
socket.io.engine.close();
});
});
it("should enable compression by default", (done) => {
const socket = io({ forceNew: true });
socket.on("connect", () => {
socket.io.engine.once("packetCreate", (packet) => {
expect(packet.options.compress).to.be(true);
socket.disconnect();
done();
});
socket.emit("hi");
});
});
it("should disable compression", (done) => {
const socket = io({ forceNew: true });
socket.on("connect", () => {
socket.io.engine.once("packetCreate", (packet) => {
expect(packet.options.compress).to.be(false);
socket.disconnect();
done();
});
socket.compress(false).emit("hi");
});
});
describe("query option", () => {
it("should accept an object (default namespace)", (done) => {
const socket = io("/", { forceNew: true, query: { e: "f" } });
socket.emit("getHandshake", (handshake) => {
expect(handshake.query.e).to.be("f");
socket.disconnect();
done();
});
});
it("should accept a query string (default namespace)", (done) => {
const socket = io("/?c=d", { forceNew: true });
socket.emit("getHandshake", (handshake) => {
expect(handshake.query.c).to.be("d");
socket.disconnect();
done();
});
});
it("should accept an object", (done) => {
const socket = io("/abc", { forceNew: true, query: { a: "b" } });
socket.on("handshake", (handshake) => {
expect(handshake.query.a).to.be("b");
socket.disconnect();
done();
});
});
it("should accept a query string", (done) => {
const socket = io("/abc?b=c&d=e", { forceNew: true });
socket.on("handshake", (handshake) => {
expect(handshake.query.b).to.be("c");
expect(handshake.query.d).to.be("e");
socket.disconnect();
done();
});
});
it("should properly encode the parameters", (done) => {
const socket = io("/abc", { forceNew: true, query: { "&a": "&=?a" } });
socket.on("handshake", (handshake) => {
expect(handshake.query["&a"]).to.be("&=?a");
socket.disconnect();
done();
});
});
});
describe("auth option", () => {
it("should accept an object", (done) => {
const socket = io("/abc", { forceNew: true, auth: { a: "b", c: "d" } });
socket.on("handshake", (handshake) => {
expect(handshake.auth.a).to.be("b");
expect(handshake.auth.c).to.be("d");
expect(handshake.query.a).to.be(undefined);
socket.disconnect();
done();
});
});
it("should accept an function", (done) => {
const socket = io("/abc", {
forceNew: true,
auth: (cb) => cb({ e: "f" }),
});
socket.on("handshake", (handshake) => {
expect(handshake.auth.e).to.be("f");
expect(handshake.query.e).to.be(undefined);
socket.disconnect();
done();
});
});
});
it("should fire an error event on middleware failure from custom namespace", (done) => {
const socket = io("/no", { forceNew: true });
socket.on("connect_error", (err) => {
expect(err).to.eql("Auth failed (custom namespace)");
socket.disconnect();
done();
});
});
it("should throw on reserved event", () => {
const socket = io("/no", { forceNew: true });
expect(() => socket.emit("disconnecting", "goodbye")).to.throwException(
/"disconnecting" is a reserved event name/
);
});
describe("volatile packets", () => {
it("should discard a volatile packet when the socket is not connected", (done) => {
const socket = io({ forceNew: true, autoConnect: false });
socket.volatile.emit("getId", () => {
done(new Error("should not happen"));
});
socket.emit("getId", () => {
socket.disconnect();
done();
});
socket.connect();
});
it("should discard a volatile packet when the pipe is not ready", (done) => {
const socket = io({ forceNew: true });
socket.on("connect", () => {
socket.emit("getId", () => {
socket.disconnect();
done();
});
socket.volatile.emit("getId", () => {
done(new Error("should not happen"));
});
});
});
it("should send a volatile packet when the socket is connected and the pipe is ready", (done) => {
const socket = io({ forceNew: true });
const interval = setInterval(() => {
socket.volatile.emit("getId", () => {
clearInterval(interval);
socket.disconnect();
done();
});
}, 200);
});
});
describe("onAny", () => {
it("should call listener", (done) => {
const socket = io("/abc");
socket.onAny((event, arg1) => {
expect(event).to.be("handshake");
expect(arg1).to.be.an(Object);
done();
});
});
it("should prepend listener", (done) => {
const socket = io("/abc");
let count = 0;
socket.onAny((event, arg1) => {
expect(count).to.be(2);
done();
});
socket.prependAny(() => {
expect(count++).to.be(1);
});
socket.prependAny(() => {
expect(count++).to.be(0);
});
});
it("should remove listener", (done) => {
const socket = io("/abc");
let count = 0;
const fail = () => done(new Error("fail"));
socket.onAny(fail);
socket.offAny(fail);
expect(socket.listenersAny.length).to.be(0);
socket.onAny(() => {
done();
});
});
});
});