connection.js 14.4 KB
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 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506
var expect = require('expect.js');
var io = require('../');
var hasCORS = require('has-cors');
var textBlobBuilder = require('text-blob-builder');

describe('connection', function() {
  this.timeout(70000);

  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();
    socket.disconnect();
  });

  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();
        manager.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();
        manager.close();
        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('should reconnect manually', function(done) {
    var socket = io({ forceNew: true });
    socket.once('connect', function() {
      socket.disconnect();
    }).once('disconnect', function() {
      socket.once('connect', function() {
        socket.disconnect();
        done();
      });
      socket.connect();
    });
  });

  it('should reconnect automatically after reconnecting manually', function(done){
    var socket = io({ forceNew: true });
    socket.once('connect', function() {
      socket.disconnect();
    }).once('disconnect', function() {
      socket.on('reconnect', function() {
        socket.disconnect();
        done();
      });
      socket.connect();
      setTimeout(function() {
        socket.io.engine.close();
      }, 500);
    });
  });

  it('should attempt reconnects after a failed reconnect', function(done){
    var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
    var socket = manager.socket('/timeout');
    socket.once('reconnect_failed', function() {
      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();
        manager.close();
        done();
      });
      socket.connect();
    });
  });

  it('reconnect delay should increase every time', function(done){
    var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 5, reconnectionDelay: 10, randomizationFactor: 0.2 });
    var socket = manager.socket('/timeout');
    var reconnects = 0, increasingDelay = true, startTime, prevDelay = 0;
    
    socket.on('connect_error', function() {
      startTime = new Date().getTime();
    });
    socket.on('reconnect_attempt', function() {
      reconnects++;
      var currentTime = new Date().getTime();
      var delay = currentTime - startTime;
      if (delay <= prevDelay) {
        increasingDelay = false;
      }
      prevDelay = delay;
    });

    socket.on('reconnect_failed', function failed() {
      expect(reconnects).to.be(5);
      expect(increasingDelay).to.be.ok();
      socket.close();
      manager.close();
      done();
    });
  });

  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 not reconnect when force closed', function(done){
    var socket = io('/invalid', { forceNew: true, timeout: 0, reconnectionDelay: 10 });
    socket.on('connect_error', function() {
      socket.on('reconnect_attempt', function() {
        expect().fail();
      });
      socket.disconnect();
      // set a timeout to let reconnection possibly fire
      setTimeout(function() {
        done();
      }, 500);
    });
  });

  it('should stop reconnecting when force closed', function(done){
    var socket = io('/invalid', { forceNew: true, timeout: 0, reconnectionDelay: 10 });
    socket.once('reconnect_attempt', function() {
      socket.on('reconnect_attempt', function() {
        expect().fail();
      });
      socket.disconnect();
      // set a timeout to let reconnection possibly fire
      setTimeout(function() {
        done();
      }, 500);
    });
  });

  it('should stop reconnecting on a socket and keep to reconnect on another', function(done){
    var manager = io.Manager();
    var socket1 = manager.socket('/');
    var socket2 = manager.socket('/asd');

    manager.on('reconnect_attempt', function() {
      socket1.on('connect', function() {
        expect().fail();
      });
      socket2.on('connect', function() {
        setTimeout(function() {
          socket2.disconnect();
          manager.disconnect();
          done();
        }, 500);
      });
      socket1.disconnect();
    });

    setTimeout(function() {
      manager.engine.close();
    }, 1000);
  });

  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();
      manager.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();
      manager.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();
      manager.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();
      manager.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();
        manager.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();
        manager.close();
        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();
          manager.close();
          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) {
      socket.close();
      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) {
      socket.close();
      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);
    });
  }
});