PageRenderTime 103ms CodeModel.GetById 3ms app.highlight 89ms RepoModel.GetById 1ms app.codeStats 1ms

/test/socket.io.js

http://github.com/LearnBoost/socket.io
JavaScript | 2459 lines | 2190 code | 253 blank | 16 comment | 88 complexity | d622652cc492d58ba1bca79209012617 MD5 | raw file

Large files files are truncated, but you can click here to view the full file

   1'use strict';
   2
   3var http = require('http').Server;
   4var io = require('../lib');
   5var fs = require('fs');
   6var join = require('path').join;
   7var exec = require('child_process').exec;
   8var ioc = require('socket.io-client');
   9var request = require('supertest');
  10var expect = require('expect.js');
  11
  12// Creates a socket.io client for the given server
  13function client(srv, nsp, opts){
  14  if ('object' == typeof nsp) {
  15    opts = nsp;
  16    nsp = null;
  17  }
  18  var addr = srv.address();
  19  if (!addr) addr = srv.listen().address();
  20  var url = 'ws://localhost:' + addr.port + (nsp || '');
  21  return ioc(url, opts);
  22}
  23
  24describe('socket.io', function(){
  25
  26  it.skip('should be the same version as client', function(){
  27    var version = require('../package').version;
  28    expect(version).to.be(require('socket.io-client/package').version);
  29  });
  30
  31  describe('set', function() {
  32    it('should be able to set ping timeout to engine.io', function() {
  33      var srv = io(http());
  34      srv.set('heartbeat timeout', 10);
  35      expect(srv.eio.pingTimeout).to.be(10);
  36    });
  37
  38    it('should be able to set ping interval to engine.io', function() {
  39      var srv = io(http());
  40      srv.set('heartbeat interval', 10);
  41      expect(srv.eio.pingInterval).to.be(10);
  42    });
  43
  44    it('should be able to set transports to engine.io', function() {
  45      var srv = io(http());
  46      srv.set('transports', ['polling']);
  47      expect(srv.eio.transports).to.eql(['polling']);
  48    });
  49
  50    it('should be able to set maxHttpBufferSize to engine.io', function() {
  51      var srv = io(http());
  52      srv.set('destroy buffer size', 10);
  53      expect(srv.eio.maxHttpBufferSize).to.eql(10);
  54    });
  55
  56    it('should be able to set path with setting resource', function(done) {
  57      var eio = io();
  58      var srv = http();
  59
  60      eio.set('resource', '/random');
  61      eio.attach(srv);
  62
  63      // Check that the server is accessible through the specified path
  64      request(srv)
  65      .get('/random/socket.io.js')
  66      .buffer(true)
  67      .end(function(err, res){
  68        if (err) return done(err);
  69        done();
  70      });
  71    });
  72
  73    it('should be able to set origins to engine.io', function() {
  74      var srv = io(http());
  75      srv.set('origins', 'http://hostname.com:*');
  76      expect(srv.origins()).to.be('http://hostname.com:*');
  77    });
  78
  79    it('should be able to set authorization and send error packet', function(done) {
  80      var httpSrv = http();
  81      var srv = io(httpSrv);
  82      srv.set('authorization', function(o, f) { f(null, false); });
  83
  84      var socket = client(httpSrv);
  85      socket.on('connect', function(){
  86        expect().fail();
  87      });
  88      socket.on('error', function(err) {
  89        expect(err).to.be('Not authorized');
  90        done();
  91      });
  92    });
  93
  94    it('should be able to set authorization and succeed', function(done) {
  95      var httpSrv = http();
  96      var srv = io(httpSrv);
  97      srv.set('authorization', function(o, f) { f(null, true); });
  98
  99      srv.on('connection', function(s) {
 100        s.on('yoyo', function(data) {
 101          expect(data).to.be('data');
 102          done();
 103        });
 104      });
 105
 106      var socket = client(httpSrv);
 107      socket.on('connect', function(){
 108        socket.emit('yoyo', 'data');
 109      });
 110
 111      socket.on('error', function(err) {
 112        expect().fail();
 113      });
 114    });
 115
 116    it('should set the handshake BC object', function(done){
 117      var httpSrv = http();
 118      var srv = io(httpSrv);
 119
 120      srv.on('connection', function(s) {
 121        expect(s.handshake).to.not.be(undefined);
 122
 123        // Headers set and has some valid properties
 124        expect(s.handshake.headers).to.be.an('object');
 125        expect(s.handshake.headers['user-agent']).to.be('node-XMLHttpRequest');
 126
 127        // Time set and is valid looking string
 128        expect(s.handshake.time).to.be.a('string');
 129        expect(s.handshake.time.split(' ').length > 0); // Is "multipart" string representation
 130
 131        // Address, xdomain, secure, issued and url set
 132        expect(s.handshake.address).to.contain('127.0.0.1');
 133        expect(s.handshake.xdomain).to.be.a('boolean');
 134        expect(s.handshake.secure).to.be.a('boolean');
 135        expect(s.handshake.issued).to.be.a('number');
 136        expect(s.handshake.url).to.be.a('string');
 137
 138        // Query set and has some right properties
 139        expect(s.handshake.query).to.be.an('object');
 140        expect(s.handshake.query.EIO).to.not.be(undefined);
 141        expect(s.handshake.query.transport).to.not.be(undefined);
 142        expect(s.handshake.query.t).to.not.be(undefined);
 143
 144        done();
 145      });
 146
 147      var socket = client(httpSrv);
 148    });
 149  });
 150
 151  describe('server attachment', function(){
 152    describe('http.Server', function(){
 153      var clientVersion = require('socket.io-client/package').version;
 154
 155      it('should serve static files', function(done){
 156        var srv = http();
 157        io(srv);
 158        request(srv)
 159        .get('/socket.io/socket.io.js')
 160        .buffer(true)
 161        .end(function(err, res){
 162          if (err) return done(err);
 163          var ctype = res.headers['content-type'];
 164          expect(ctype).to.be('application/javascript');
 165          expect(res.headers.etag).to.be('"' + clientVersion + '"');
 166          expect(res.text).to.match(/engine\.io/);
 167          expect(res.status).to.be(200);
 168          done();
 169        });
 170      });
 171
 172      it('should handle 304', function(done){
 173        var srv = http();
 174        io(srv);
 175        request(srv)
 176        .get('/socket.io/socket.io.js')
 177        .set('If-None-Match', '"' + clientVersion + '"')
 178        .end(function(err, res){
 179          if (err) return done(err);
 180          expect(res.statusCode).to.be(304);
 181          done();
 182        });
 183      });
 184
 185      it('should not serve static files', function(done){
 186        var srv = http();
 187        io(srv, { serveClient: false });
 188        request(srv)
 189        .get('/socket.io/socket.io.js')
 190        .expect(400, done);
 191      });
 192
 193      it('should work with #attach', function(done){
 194        var srv = http(function(req, res){
 195          res.writeHead(404);
 196          res.end();
 197        });
 198        var sockets = io();
 199        sockets.attach(srv);
 200        request(srv)
 201        .get('/socket.io/socket.io.js')
 202        .end(function(err, res){
 203          if (err) return done(err);
 204          expect(res.status).to.be(200);
 205          done();
 206        });
 207      });
 208    });
 209
 210    describe('port', function(done){
 211      it('should be bound', function(done){
 212        var sockets = io(54010);
 213        request('http://localhost:54010')
 214        .get('/socket.io/socket.io.js')
 215        .expect(200, done);
 216      });
 217
 218      it('should be bound as a string', function(done) {
 219        var sockets = io('54020');
 220        request('http://localhost:54020')
 221        .get('/socket.io/socket.io.js')
 222        .expect(200, done);
 223      });
 224
 225      it('with listen', function(done){
 226        var sockets = io().listen(54011);
 227        request('http://localhost:54011')
 228        .get('/socket.io/socket.io.js')
 229        .expect(200, done);
 230      });
 231
 232      it('as a string', function(done){
 233        var sockets = io().listen('54012');
 234        request('http://localhost:54012')
 235        .get('/socket.io/socket.io.js')
 236        .expect(200, done);
 237      });
 238    });
 239  });
 240
 241  describe('handshake', function(){
 242    var request = require('superagent');
 243
 244    it('should disallow request when origin defined and none specified', function(done) {
 245      var sockets = io({ origins: 'http://foo.example:*' }).listen('54013');
 246      request.get('http://localhost:54013/socket.io/default/')
 247       .query({ transport: 'polling' })
 248       .end(function (err, res) {
 249          expect(res.status).to.be(403);
 250          done();
 251        });
 252    });
 253
 254    it('should disallow request when origin defined and a different one specified', function(done) {
 255      var sockets = io({ origins: 'http://foo.example:*' }).listen('54014');
 256      request.get('http://localhost:54014/socket.io/default/')
 257       .query({ transport: 'polling' })
 258       .set('origin', 'http://herp.derp')
 259       .end(function (err, res) {
 260          expect(res.status).to.be(403);
 261          done();
 262       });
 263    });
 264
 265    it('should allow request when origin defined an the same is specified', function(done) {
 266      var sockets = io({ origins: 'http://foo.example:*' }).listen('54015');
 267      request.get('http://localhost:54015/socket.io/default/')
 268       .set('origin', 'http://foo.example')
 269       .query({ transport: 'polling' })
 270       .end(function (err, res) {
 271          expect(res.status).to.be(200);
 272          done();
 273        });
 274    });
 275
 276    it('should allow request when origin defined as function and same is supplied', function(done) {
 277      var sockets = io({ origins: function(origin,callback){
 278        if (origin == 'http://foo.example') {
 279          return callback(null, true);
 280        }
 281        return callback(null, false);
 282      } }).listen('54016');
 283      request.get('http://localhost:54016/socket.io/default/')
 284       .set('origin', 'http://foo.example')
 285       .query({ transport: 'polling' })
 286       .end(function (err, res) {
 287          expect(res.status).to.be(200);
 288          done();
 289        });
 290    });
 291
 292    it('should allow request when origin defined as function and different is supplied', function(done) {
 293      var sockets = io({ origins: function(origin,callback){
 294        if (origin == 'http://foo.example') {
 295          return callback(null, true);
 296        }
 297        return callback(null, false);
 298      } }).listen('54017');
 299      request.get('http://localhost:54017/socket.io/default/')
 300       .set('origin', 'http://herp.derp')
 301       .query({ transport: 'polling' })
 302       .end(function (err, res) {
 303          expect(res.status).to.be(403);
 304          done();
 305        });
 306    });
 307
 308    it('should allow request when origin defined as function and no origin is supplied', function(done) {
 309      var sockets = io({ origins: function(origin,callback){
 310        if (origin == '*') {
 311          return callback(null, true);
 312        }
 313        return callback(null, false);
 314      } }).listen('54021');
 315      request.get('http://localhost:54021/socket.io/default/')
 316       .query({ transport: 'polling' })
 317       .end(function (err, res) {
 318          expect(res.status).to.be(200);
 319          done();
 320        });
 321    });
 322
 323    it('should default to port 443 when protocol is https', function(done) {
 324      var sockets = io({ origins: 'https://foo.example:443' }).listen('54036');
 325      request.get('http://localhost:54036/socket.io/default/')
 326        .set('origin', 'https://foo.example')
 327        .query({ transport: 'polling' })
 328        .end(function (err, res) {
 329          expect(res.status).to.be(200);
 330          done();
 331        });
 332    });
 333
 334    it('should allow request if custom function in opts.allowRequest returns true', function(done){
 335      var sockets = io(http().listen(54022), { allowRequest: function (req, callback) {
 336        return callback(null, true);
 337      }, origins: 'http://foo.example:*' });
 338
 339      request.get('http://localhost:54022/socket.io/default/')
 340       .query({ transport: 'polling' })
 341       .end(function (err, res) {
 342          expect(res.status).to.be(200);
 343          done();
 344        });
 345    });
 346
 347    it('should disallow request if custom function in opts.allowRequest returns false', function(done){
 348      var sockets = io(http().listen(54023), { allowRequest: function (req, callback) {
 349        return callback(null, false);
 350      } });
 351      request.get('http://localhost:54023/socket.io/default/')
 352       .set('origin', 'http://foo.example')
 353       .query({ transport: 'polling' })
 354       .end(function (err, res) {
 355          expect(res.status).to.be(403);
 356          done();
 357        });
 358    });
 359
 360    it('should allow request when using an array of origins', function(done) {
 361      io({ origins: [ 'http://foo.example:54024' ] }).listen('54024');
 362      request.get('http://localhost:54024/socket.io/default/')
 363        .set('origin', 'http://foo.example:54024')
 364        .query({ transport: 'polling' })
 365        .end(function (err, res) {
 366          expect(res.status).to.be(200);
 367          done();
 368        });
 369    });
 370  });
 371
 372  describe('close', function(){
 373
 374    it('should be able to close sio sending a srv', function(){
 375      var PORT   = 54018;
 376      var srv    = http().listen(PORT);
 377      var sio    = io(srv);
 378      var net    = require('net');
 379      var server = net.createServer();
 380
 381      var clientSocket = client(srv, { reconnection: false });
 382
 383      clientSocket.on('disconnect', function init() {
 384        expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(0);
 385        server.listen(PORT);
 386      });
 387
 388      clientSocket.on('connect', function init() {
 389        expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(1);
 390        sio.close();
 391      });
 392
 393      server.once('listening', function() {
 394        // PORT should be free
 395        server.close(function(error){
 396          expect(error).to.be(undefined);
 397        });
 398      });
 399
 400    });
 401
 402    it('should be able to close sio sending a port', function(){
 403      var PORT   = 54019;
 404      var sio    = io(PORT);
 405      var net    = require('net');
 406      var server = net.createServer();
 407
 408      var clientSocket = ioc('ws://0.0.0.0:' + PORT, { reconnection: false });
 409
 410      clientSocket.on('disconnect', function init() {
 411        expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(0);
 412        server.listen(PORT);
 413      });
 414
 415      clientSocket.on('connect', function init() {
 416        expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(1);
 417        sio.close();
 418      });
 419
 420      server.once('listening', function() {
 421        // PORT should be free
 422        server.close(function(error){
 423          expect(error).to.be(undefined);
 424        });
 425      });
 426    });
 427
 428    describe('graceful close', function(){
 429      function fixture(filename) {
 430        return '"' + process.execPath + '" "' +
 431          join(__dirname, 'fixtures', filename) + '"';
 432      }
 433
 434      it('should stop socket and timers', function(done){
 435        exec(fixture('server-close.js'), done);
 436      });
 437    });
 438  });
 439
 440  describe('namespaces', function(){
 441    var Socket = require('../lib/socket');
 442    var Namespace = require('../lib/namespace');
 443
 444    it('should be accessible through .sockets', function(){
 445      var sio = io();
 446      expect(sio.sockets).to.be.a(Namespace);
 447    });
 448
 449    it('should be aliased', function(){
 450      var sio = io();
 451      expect(sio.use).to.be.a('function');
 452      expect(sio.to).to.be.a('function');
 453      expect(sio['in']).to.be.a('function');
 454      expect(sio.emit).to.be.a('function');
 455      expect(sio.send).to.be.a('function');
 456      expect(sio.write).to.be.a('function');
 457      expect(sio.clients).to.be.a('function');
 458      expect(sio.compress).to.be.a('function');
 459      expect(sio.json).to.be(sio);
 460      expect(sio.volatile).to.be(sio);
 461      expect(sio.sockets.flags).to.eql({ json: true, volatile: true });
 462      delete sio.sockets.flags;
 463    });
 464
 465    it('should automatically connect', function(done){
 466      var srv = http();
 467      var sio = io(srv);
 468      srv.listen(function(){
 469        var socket = client(srv);
 470        socket.on('connect', function(){
 471          done();
 472        });
 473      });
 474    });
 475
 476    it('should fire a `connection` event', function(done){
 477      var srv = http();
 478      var sio = io(srv);
 479      srv.listen(function(){
 480        var socket = client(srv);
 481        sio.on('connection', function(socket){
 482          expect(socket).to.be.a(Socket);
 483          done();
 484        });
 485      });
 486    });
 487
 488    it('should fire a `connect` event', function(done){
 489      var srv = http();
 490      var sio = io(srv);
 491      srv.listen(function(){
 492        var socket = client(srv);
 493        sio.on('connect', function(socket){
 494          expect(socket).to.be.a(Socket);
 495          done();
 496        });
 497      });
 498    });
 499
 500    it('should work with many sockets', function(done){
 501      var srv = http();
 502      var sio = io(srv);
 503      srv.listen(function(){
 504        sio.of('/chat');
 505        sio.of('/news');
 506        var chat = client(srv, '/chat');
 507        var news = client(srv, '/news');
 508        var total = 2;
 509        chat.on('connect', function(){
 510          --total || done();
 511        });
 512        news.on('connect', function(){
 513          --total || done();
 514        });
 515      });
 516    });
 517
 518    it('should be able to equivalently start with "" or "/" on server', function(done){
 519      var srv = http();
 520      var sio = io(srv);
 521      var total = 2;
 522      sio.of('').on('connection', function(){
 523        --total || done();
 524      });
 525      sio.of('abc').on('connection', function(){
 526        --total || done();
 527      });
 528      var c1 = client(srv, '/');
 529      var c2 = client(srv, '/abc');
 530    });
 531
 532    it('should be equivalent for "" and "/" on client', function(done){
 533      var srv = http();
 534      var sio = io(srv);
 535      sio.of('/').on('connection', function(){
 536          done();
 537      });
 538      var c1 = client(srv, '');
 539    });
 540
 541    it('should work with `of` and many sockets', function(done){
 542      var srv = http();
 543      var sio = io(srv);
 544      srv.listen(function(){
 545        var chat = client(srv, '/chat');
 546        var news = client(srv, '/news');
 547        var total = 2;
 548        sio.of('/news').on('connection', function(socket){
 549          expect(socket).to.be.a(Socket);
 550          --total || done();
 551        });
 552        sio.of('/news').on('connection', function(socket){
 553          expect(socket).to.be.a(Socket);
 554          --total || done();
 555        });
 556      });
 557    });
 558
 559    it('should work with `of` second param', function(done){
 560      var srv = http();
 561      var sio = io(srv);
 562      srv.listen(function(){
 563        var chat = client(srv, '/chat');
 564        var news = client(srv, '/news');
 565        var total = 2;
 566        sio.of('/news', function(socket){
 567          expect(socket).to.be.a(Socket);
 568          --total || done();
 569        });
 570        sio.of('/news', function(socket){
 571          expect(socket).to.be.a(Socket);
 572          --total || done();
 573        });
 574      });
 575    });
 576
 577    it('should disconnect upon transport disconnection', function(done){
 578      var srv = http();
 579      var sio = io(srv);
 580      srv.listen(function(){
 581        var chat = client(srv, '/chat');
 582        var news = client(srv, '/news');
 583        var total = 2;
 584        var totald = 2;
 585        var s;
 586        sio.of('/news', function(socket){
 587          socket.on('disconnect', function(reason){
 588            --totald || done();
 589          });
 590          --total || close();
 591        });
 592        sio.of('/chat', function(socket){
 593          s = socket;
 594          socket.on('disconnect', function(reason){
 595            --totald || done();
 596          });
 597          --total || close();
 598        });
 599        function close(){
 600          s.disconnect(true);
 601        }
 602      });
 603    });
 604
 605    it('should disconnect both default and custom namespace upon disconnect', function(done){
 606      var srv = http();
 607      var sio = io(srv);
 608      srv.listen(function(){
 609        var lolcats = client(srv, '/lolcats');
 610        var total = 2;
 611        var totald = 2;
 612        var s;
 613        sio.of('/', function(socket){
 614          socket.on('disconnect', function(reason){
 615            --totald || done();
 616          });
 617          --total || close();
 618        });
 619        sio.of('/lolcats', function(socket){
 620          s = socket;
 621          socket.on('disconnect', function(reason){
 622            --totald || done();
 623          });
 624          --total || close();
 625        });
 626        function close(){
 627          s.disconnect(true);
 628        }
 629      });
 630    });
 631
 632    it('should not crash while disconnecting socket', function(done){
 633      var srv = http();
 634      var sio = io(srv);
 635      srv.listen(function(){
 636        var socket = client(srv,'/ns');
 637        sio.on('connection', function(socket){
 638          socket.disconnect();
 639          done();
 640        });
 641      });
 642    });
 643
 644    it('should fire a `disconnecting` event just before leaving all rooms', function(done){
 645      var srv = http();
 646      var sio = io(srv);
 647      srv.listen(function(){
 648        var socket = client(srv);
 649
 650        sio.on('connection', function(s){
 651          s.join('a', function(){
 652            s.disconnect();
 653          });
 654
 655          var total = 2;
 656          s.on('disconnecting', function(reason){
 657            expect(Object.keys(s.rooms)).to.eql([s.id, 'a']);
 658            total--;
 659          });
 660
 661          s.on('disconnect', function(reason){
 662            expect(Object.keys(s.rooms)).to.eql([]);
 663            --total || done();
 664          });
 665        });
 666      });
 667    });
 668
 669    it('should return error connecting to non-existent namespace', function(done){
 670      var srv = http();
 671      var sio = io(srv);
 672      srv.listen(function(){
 673        var socket = client(srv,'/doesnotexist');
 674        socket.on('error', function(err) {
 675          expect(err).to.be('Invalid namespace');
 676          done();
 677        });
 678      });
 679    });
 680    
 681    it('should not reuse same-namespace connections', function(done){
 682      var srv = http();
 683      var sio = io(srv);
 684      var connections = 0;
 685
 686      srv.listen(function() {
 687        var clientSocket1 = client(srv);
 688        var clientSocket2 = client(srv);
 689        sio.on('connection', function() {
 690          connections++;
 691          if (connections === 2) {
 692            done();
 693          }
 694        });
 695      });
 696    });
 697
 698    it('should find all clients in a namespace', function(done){
 699      var srv = http();
 700      var sio = io(srv);
 701      var chatSids = [];
 702      var otherSid = null;
 703      srv.listen(function(){
 704        var c1 = client(srv, '/chat');
 705        var c2 = client(srv, '/chat', {forceNew: true});
 706        var c3 = client(srv, '/other', {forceNew: true});
 707        var total = 3;
 708        sio.of('/chat').on('connection', function(socket){
 709          chatSids.push(socket.id);
 710          --total || getClients();
 711        });
 712        sio.of('/other').on('connection', function(socket){
 713          otherSid = socket.id;
 714          --total || getClients();
 715        });
 716      });
 717      function getClients() {
 718        sio.of('/chat').clients(function(error, sids) {
 719          expect(error).to.not.be.ok();
 720          expect(sids).to.contain(chatSids[0]);
 721          expect(sids).to.contain(chatSids[1]);
 722          expect(sids).to.not.contain(otherSid);
 723          done();
 724        });
 725      }
 726    });
 727
 728    it('should find all clients in a namespace room', function(done){
 729      var srv = http();
 730      var sio = io(srv);
 731      var chatFooSid = null;
 732      var chatBarSid = null;
 733      var otherSid = null;
 734      srv.listen(function(){
 735        var c1 = client(srv, '/chat');
 736        var c2 = client(srv, '/chat', {forceNew: true});
 737        var c3 = client(srv, '/other', {forceNew: true});
 738        var chatIndex = 0;
 739        var total = 3;
 740        sio.of('/chat').on('connection', function(socket){
 741          if (chatIndex++) {
 742            socket.join('foo', function() {
 743              chatFooSid = socket.id;
 744              --total || getClients();
 745            });
 746          } else {
 747            socket.join('bar', function() {
 748              chatBarSid = socket.id;
 749              --total || getClients();
 750            });
 751          }
 752        });
 753        sio.of('/other').on('connection', function(socket){
 754          socket.join('foo', function() {
 755            otherSid = socket.id;
 756            --total || getClients();
 757          });
 758        });
 759      });
 760      function getClients() {
 761        sio.of('/chat').in('foo').clients(function(error, sids) {
 762          expect(error).to.not.be.ok();
 763          expect(sids).to.contain(chatFooSid);
 764          expect(sids).to.not.contain(chatBarSid);
 765          expect(sids).to.not.contain(otherSid);
 766          done();
 767        });
 768      }
 769    });
 770
 771    it('should find all clients across namespace rooms', function(done){
 772      var srv = http();
 773      var sio = io(srv);
 774      var chatFooSid = null;
 775      var chatBarSid = null;
 776      var otherSid = null;
 777      srv.listen(function(){
 778        var c1 = client(srv, '/chat');
 779        var c2 = client(srv, '/chat', {forceNew: true});
 780        var c3 = client(srv, '/other', {forceNew: true});
 781        var chatIndex = 0;
 782        var total = 3;
 783        sio.of('/chat').on('connection', function(socket){
 784          if (chatIndex++) {
 785            socket.join('foo', function() {
 786              chatFooSid = socket.id;
 787              --total || getClients();
 788            });
 789          } else {
 790            socket.join('bar', function() {
 791              chatBarSid = socket.id;
 792              --total || getClients();
 793            });
 794          }
 795        });
 796        sio.of('/other').on('connection', function(socket){
 797          socket.join('foo', function() {
 798            otherSid = socket.id;
 799            --total || getClients();
 800          });
 801        });
 802      });
 803      function getClients() {
 804        sio.of('/chat').clients(function(error, sids) {
 805          expect(error).to.not.be.ok();
 806          expect(sids).to.contain(chatFooSid);
 807          expect(sids).to.contain(chatBarSid);
 808          expect(sids).to.not.contain(otherSid);
 809          done();
 810        });
 811      }
 812    });
 813
 814    it('should not emit volatile event after regular event', function(done) {
 815      var srv = http();
 816      var sio = io(srv);
 817
 818      var counter = 0;
 819      srv.listen(function(){
 820        sio.of('/chat').on('connection', function(s){
 821          // Wait to make sure there are no packets being sent for opening the connection
 822          setTimeout(function() {
 823            sio.of('/chat').emit('ev', 'data');
 824            sio.of('/chat').volatile.emit('ev', 'data');
 825          }, 50);
 826        });
 827
 828        var socket = client(srv, '/chat');
 829        socket.on('ev', function() {
 830          counter++;
 831        });
 832      });
 833
 834      setTimeout(function() {
 835        expect(counter).to.be(1);
 836        done();
 837      }, 500);
 838    });
 839
 840    it('should emit volatile event', function(done) {
 841      var srv = http();
 842      var sio = io(srv);
 843
 844      var counter = 0;
 845      srv.listen(function(){
 846        sio.of('/chat').on('connection', function(s){
 847          // Wait to make sure there are no packets being sent for opening the connection
 848          setTimeout(function() {
 849            sio.of('/chat').volatile.emit('ev', 'data');
 850          }, 100);
 851        });
 852
 853        var socket = client(srv, '/chat');
 854        socket.on('ev', function() {
 855          counter++;
 856        });
 857      });
 858
 859      setTimeout(function() {
 860        expect(counter).to.be(1);
 861        done();
 862      }, 500);
 863    });
 864
 865    it('should enable compression by default', function(done){
 866      var srv = http();
 867      var sio = io(srv);
 868      srv.listen(function(){
 869        var socket = client(srv, '/chat');
 870        sio.of('/chat').on('connection', function(s){
 871          s.conn.once('packetCreate', function(packet) {
 872            expect(packet.options.compress).to.be(true);
 873            done();
 874          });
 875          sio.of('/chat').emit('woot', 'hi');
 876        });
 877      });
 878    });
 879
 880    it('should disable compression', function(done){
 881      var srv = http();
 882      var sio = io(srv);
 883      srv.listen(function(){
 884        var socket = client(srv, '/chat');
 885        sio.of('/chat').on('connection', function(s){
 886          s.conn.once('packetCreate', function(packet) {
 887            expect(packet.options.compress).to.be(false);
 888            done();
 889          });
 890          sio.of('/chat').compress(false).emit('woot', 'hi');
 891        });
 892      });
 893    });
 894
 895    describe('dynamic namespaces', function () {
 896      it('should allow connections to dynamic namespaces with a regex', function(done){
 897        const srv = http();
 898        const sio = io(srv);
 899        let count = 0;
 900        srv.listen(function(){
 901          const socket = client(srv, '/dynamic-101');
 902          let dynamicNsp = sio.of(/^\/dynamic-\d+$/).on('connect', (socket) => {
 903            expect(socket.nsp.name).to.be('/dynamic-101');
 904            dynamicNsp.emit('hello', 1, '2', { 3: '4'});
 905            if (++count === 4) done();
 906          }).use((socket, next) => {
 907            next();
 908            if (++count === 4) done();
 909          });
 910          socket.on('error', function(err) {
 911            expect().fail();
 912          });
 913          socket.on('connect', () => {
 914            if (++count === 4) done();
 915          });
 916          socket.on('hello', (a, b, c) => {
 917            expect(a).to.eql(1);
 918            expect(b).to.eql('2');
 919            expect(c).to.eql({ 3: '4' });
 920            if (++count === 4) done();
 921          });
 922        });
 923      });
 924
 925      it('should allow connections to dynamic namespaces with a function', function(done){
 926        const srv = http();
 927        const sio = io(srv);
 928        srv.listen(function(){
 929          const socket = client(srv, '/dynamic-101');
 930          sio.of((name, query, next) => next(null, '/dynamic-101' === name));
 931          socket.on('connect', done);
 932        });
 933      });
 934
 935      it('should disallow connections when no dynamic namespace matches', function(done){
 936        const srv = http();
 937        const sio = io(srv);
 938        srv.listen(function(){
 939          const socket = client(srv, '/abc');
 940          sio.of(/^\/dynamic-\d+$/);
 941          sio.of((name, query, next) => next(null, '/dynamic-101' === name));
 942          socket.on('error', (err) => {
 943            expect(err).to.be('Invalid namespace');
 944            done();
 945          });
 946        });
 947      });
 948    });
 949  });
 950
 951  describe('socket', function(){
 952
 953    it('should not fire events more than once after manually reconnecting', function(done) {
 954      var srv = http();
 955      var sio = io(srv);
 956      srv.listen(function(){
 957        var clientSocket = client(srv, { reconnection: false });
 958        clientSocket.on('connect', function init() {
 959          clientSocket.removeListener('connect', init);
 960          clientSocket.io.engine.close();
 961
 962          clientSocket.connect();
 963          clientSocket.on('connect', function() {
 964            done();
 965          });
 966        });
 967      });
 968    });
 969
 970    it('should not fire reconnect_failed event more than once when server closed', function(done) {
 971      var srv = http();
 972      var sio = io(srv);
 973      srv.listen(function(){
 974        var clientSocket = client(srv, { reconnectionAttempts: 3, reconnectionDelay: 10 });
 975        clientSocket.on('connect', function() {
 976          srv.close();
 977        });
 978
 979        clientSocket.on('reconnect_failed', function() {
 980          done();
 981        });
 982      });
 983    });
 984
 985    it('should receive events', function(done){
 986      var srv = http();
 987      var sio = io(srv);
 988      srv.listen(function(){
 989        var socket = client(srv);
 990        sio.on('connection', function(s){
 991          s.on('random', function(a, b, c){
 992            expect(a).to.be(1);
 993            expect(b).to.be('2');
 994            expect(c).to.eql([3]);
 995            done();
 996          });
 997          socket.emit('random', 1, '2', [3]);
 998        });
 999      });
1000    });
1001
1002    it('should receive message events through `send`', function(done){
1003      var srv = http();
1004      var sio = io(srv);
1005      srv.listen(function(){
1006        var socket = client(srv);
1007        sio.on('connection', function(s){
1008          s.on('message', function(a){
1009            expect(a).to.be(1337);
1010            done();
1011          });
1012          socket.send(1337);
1013        });
1014      });
1015    });
1016
1017    it('should error with null messages', function(done){
1018      var srv = http();
1019      var sio = io(srv);
1020      srv.listen(function(){
1021        var socket = client(srv);
1022        sio.on('connection', function(s){
1023          s.on('message', function(a){
1024            expect(a).to.be(null);
1025            done();
1026          });
1027          socket.send(null);
1028        });
1029      });
1030    });
1031
1032    it('should handle transport null messages', function(done){
1033      var srv = http();
1034      var sio = io(srv);
1035      srv.listen(function(){
1036        var socket = client(srv, { reconnection: false });
1037        sio.on('connection', function(s){
1038          s.on('error', function(err){
1039            expect(err).to.be.an(Error);
1040            s.on('disconnect', function(reason){
1041              expect(reason).to.be('forced close');
1042              done();
1043            });
1044          });
1045          s.client.ondata(null);
1046        });
1047      });
1048    });
1049
1050    it('should emit events', function(done){
1051      var srv = http();
1052      var sio = io(srv);
1053      srv.listen(function(){
1054        var socket = client(srv);
1055        socket.on('woot', function(a){
1056          expect(a).to.be('tobi');
1057          done();
1058        });
1059        sio.on('connection', function(s){
1060          s.emit('woot', 'tobi');
1061        });
1062      });
1063    });
1064
1065    it('should emit events with utf8 multibyte character', function(done) {
1066      var srv = http();
1067      var sio = io(srv);
1068      srv.listen(function(){
1069        var socket = client(srv);
1070        var i = 0;
1071        socket.on('hoot', function(a){
1072          expect(a).to.be('utf8 — string');
1073          i++;
1074
1075          if (3 == i) {
1076            done();
1077          }
1078        });
1079        sio.on('connection', function(s){
1080          s.emit('hoot', 'utf8 — string');
1081          s.emit('hoot', 'utf8 — string');
1082          s.emit('hoot', 'utf8 — string');
1083        });
1084      });
1085    });
1086
1087    it('should emit events with binary data', function(done){
1088      var srv = http();
1089      var sio = io(srv);
1090      srv.listen(function(){
1091        var socket = client(srv);
1092        var imageData;
1093        socket.on('doge', function(a){
1094          expect(Buffer.isBuffer(a)).to.be(true);
1095          expect(imageData.length).to.equal(a.length);
1096          expect(imageData[0]).to.equal(a[0]);
1097          expect(imageData[imageData.length - 1]).to.equal(a[a.length - 1]);
1098          done();
1099        });
1100        sio.on('connection', function(s){
1101          fs.readFile(join(__dirname, 'support', 'doge.jpg'), function(err, data){
1102            if (err) return done(err);
1103            imageData = data;
1104            s.emit('doge', data);
1105          });
1106        });
1107      });
1108    });
1109
1110    it('should emit events with several types of data (including binary)', function(done){
1111      var srv = http();
1112      var sio = io(srv);
1113      srv.listen(function(){
1114        var socket = client(srv);
1115        socket.on('multiple', function(a, b, c, d, e, f){
1116          expect(a).to.be(1);
1117          expect(Buffer.isBuffer(b)).to.be(true);
1118          expect(c).to.be('3');
1119          expect(d).to.eql([4]);
1120          expect(Buffer.isBuffer(e)).to.be(true);
1121          expect(Buffer.isBuffer(f[0])).to.be(true);
1122          expect(f[1]).to.be('swag');
1123          expect(Buffer.isBuffer(f[2])).to.be(true);
1124          done();
1125        });
1126        sio.on('connection', function(s){
1127          fs.readFile(join(__dirname, 'support', 'doge.jpg'), function(err, data){
1128            if (err) return done(err);
1129            var buf = Buffer.from('asdfasdf', 'utf8');
1130            s.emit('multiple', 1, data, '3', [4], buf, [data, 'swag', buf]);
1131          });
1132        });
1133      });
1134    });
1135
1136    it('should receive events with binary data', function(done){
1137      var srv = http();
1138      var sio = io(srv);
1139      srv.listen(function(){
1140        var socket = client(srv);
1141        sio.on('connection', function(s){
1142          s.on('buff', function(a){
1143            expect(Buffer.isBuffer(a)).to.be(true);
1144            done();
1145          });
1146          var buf = Buffer.from('abcdefg', 'utf8');
1147          socket.emit('buff', buf);
1148        });
1149      });
1150    });
1151
1152    it('should receive events with several types of data (including binary)', function(done){
1153      var srv = http();
1154      var sio = io(srv);
1155      srv.listen(function(){
1156        var socket = client(srv);
1157        sio.on('connection', function(s){
1158          s.on('multiple', function(a, b, c, d, e, f){
1159          expect(a).to.be(1);
1160          expect(Buffer.isBuffer(b)).to.be(true);
1161          expect(c).to.be('3');
1162          expect(d).to.eql([4]);
1163          expect(Buffer.isBuffer(e)).to.be(true);
1164          expect(Buffer.isBuffer(f[0])).to.be(true);
1165          expect(f[1]).to.be('swag');
1166          expect(Buffer.isBuffer(f[2])).to.be(true);
1167          done();
1168          });
1169          fs.readFile(join(__dirname, 'support', 'doge.jpg'), function(err, data){
1170            if (err) return done(err);
1171            var buf = Buffer.from('asdfasdf', 'utf8');
1172            socket.emit('multiple', 1, data, '3', [4], buf, [data, 'swag', buf]);
1173          });
1174        });
1175      });
1176    });
1177
1178    it('should not emit volatile event after regular event (polling)', function(done) {
1179      var srv = http();
1180      var sio = io(srv, { transports: ['polling'] });
1181
1182      var counter = 0;
1183      srv.listen(function(){
1184        sio.on('connection', function(s){
1185          s.emit('ev', 'data');
1186          s.volatile.emit('ev', 'data');
1187        });
1188
1189        var socket = client(srv, { transports: ['polling'] });
1190        socket.on('ev', function() {
1191          counter++;
1192        });
1193      });
1194
1195      setTimeout(function() {
1196        expect(counter).to.be(1);
1197        done();
1198      }, 200);
1199    });
1200
1201    it('should not emit volatile event after regular event (ws)', function(done) {
1202      var srv = http();
1203      var sio = io(srv, { transports: ['websocket'] });
1204
1205      var counter = 0;
1206      srv.listen(function(){
1207        sio.on('connection', function(s){
1208          s.emit('ev', 'data');
1209          s.volatile.emit('ev', 'data');
1210        });
1211
1212        var socket = client(srv, { transports: ['websocket'] });
1213        socket.on('ev', function() {
1214          counter++;
1215        });
1216      });
1217
1218      setTimeout(function() {
1219        expect(counter).to.be(1);
1220        done();
1221      }, 200);
1222    });
1223
1224    it('should emit volatile event (polling)', function(done) {
1225      var srv = http();
1226      var sio = io(srv, { transports: ['polling'] });
1227
1228      var counter = 0;
1229      srv.listen(function(){
1230        sio.on('connection', function(s){
1231          // Wait to make sure there are no packets being sent for opening the connection
1232          setTimeout(function() {
1233            s.volatile.emit('ev', 'data');
1234          }, 100);
1235        });
1236
1237        var socket = client(srv, { transports: ['polling'] });
1238        socket.on('ev', function() {
1239          counter++;
1240        });
1241      });
1242
1243      setTimeout(function() {
1244        expect(counter).to.be(1);
1245        done();
1246      }, 500);
1247    });
1248
1249    it('should emit volatile event (ws)', function(done) {
1250      var srv = http();
1251      var sio = io(srv, { transports: ['websocket'] });
1252
1253      var counter = 0;
1254      srv.listen(function(){
1255        sio.on('connection', function(s){
1256          // Wait to make sure there are no packets being sent for opening the connection
1257          setTimeout(function() {
1258            s.volatile.emit('ev', 'data');
1259          }, 20);
1260        });
1261
1262        var socket = client(srv, { transports: ['websocket'] });
1263        socket.on('ev', function() {
1264          counter++;
1265        });
1266      });
1267
1268      setTimeout(function() {
1269        expect(counter).to.be(1);
1270        done();
1271      }, 200);
1272    });
1273
1274    it('should emit only one consecutive volatile event (polling)', function(done) {
1275      var srv = http();
1276      var sio = io(srv, { transports: ['polling'] });
1277
1278      var counter = 0;
1279      srv.listen(function(){
1280        sio.on('connection', function(s){
1281          // Wait to make sure there are no packets being sent for opening the connection
1282          setTimeout(function() {
1283            s.volatile.emit('ev', 'data');
1284            s.volatile.emit('ev', 'data');
1285          }, 100);
1286        });
1287
1288        var socket = client(srv, { transports: ['polling'] });
1289        socket.on('ev', function() {
1290          counter++;
1291        });
1292      });
1293
1294      setTimeout(function() {
1295        expect(counter).to.be(1);
1296        done();
1297      }, 500);
1298    });
1299
1300    it('should emit only one consecutive volatile event (ws)', function(done) {
1301      var srv = http();
1302      var sio = io(srv, { transports: ['websocket'] });
1303
1304      var counter = 0;
1305      srv.listen(function(){
1306        sio.on('connection', function(s){
1307          // Wait to make sure there are no packets being sent for opening the connection
1308          setTimeout(function() {
1309            s.volatile.emit('ev', 'data');
1310            s.volatile.emit('ev', 'data');
1311          }, 20);
1312        });
1313
1314        var socket = client(srv, { transports: ['websocket'] });
1315        socket.on('ev', function() {
1316          counter++;
1317        });
1318      });
1319
1320      setTimeout(function() {
1321        expect(counter).to.be(1);
1322        done();
1323      }, 200);
1324    });
1325
1326    it('should emit regular events after trying a failed volatile event (polling)', function(done) {
1327      var srv = http();
1328      var sio = io(srv, { transports: ['polling'] });
1329
1330      var counter = 0;
1331      srv.listen(function(){
1332        sio.on('connection', function(s){
1333          // Wait to make sure there are no packets being sent for opening the connection
1334          setTimeout(function() {
1335            s.emit('ev', 'data');
1336            s.volatile.emit('ev', 'data');
1337            s.emit('ev', 'data');
1338          }, 20);
1339        });
1340
1341        var socket = client(srv, { transports: ['polling'] });
1342        socket.on('ev', function() {
1343          counter++;
1344        });
1345      });
1346
1347      setTimeout(function() {
1348        expect(counter).to.be(2);
1349        done();
1350      }, 200);
1351    });
1352
1353    it('should emit regular events after trying a failed volatile event (ws)', function(done) {
1354      var srv = http();
1355      var sio = io(srv, { transports: ['websocket'] });
1356
1357      var counter = 0;
1358      srv.listen(function(){
1359        sio.on('connection', function(s){
1360          // Wait to make sure there are no packets being sent for opening the connection
1361          setTimeout(function() {
1362            s.emit('ev', 'data');
1363            s.volatile.emit('ev', 'data');
1364            s.emit('ev', 'data');
1365          }, 20);
1366        });
1367
1368        var socket = client(srv, { transports: ['websocket'] });
1369        socket.on('ev', function() {
1370          counter++;
1371        });
1372      });
1373
1374      setTimeout(function() {
1375        expect(counter).to.be(2);
1376        done();
1377      }, 200);
1378    });
1379
1380    it('should emit message events through `send`', function(done){
1381      var srv = http();
1382      var sio = io(srv);
1383      srv.listen(function(){
1384        var socket = client(srv);
1385        socket.on('message', function(a){
1386          expect(a).to.be('a');
1387          done();
1388        });
1389        sio.on('connection', function(s){
1390          s.send('a');
1391        });
1392      });
1393    });
1394
1395    it('should receive event with callbacks', function(done){
1396      var srv = http();
1397      var sio = io(srv);
1398      srv.listen(function(){
1399        var socket = client(srv);
1400        sio.on('connection', function(s){
1401          s.on('woot', function(fn){
1402            fn(1, 2);
1403          });
1404          socket.emit('woot', function(a, b){
1405            expect(a).to.be(1);
1406            expect(b).to.be(2);
1407            done();
1408          });
1409        });
1410      });
1411    });
1412
1413    it('should receive all events emitted from namespaced client immediately and in order', function(done) {
1414      var srv = http();
1415      var sio = io(srv);
1416      var total = 0;
1417      srv.listen(function(){
1418        sio.of('/chat', function(s){
1419          s.on('hi', function(letter){
1420            total++;
1421            if (total == 2 && letter == 'b') {
1422              done();
1423            } else if (total == 1 && letter != 'a') {
1424              throw new Error('events out of order');
1425            }
1426          });
1427        });
1428
1429        var chat = client(srv, '/chat');
1430        chat.emit('hi', 'a');
1431        setTimeout(function() {
1432          chat.emit('hi', 'b');
1433        }, 50);
1434      });
1435    });
1436
1437    it('should emit events with callbacks', function(done){
1438      var srv = http();
1439      var sio = io(srv);
1440      srv.listen(function(){
1441        var socket = client(srv);
1442        sio.on('connection', function(s){
1443          socket.on('hi', function(fn){
1444            fn();
1445          });
1446          s.emit('hi', function(){
1447            done();
1448          });
1449        });
1450      });
1451    });
1452
1453    it('should receive events with args and callback', function(done){
1454      var srv = http();
1455      var sio = io(srv);
1456      srv.listen(function(){
1457        var socket = client(srv);
1458        sio.on('connection', function(s){
1459          s.on('woot', function(a, b, fn){
1460            expect(a).to.be(1);
1461            expect(b).to.be(2);
1462            fn();
1463          });
1464          socket.emit('woot', 1, 2, function(){
1465            done();
1466          });
1467        });
1468      });
1469    });
1470
1471    it('should emit events with args and callback', function(done){
1472      var srv = http();
1473      var sio = io(srv);
1474      srv.listen(function(){
1475        var socket = client(srv);
1476        sio.on('connection', function(s){
1477          socket.on('hi', function(a, b, fn){
1478            expect(a).to.be(1);
1479            expect(b).to.be(2);
1480            fn();
1481          });
1482          s.emit('hi', 1, 2, function(){
1483            done();
1484          });
1485        });
1486      });
1487    });
1488
1489    it('should receive events with binary args and callbacks', function(done) {
1490      var srv = http();
1491      var sio = io(srv);
1492      srv.listen(function(){
1493        var socket = client(srv);
1494        sio.on('connection', function(s){
1495          s.on('woot', function(buf, fn){
1496            expect(Buffer.isBuffer(buf)).to.be(true);
1497            fn(1, 2);
1498          });
1499          socket.emit('woot', Buffer.alloc(3), function(a, b){
1500            expect(a).to.be(1);
1501            expect(b).to.be(2);
1502            done();
1503          });
1504        });
1505      });
1506    });
1507
1508    it('should emit events with binary args and callback', function(done){
1509      var srv = http();
1510      var sio = io(srv);
1511      srv.listen(function(){
1512        var socket = client(srv);
1513        sio.on('connection', function(s){
1514          socket.on('hi', function(a, fn){
1515            expect(Buffer.isBuffer(a)).to.be(true);
1516            fn();
1517          });
1518          s.emit('hi', Buffer.alloc(4), function(){
1519            done();
1520          });
1521        });
1522      });
1523    });
1524
1525    it('should emit events and receive binary data in a callback', function(done) {
1526      var srv = http();
1527      var sio = io(srv);
1528      srv.listen(function(){
1529        var socket = client(srv);
1530        sio.on('connection', function(s){
1531          socket.on('hi', function(fn){
1532            fn(Buffer.alloc(1));
1533          });
1534          s.emit('hi', function(a){
1535            expect(Buffer.isBuffer(a)).to.be(true);
1536            done();
1537          });
1538        });
1539      });
1540    });
1541
1542    it('should receive events and pass binary data in a callback', function(done) {
1543      var srv = http();
1544      var sio = io(srv);
1545      srv.listen(function(){
1546        var socket = client(srv);
1547        sio.on('connection', function(s){
1548          s.on('woot', function(fn){
1549            fn(Buffer.alloc(2));
1550          });
1551          socket.emit('woot', function(a){
1552            expect(Buffer.isBuffer(a)).to.be(true);
1553            done();
1554          });
1555        });
1556      });
1557    });
1558
1559    it('should have access to the client', function(done){
1560      var srv = http();
1561      var sio = io(srv);
1562      srv.listen(function(){
1563        var socket = client(srv);
1564        sio.on('connection', function(s){
1565          expect(s.client).to.be.an('object');
1566          done();
1567        });
1568      });
1569    });
1570
1571    it('should have access to the connection', function(done){
1572      var srv = http();
1573      var sio = io(srv);
1574      srv.listen(function(){
1575        var socket = client(srv);
1576        sio.on('connection', function(s){
1577          expect(s.client.conn).to.be.an('object');
1578          expect(s.conn).to.be.an('object');
1579          done();
1580        });
1581      });
1582    });
1583
1584    it('should have access to the request', function(done){
1585      var srv = http();
1586      var sio = io(srv);
1587      srv.listen(function(){
1588        var socket = client(srv);
1589        sio.on('connection', function(s){
1590          expect(s.client.request.headers).to.be.an('object');
1591          expect(s.request.headers).to.be.an('object');
1592          done();
1593        });
1594      });
1595    });
1596
1597    it('should see query parameters in the request', function(done) {
1598      var srv = http();
1599      var sio = io(srv);
1600      srv.listen(function() {
1601        var socket = client(srv, {query: {key1: 1, key2: 2}});
1602        sio.on('connection', function(s) {
1603          var parsed = require('url').parse(s.request.url);
1604          var query = require('querystring').parse(parsed.query);
1605          expect(query.key1).to.be('1');
1606          expect(query.key2).to.be('2');
1607          done();
1608        });
1609      });
1610    });
1611    
1612    it('should see query parameters sent from secondary namespace connections in handshake object', function(done){
1613      var srv = http();
1614      var sio = io(srv);
1615      var client1 = client(srv);
1616      var client2 = client(srv, '/connection2', {query: {key1: 'aa', key2: '&=bb'}});
1617      sio.on('connection', function(s){
1618      });
1619      sio.of('/connection2').on('connection', function(s){
1620        expect(s.handshake.query.key1).to.be('aa');
1621        expect(s.handshake.query.key2).to.be('&=bb');
1622        done();
1623      });
1624
1625
1626    });
1627
1628    it('should handle very large json', function(done){
1629      this.timeout(30000);
1630      var srv = http();
1631      var sio = io(srv, { perMessageDeflate: false });
1632      var received = 0;
1633      srv.listen(function(){
1634        var socket = client(srv);
1635        socket.on('big', function(a){
1636          expect(Buffer.isBuffer(a.json)).to.be(false);
1637          if (++received == 3)
1638            done();
1639          else
1640            socket.emit('big', a);
1641        });
1642        sio.on('connection', function(s){
1643          fs.readFile(join(__dirname, 'fixtures', 'big.json'), function(err, data){
1644            if (err) return done(err);
1645            data = JSON.parse(data);
1646            s.emit('big', {hello: 'friend', json: data});
1647          });
1648          s.on('big', function(a){
1649            s.emit('big', a);
1650          });
1651        });
1652      });
1653    });
1654
1655    it('should handle very large binary data', function(done){
1656      this.timeout(30000);
1657      var srv = http();
1658      var sio = io(srv, { perMessageDeflate: false });
1659      var received = 0;
1660      srv.listen(function(){
1661        var socket = client(srv);
1662        socket.on('big', function(a){
1663          expect(Buffer.isBuffer(a.image)).to.be(true);
1664          if (++received == 3)
1665            done();
1666          else
1667            socket.emit('big'

Large files files are truncated, but you can click here to view the full file