/
connection.js
366 lines (319 loc) · 10.4 KB
/
connection.js
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
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
var expect = require('expect.js');
var io = require('../');
var hasCORS = require('has-cors');
var textBlobBuilder = require('text-blob-builder');
describe('connection', function() {
this.timeout(20000);
it('should connect to localhost', function(done) {
var socket = io({ forceNew: true });
socket.emit('hi');
socket.on('hi', function(data){
socket.disconnect();
done();
});
});
it('should not connect when autoConnect option set to false', function() {
var socket = io({ forceNew: true, autoConnect: false });
expect(socket.io.engine).to.not.be.ok();
});
it('should work with acks', function(done){
var socket = io({ forceNew: true });
socket.emit('ack');
socket.on('ack', function(fn){
fn(5, { test: true });
});
socket.on('got it', function(){
socket.disconnect();
done();
});
});
it('should receive date with ack', function(done){
var socket = io({ forceNew: true });
socket.emit('getAckDate', { test: true }, function(data){
expect(data).to.be.a('string');
socket.disconnect();
done();
});
});
it('should work with false', function(done){
var socket = io({ forceNew: true });
socket.emit('false');
socket.on('false', function(f){
expect(f).to.be(false);
socket.disconnect();
done();
});
});
it('should receive utf8 multibyte characters', function(done) {
var correct = [
'てすと',
'Я Б Г Д Ж Й',
'Ä ä Ü ü ß',
'utf8 — string',
'utf8 — string'
];
var socket = io({ forceNew: true });
var i = 0;
socket.on('takeUtf8', function(data) {
expect(data).to.be(correct[i]);
i++;
if (i == correct.length) {
socket.disconnect();
done();
}
});
socket.emit('getUtf8');
});
it('should connect to a namespace after connection established', function(done) {
var manager = io.Manager();
var socket = manager.socket('/');
socket.on('connect', function(){
var foo = manager.socket('/foo');
foo.on('connect', function(){
foo.close();
socket.close();
done();
});
});
});
it('should open a new namespace after connection gets closed', function(done){
var manager = io.Manager();
var socket = manager.socket('/');
socket.on('connect', function() {
socket.disconnect();
}).on('disconnect', function() {
var foo = manager.socket('/foo');
foo.on('connect', function() {
foo.disconnect();
done();
});
});
});
it('should reconnect by default', function(done){
var socket = io({ forceNew: true });
socket.io.on('reconnect', function() {
socket.disconnect();
done();
});
setTimeout(function() {
socket.io.engine.close();
}, 500);
});
it('reconnect event should fire in socket', function(done){
var socket = io({ forceNew: true });
socket.on('reconnect', function() {
socket.disconnect();
done();
});
setTimeout(function() {
socket.io.engine.close();
}, 500);
});
it('should try to reconnect twice and fail when requested two attempts with immediate timeout and reconnect enabled', function(done) {
var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
var socket;
var reconnects = 0;
var reconnectCb = function() {
reconnects++;
};
manager.on('reconnect_attempt', reconnectCb);
manager.on('reconnect_failed', function failed() {
expect(reconnects).to.be(2);
socket.close();
done();
});
socket = manager.socket('/timeout');
});
it('should fire reconnect_* events on socket', function(done) {
var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
var socket = manager.socket('/timeout_socket');
var reconnects = 0;
var reconnectCb = function(attempts) {
reconnects++;
expect(attempts).to.be(reconnects);
};
socket.on('reconnect_attempt', reconnectCb);
socket.on('reconnect_failed', function failed() {
expect(reconnects).to.be(2);
socket.close();
done();
});
});
it('should fire error on socket', function(done) {
var manager = io.Manager({ reconnection: true });
var socket = manager.socket('/timeout_socket');
socket.on('error', function(data) {
expect(data.code).to.be('test');
socket.close();
done();
});
socket.on('connect', function() {
manager.engine.onPacket({ type: 'error', data: 'test' });
});
});
it('should fire reconnecting (on socket) with attempts number when reconnecting twice', function(done) {
var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
var socket = manager.socket('/timeout_socket');
var reconnects = 0;
var reconnectCb = function(attempts) {
reconnects++;
expect(attempts).to.be(reconnects);
};
socket.on('reconnecting', reconnectCb);
socket.on('reconnect_failed', function failed() {
expect(reconnects).to.be(2);
socket.close();
done();
});
});
it('should not try to reconnect and should form a connection when connecting to correct port with default timeout', function(done) {
var manager = io.Manager({ reconnection: true, reconnectionDelay: 10 });
var cb = function() {
socket.close();
expect().fail();
};
manager.on('reconnect_attempt', cb);
var socket = manager.socket('/valid');
socket.on('connect', function(){
// set a timeout to let reconnection possibly fire
setTimeout(function() {
socket.close();
done();
}, 1000);
});
});
// Ignore incorrect connection test for old IE due to no support for
// `script.onerror` (see: http://requirejs.org/docs/api.html#ieloadfail)
if (!global.document || hasCORS) {
it('should try to reconnect twice and fail when requested two attempts with incorrect address and reconnect enabled', function(done) {
var manager = io.Manager('http://localhost:3940', { reconnection: true, reconnectionAttempts: 2, reconnectionDelay: 10 });
var socket = manager.socket('/asd');
var reconnects = 0;
var cb = function() {
reconnects++;
};
manager.on('reconnect_attempt', cb);
manager.on('reconnect_failed', function() {
expect(reconnects).to.be(2);
socket.disconnect();
done();
});
});
it('should not try to reconnect with incorrect port when reconnection disabled', function(done) {
var manager = io.Manager('http://localhost:9823', { reconnection: false });
var cb = function() {
socket.close();
expect().fail();
};
manager.on('reconnect_attempt', cb);
manager.on('connect_error', function(){
// set a timeout to let reconnection possibly fire
setTimeout(function() {
socket.disconnect();
done();
}, 1000);
});
var socket = manager.socket('/invalid');
});
}
it('should emit date as string', function(done){
var socket = io({ forceNew: true });
socket.on('takeDate', function(data) {
expect(data).to.be.a('string');
done();
});
socket.emit('getDate');
});
it('should emit date in object', function(done){
var socket = io({ forceNew: true });
socket.on('takeDateObj', function(data) {
expect(data).to.be.an('object');
expect(data.date).to.be.a('string');
done();
});
socket.emit('getDateObj');
});
if (!global.Blob && !global.ArrayBuffer) {
it('should get base64 data as a last resort', function(done) {
var socket = io({ forceNew: true });
socket.on('takebin', function(a) {
socket.disconnect();
expect(a.base64).to.be(true);
expect(a.data).to.eql('YXNkZmFzZGY=');
done();
});
socket.emit('getbin');
});
}
if (global.ArrayBuffer) {
var base64 = require('base64-arraybuffer');
it('should get binary data (as an ArrayBuffer)', function(done){
var socket = io({ forceNew: true });
socket.emit('doge');
socket.on('doge', function(buffer){
expect(buffer instanceof ArrayBuffer).to.be(true);
socket.disconnect();
done();
});
});
it('should send binary data (as an ArrayBuffer)', function(done){
var socket = io({ forceNew: true });
socket.on('buffack', function(){
socket.disconnect();
done();
});
var buf = base64.decode("asdfasdf");
socket.emit('buffa', buf);
});
it('should send binary data (as an ArrayBuffer) mixed with json', function(done) {
var socket = io({ forceNew: true });
socket.on('jsonbuff-ack', function() {
socket.disconnect();
done();
});
var buf = base64.decode("howdy");
socket.emit('jsonbuff', {hello: 'lol', message: buf, goodbye: 'gotcha'});
});
it('should send events with ArrayBuffers in the correct order', function(done) {
var socket = io({ forceNew: true });
socket.on('abuff2-ack', function() {
socket.disconnect();
done();
});
var buf = base64.decode("abuff1");
socket.emit('abuff1', buf);
socket.emit('abuff2', 'please arrive second');
});
}
if (global.Blob && null != textBlobBuilder('xxx')) {
it('should send binary data (as a Blob)', function(done){
var socket = io({ forceNew: true });
socket.on('back', function(){
socket.disconnect();
done();
});
var blob = textBlobBuilder('hello world');
socket.emit('blob', blob);
});
it('should send binary data (as a Blob) mixed with json', function(done) {
var socket = io({ forceNew: true });
socket.on('jsonblob-ack', function() {
socket.disconnect();
done();
});
var blob = textBlobBuilder('EEEEEEEEE');
socket.emit('jsonblob', {hello: 'lol', message: blob, goodbye: 'gotcha'});
});
it('should send events with Blobs in the correct order', function(done) {
var socket = io({ forceNew: true });
socket.on('blob3-ack', function() {
socket.disconnect();
done();
});
var blob = textBlobBuilder('BLOBBLOB');
socket.emit('blob1', blob);
socket.emit('blob2', 'second');
socket.emit('blob3', blob);
});
}
});