PageRenderTime 33ms CodeModel.GetById 2ms app.highlight 26ms RepoModel.GetById 1ms app.codeStats 1ms

/public/vendor/bower_components/socket.io-client/test/connection.js

https://github.com/kirillKey/questSearchTrain
JavaScript | 347 lines | 298 code | 45 blank | 4 comment | 10 complexity | 6a3cc54b6f5963f21812c3314510cea3 MD5 | raw file
  1var expect = require('expect.js');
  2var io = require('../');
  3var hasCORS = require('has-cors');
  4var textBlobBuilder = require('text-blob-builder');
  5
  6describe('connection', function() {
  7  this.timeout(20000);
  8
  9  it('should connect to localhost', function(done) {
 10    var socket = io({ forceNew: true });
 11    socket.emit('hi');
 12    socket.on('hi', function(data){
 13      socket.disconnect();
 14      done();
 15    });
 16  });
 17
 18  it('should work with acks', function(done){
 19    var socket = io({ forceNew: true });
 20    socket.emit('ack');
 21    socket.on('ack', function(fn){
 22      fn(5, { test: true });
 23    });
 24    socket.on('got it', function(){
 25      socket.disconnect();
 26      done();
 27    });
 28  });
 29
 30  it('should receive date with ack', function(done){
 31    var socket = io({ forceNew: true });
 32    socket.emit('getAckDate', { test: true }, function(data){
 33       expect(data).to.be.a('string');
 34       socket.disconnect();
 35       done();
 36    });
 37  });
 38
 39  it('should work with false', function(done){
 40    var socket = io({ forceNew: true });
 41    socket.emit('false');
 42    socket.on('false', function(f){
 43      expect(f).to.be(false);
 44      socket.disconnect();
 45      done();
 46    });
 47  });
 48
 49  it('should receive utf8 multibyte characters', function(done) {
 50    var correct = [
 51      'てすと',
 52      'Я Б Г Д Ж Й',
 53      'Ä ä Ü ü ß',
 54      'utf8 — string',
 55      'utf8 — string'
 56    ];
 57
 58    var socket = io({ forceNew: true });
 59    var i = 0;
 60    socket.on('takeUtf8', function(data) {
 61      expect(data).to.be(correct[i]);
 62      i++;
 63      if (i == correct.length) {
 64        socket.disconnect();
 65        done();
 66      }
 67    });
 68    socket.emit('getUtf8');
 69  });
 70
 71  it('should connect to a namespace after connection established', function(done) {
 72    var manager = io.Manager();
 73    var socket = manager.socket('/');
 74    socket.on('connect', function(){
 75      var foo = manager.socket('/foo');
 76      foo.on('connect', function(){
 77        foo.close();
 78        socket.close();
 79        done();
 80      });
 81    });
 82  });
 83
 84  it('should reconnect by default', function(done){
 85    var socket = io({ forceNew: true });
 86    socket.io.on('reconnect', function() {
 87      socket.disconnect();
 88      done();
 89    });
 90
 91    setTimeout(function() {
 92      socket.io.engine.close();
 93    }, 500);
 94  });
 95
 96  it('reconnect event should fire in socket', function(done){
 97    var socket = io({ forceNew: true });
 98
 99    socket.on('reconnect', function() {
100      socket.disconnect();
101      done();
102    });
103
104    setTimeout(function() {
105      socket.io.engine.close();
106    }, 500);
107  });
108
109  it('should try to reconnect twice and fail when requested two attempts with immediate timeout and reconnect enabled', function(done) {
110    var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
111    var socket;
112
113    var reconnects = 0;
114    var reconnectCb = function() {
115      reconnects++;
116    };
117
118    manager.on('reconnect_attempt', reconnectCb);
119    manager.on('reconnect_failed', function failed() {
120      expect(reconnects).to.be(2);
121      socket.close();
122      done();
123    });
124
125    socket = manager.socket('/timeout');
126  });
127
128  it('should fire reconnect_* events on socket', function(done) {
129    var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
130    var socket = manager.socket('/timeout_socket');
131
132    var reconnects = 0;
133    var reconnectCb = function(attempts) {
134      reconnects++;
135      expect(attempts).to.be(reconnects);
136    };
137
138    socket.on('reconnect_attempt', reconnectCb);
139    socket.on('reconnect_failed', function failed() {
140      expect(reconnects).to.be(2);
141      socket.close();
142      done();
143    });
144  });
145
146  it('should fire error on socket', function(done) {
147    var manager = io.Manager({ reconnection: true });
148    var socket = manager.socket('/timeout_socket');
149
150    socket.on('error', function(data) {
151      expect(data.code).to.be('test');
152      socket.close();
153      done();
154    });
155
156    socket.on('connect', function() {
157      manager.engine.onPacket({ type: 'error', data: 'test' });
158    });
159  });
160
161  it('should fire reconnecting (on socket) with attempts number when reconnecting twice', function(done) {
162    var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
163    var socket = manager.socket('/timeout_socket');
164
165    var reconnects = 0;
166    var reconnectCb = function(attempts) {
167      reconnects++;
168      expect(attempts).to.be(reconnects);
169    };
170
171    socket.on('reconnecting', reconnectCb);
172    socket.on('reconnect_failed', function failed() {
173      expect(reconnects).to.be(2);
174      socket.close();
175      done();
176    });
177  });
178
179  it('should not try to reconnect and should form a connection when connecting to correct port with default timeout', function(done) {
180    var manager = io.Manager({ reconnection: true, reconnectionDelay: 10 });
181    var cb = function() {
182      socket.close();
183      expect().fail();
184    };
185    manager.on('reconnect_attempt', cb);
186
187    var socket = manager.socket('/valid');
188    socket.on('connect', function(){
189      // set a timeout to let reconnection possibly fire
190      setTimeout(function() {
191        socket.close();
192        done();
193      }, 1000);
194    });
195  });
196
197  // Ignore incorrect connection test for old IE due to no support for
198  // `script.onerror` (see: http://requirejs.org/docs/api.html#ieloadfail)
199  if (!global.document || hasCORS) {
200    it('should try to reconnect twice and fail when requested two attempts with incorrect address and reconnect enabled', function(done) {
201      var manager = io.Manager('http://localhost:3940', { reconnection: true, reconnectionAttempts: 2, reconnectionDelay: 10 });
202      var socket = manager.socket('/asd');
203      var reconnects = 0;
204      var cb = function() {
205        reconnects++;
206      };
207
208      manager.on('reconnect_attempt', cb);
209
210      manager.on('reconnect_failed', function() {
211        expect(reconnects).to.be(2);
212        socket.disconnect();
213        done();
214      });
215    });
216
217    it('should not try to reconnect with incorrect port when reconnection disabled', function(done) {
218      var manager = io.Manager('http://localhost:9823', { reconnection: false });
219      var cb = function() {
220        socket.close();
221        expect().fail();
222      };
223      manager.on('reconnect_attempt', cb);
224
225      manager.on('connect_error', function(){
226        // set a timeout to let reconnection possibly fire
227        setTimeout(function() {
228          socket.disconnect();
229          done();
230        }, 1000);
231      });
232
233      var socket = manager.socket('/invalid');
234    });
235  }
236
237  it('should emit date as string', function(done){
238    var socket = io({ forceNew: true });
239    socket.on('takeDate', function(data) {
240      expect(data).to.be.a('string');
241      done();
242    });
243    socket.emit('getDate');
244  });
245
246  it('should emit date in object', function(done){
247    var socket = io({ forceNew: true });
248    socket.on('takeDateObj', function(data) {
249      expect(data).to.be.an('object');
250      expect(data.date).to.be.a('string');
251      done();
252    });
253    socket.emit('getDateObj');
254  });
255
256  if (!global.Blob && !global.ArrayBuffer) {
257    it('should get base64 data as a last resort', function(done) {
258      var socket = io({ forceNew: true });
259      socket.on('takebin', function(a) {
260        socket.disconnect();
261        expect(a.base64).to.be(true);
262        expect(a.data).to.eql('YXNkZmFzZGY=');
263        done();
264      });
265      socket.emit('getbin');
266    });
267  }
268
269  if (global.ArrayBuffer) {
270    var base64 = require('base64-arraybuffer');
271
272    it('should get binary data (as an ArrayBuffer)', function(done){
273      var socket = io({ forceNew: true });
274      socket.emit('doge');
275      socket.on('doge', function(buffer){
276        expect(buffer instanceof ArrayBuffer).to.be(true);
277        socket.disconnect();
278        done();
279      });
280    });
281
282    it('should send binary data (as an ArrayBuffer)', function(done){
283      var socket = io({ forceNew: true });
284      socket.on('buffack', function(){
285        socket.disconnect();
286        done();
287      });
288      var buf = base64.decode("asdfasdf");
289      socket.emit('buffa', buf);
290    });
291
292    it('should send binary data (as an ArrayBuffer) mixed with json', function(done) {
293      var socket = io({ forceNew: true });
294      socket.on('jsonbuff-ack', function() {
295        socket.disconnect();
296        done();
297      });
298      var buf = base64.decode("howdy");
299      socket.emit('jsonbuff', {hello: 'lol', message: buf, goodbye: 'gotcha'});
300    });
301
302    it('should send events with ArrayBuffers in the correct order', function(done) {
303      var socket = io({ forceNew: true });
304      socket.on('abuff2-ack', function() {
305        socket.disconnect();
306        done();
307      });
308      var buf = base64.decode("abuff1");
309      socket.emit('abuff1', buf);
310      socket.emit('abuff2', 'please arrive second');
311    });
312  }
313
314  if (global.Blob && null != textBlobBuilder('xxx')) {
315    it('should send binary data (as a Blob)', function(done){
316      var socket = io({ forceNew: true });
317      socket.on('back', function(){
318        socket.disconnect();
319        done();
320      });
321      var blob = textBlobBuilder('hello world');
322      socket.emit('blob', blob);
323    });
324
325    it('should send binary data (as a Blob) mixed with json', function(done) {
326      var socket = io({ forceNew: true });
327      socket.on('jsonblob-ack', function() {
328        socket.disconnect();
329        done();
330      });
331      var blob = textBlobBuilder('EEEEEEEEE');
332      socket.emit('jsonblob', {hello: 'lol', message: blob, goodbye: 'gotcha'});
333    });
334
335    it('should send events with Blobs in the correct order', function(done) {
336      var socket = io({ forceNew: true });
337      socket.on('blob3-ack', function() {
338        socket.disconnect();
339        done();
340      });
341      var blob = textBlobBuilder('BLOBBLOB');
342      socket.emit('blob1', blob);
343      socket.emit('blob2', 'second');
344      socket.emit('blob3', blob);
345    });
346  }
347});