PageRenderTime 66ms CodeModel.GetById 24ms RepoModel.GetById 0ms app.codeStats 0ms

/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
Possible License(s): MIT
  1. 'use strict';
  2. var http = require('http').Server;
  3. var io = require('../lib');
  4. var fs = require('fs');
  5. var join = require('path').join;
  6. var exec = require('child_process').exec;
  7. var ioc = require('socket.io-client');
  8. var request = require('supertest');
  9. var expect = require('expect.js');
  10. // Creates a socket.io client for the given server
  11. function client(srv, nsp, opts){
  12. if ('object' == typeof nsp) {
  13. opts = nsp;
  14. nsp = null;
  15. }
  16. var addr = srv.address();
  17. if (!addr) addr = srv.listen().address();
  18. var url = 'ws://localhost:' + addr.port + (nsp || '');
  19. return ioc(url, opts);
  20. }
  21. describe('socket.io', function(){
  22. it.skip('should be the same version as client', function(){
  23. var version = require('../package').version;
  24. expect(version).to.be(require('socket.io-client/package').version);
  25. });
  26. describe('set', function() {
  27. it('should be able to set ping timeout to engine.io', function() {
  28. var srv = io(http());
  29. srv.set('heartbeat timeout', 10);
  30. expect(srv.eio.pingTimeout).to.be(10);
  31. });
  32. it('should be able to set ping interval to engine.io', function() {
  33. var srv = io(http());
  34. srv.set('heartbeat interval', 10);
  35. expect(srv.eio.pingInterval).to.be(10);
  36. });
  37. it('should be able to set transports to engine.io', function() {
  38. var srv = io(http());
  39. srv.set('transports', ['polling']);
  40. expect(srv.eio.transports).to.eql(['polling']);
  41. });
  42. it('should be able to set maxHttpBufferSize to engine.io', function() {
  43. var srv = io(http());
  44. srv.set('destroy buffer size', 10);
  45. expect(srv.eio.maxHttpBufferSize).to.eql(10);
  46. });
  47. it('should be able to set path with setting resource', function(done) {
  48. var eio = io();
  49. var srv = http();
  50. eio.set('resource', '/random');
  51. eio.attach(srv);
  52. // Check that the server is accessible through the specified path
  53. request(srv)
  54. .get('/random/socket.io.js')
  55. .buffer(true)
  56. .end(function(err, res){
  57. if (err) return done(err);
  58. done();
  59. });
  60. });
  61. it('should be able to set origins to engine.io', function() {
  62. var srv = io(http());
  63. srv.set('origins', 'http://hostname.com:*');
  64. expect(srv.origins()).to.be('http://hostname.com:*');
  65. });
  66. it('should be able to set authorization and send error packet', function(done) {
  67. var httpSrv = http();
  68. var srv = io(httpSrv);
  69. srv.set('authorization', function(o, f) { f(null, false); });
  70. var socket = client(httpSrv);
  71. socket.on('connect', function(){
  72. expect().fail();
  73. });
  74. socket.on('error', function(err) {
  75. expect(err).to.be('Not authorized');
  76. done();
  77. });
  78. });
  79. it('should be able to set authorization and succeed', function(done) {
  80. var httpSrv = http();
  81. var srv = io(httpSrv);
  82. srv.set('authorization', function(o, f) { f(null, true); });
  83. srv.on('connection', function(s) {
  84. s.on('yoyo', function(data) {
  85. expect(data).to.be('data');
  86. done();
  87. });
  88. });
  89. var socket = client(httpSrv);
  90. socket.on('connect', function(){
  91. socket.emit('yoyo', 'data');
  92. });
  93. socket.on('error', function(err) {
  94. expect().fail();
  95. });
  96. });
  97. it('should set the handshake BC object', function(done){
  98. var httpSrv = http();
  99. var srv = io(httpSrv);
  100. srv.on('connection', function(s) {
  101. expect(s.handshake).to.not.be(undefined);
  102. // Headers set and has some valid properties
  103. expect(s.handshake.headers).to.be.an('object');
  104. expect(s.handshake.headers['user-agent']).to.be('node-XMLHttpRequest');
  105. // Time set and is valid looking string
  106. expect(s.handshake.time).to.be.a('string');
  107. expect(s.handshake.time.split(' ').length > 0); // Is "multipart" string representation
  108. // Address, xdomain, secure, issued and url set
  109. expect(s.handshake.address).to.contain('127.0.0.1');
  110. expect(s.handshake.xdomain).to.be.a('boolean');
  111. expect(s.handshake.secure).to.be.a('boolean');
  112. expect(s.handshake.issued).to.be.a('number');
  113. expect(s.handshake.url).to.be.a('string');
  114. // Query set and has some right properties
  115. expect(s.handshake.query).to.be.an('object');
  116. expect(s.handshake.query.EIO).to.not.be(undefined);
  117. expect(s.handshake.query.transport).to.not.be(undefined);
  118. expect(s.handshake.query.t).to.not.be(undefined);
  119. done();
  120. });
  121. var socket = client(httpSrv);
  122. });
  123. });
  124. describe('server attachment', function(){
  125. describe('http.Server', function(){
  126. var clientVersion = require('socket.io-client/package').version;
  127. it('should serve static files', function(done){
  128. var srv = http();
  129. io(srv);
  130. request(srv)
  131. .get('/socket.io/socket.io.js')
  132. .buffer(true)
  133. .end(function(err, res){
  134. if (err) return done(err);
  135. var ctype = res.headers['content-type'];
  136. expect(ctype).to.be('application/javascript');
  137. expect(res.headers.etag).to.be('"' + clientVersion + '"');
  138. expect(res.text).to.match(/engine\.io/);
  139. expect(res.status).to.be(200);
  140. done();
  141. });
  142. });
  143. it('should handle 304', function(done){
  144. var srv = http();
  145. io(srv);
  146. request(srv)
  147. .get('/socket.io/socket.io.js')
  148. .set('If-None-Match', '"' + clientVersion + '"')
  149. .end(function(err, res){
  150. if (err) return done(err);
  151. expect(res.statusCode).to.be(304);
  152. done();
  153. });
  154. });
  155. it('should not serve static files', function(done){
  156. var srv = http();
  157. io(srv, { serveClient: false });
  158. request(srv)
  159. .get('/socket.io/socket.io.js')
  160. .expect(400, done);
  161. });
  162. it('should work with #attach', function(done){
  163. var srv = http(function(req, res){
  164. res.writeHead(404);
  165. res.end();
  166. });
  167. var sockets = io();
  168. sockets.attach(srv);
  169. request(srv)
  170. .get('/socket.io/socket.io.js')
  171. .end(function(err, res){
  172. if (err) return done(err);
  173. expect(res.status).to.be(200);
  174. done();
  175. });
  176. });
  177. });
  178. describe('port', function(done){
  179. it('should be bound', function(done){
  180. var sockets = io(54010);
  181. request('http://localhost:54010')
  182. .get('/socket.io/socket.io.js')
  183. .expect(200, done);
  184. });
  185. it('should be bound as a string', function(done) {
  186. var sockets = io('54020');
  187. request('http://localhost:54020')
  188. .get('/socket.io/socket.io.js')
  189. .expect(200, done);
  190. });
  191. it('with listen', function(done){
  192. var sockets = io().listen(54011);
  193. request('http://localhost:54011')
  194. .get('/socket.io/socket.io.js')
  195. .expect(200, done);
  196. });
  197. it('as a string', function(done){
  198. var sockets = io().listen('54012');
  199. request('http://localhost:54012')
  200. .get('/socket.io/socket.io.js')
  201. .expect(200, done);
  202. });
  203. });
  204. });
  205. describe('handshake', function(){
  206. var request = require('superagent');
  207. it('should disallow request when origin defined and none specified', function(done) {
  208. var sockets = io({ origins: 'http://foo.example:*' }).listen('54013');
  209. request.get('http://localhost:54013/socket.io/default/')
  210. .query({ transport: 'polling' })
  211. .end(function (err, res) {
  212. expect(res.status).to.be(403);
  213. done();
  214. });
  215. });
  216. it('should disallow request when origin defined and a different one specified', function(done) {
  217. var sockets = io({ origins: 'http://foo.example:*' }).listen('54014');
  218. request.get('http://localhost:54014/socket.io/default/')
  219. .query({ transport: 'polling' })
  220. .set('origin', 'http://herp.derp')
  221. .end(function (err, res) {
  222. expect(res.status).to.be(403);
  223. done();
  224. });
  225. });
  226. it('should allow request when origin defined an the same is specified', function(done) {
  227. var sockets = io({ origins: 'http://foo.example:*' }).listen('54015');
  228. request.get('http://localhost:54015/socket.io/default/')
  229. .set('origin', 'http://foo.example')
  230. .query({ transport: 'polling' })
  231. .end(function (err, res) {
  232. expect(res.status).to.be(200);
  233. done();
  234. });
  235. });
  236. it('should allow request when origin defined as function and same is supplied', function(done) {
  237. var sockets = io({ origins: function(origin,callback){
  238. if (origin == 'http://foo.example') {
  239. return callback(null, true);
  240. }
  241. return callback(null, false);
  242. } }).listen('54016');
  243. request.get('http://localhost:54016/socket.io/default/')
  244. .set('origin', 'http://foo.example')
  245. .query({ transport: 'polling' })
  246. .end(function (err, res) {
  247. expect(res.status).to.be(200);
  248. done();
  249. });
  250. });
  251. it('should allow request when origin defined as function and different is supplied', function(done) {
  252. var sockets = io({ origins: function(origin,callback){
  253. if (origin == 'http://foo.example') {
  254. return callback(null, true);
  255. }
  256. return callback(null, false);
  257. } }).listen('54017');
  258. request.get('http://localhost:54017/socket.io/default/')
  259. .set('origin', 'http://herp.derp')
  260. .query({ transport: 'polling' })
  261. .end(function (err, res) {
  262. expect(res.status).to.be(403);
  263. done();
  264. });
  265. });
  266. it('should allow request when origin defined as function and no origin is supplied', function(done) {
  267. var sockets = io({ origins: function(origin,callback){
  268. if (origin == '*') {
  269. return callback(null, true);
  270. }
  271. return callback(null, false);
  272. } }).listen('54021');
  273. request.get('http://localhost:54021/socket.io/default/')
  274. .query({ transport: 'polling' })
  275. .end(function (err, res) {
  276. expect(res.status).to.be(200);
  277. done();
  278. });
  279. });
  280. it('should default to port 443 when protocol is https', function(done) {
  281. var sockets = io({ origins: 'https://foo.example:443' }).listen('54036');
  282. request.get('http://localhost:54036/socket.io/default/')
  283. .set('origin', 'https://foo.example')
  284. .query({ transport: 'polling' })
  285. .end(function (err, res) {
  286. expect(res.status).to.be(200);
  287. done();
  288. });
  289. });
  290. it('should allow request if custom function in opts.allowRequest returns true', function(done){
  291. var sockets = io(http().listen(54022), { allowRequest: function (req, callback) {
  292. return callback(null, true);
  293. }, origins: 'http://foo.example:*' });
  294. request.get('http://localhost:54022/socket.io/default/')
  295. .query({ transport: 'polling' })
  296. .end(function (err, res) {
  297. expect(res.status).to.be(200);
  298. done();
  299. });
  300. });
  301. it('should disallow request if custom function in opts.allowRequest returns false', function(done){
  302. var sockets = io(http().listen(54023), { allowRequest: function (req, callback) {
  303. return callback(null, false);
  304. } });
  305. request.get('http://localhost:54023/socket.io/default/')
  306. .set('origin', 'http://foo.example')
  307. .query({ transport: 'polling' })
  308. .end(function (err, res) {
  309. expect(res.status).to.be(403);
  310. done();
  311. });
  312. });
  313. it('should allow request when using an array of origins', function(done) {
  314. io({ origins: [ 'http://foo.example:54024' ] }).listen('54024');
  315. request.get('http://localhost:54024/socket.io/default/')
  316. .set('origin', 'http://foo.example:54024')
  317. .query({ transport: 'polling' })
  318. .end(function (err, res) {
  319. expect(res.status).to.be(200);
  320. done();
  321. });
  322. });
  323. });
  324. describe('close', function(){
  325. it('should be able to close sio sending a srv', function(){
  326. var PORT = 54018;
  327. var srv = http().listen(PORT);
  328. var sio = io(srv);
  329. var net = require('net');
  330. var server = net.createServer();
  331. var clientSocket = client(srv, { reconnection: false });
  332. clientSocket.on('disconnect', function init() {
  333. expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(0);
  334. server.listen(PORT);
  335. });
  336. clientSocket.on('connect', function init() {
  337. expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(1);
  338. sio.close();
  339. });
  340. server.once('listening', function() {
  341. // PORT should be free
  342. server.close(function(error){
  343. expect(error).to.be(undefined);
  344. });
  345. });
  346. });
  347. it('should be able to close sio sending a port', function(){
  348. var PORT = 54019;
  349. var sio = io(PORT);
  350. var net = require('net');
  351. var server = net.createServer();
  352. var clientSocket = ioc('ws://0.0.0.0:' + PORT, { reconnection: false });
  353. clientSocket.on('disconnect', function init() {
  354. expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(0);
  355. server.listen(PORT);
  356. });
  357. clientSocket.on('connect', function init() {
  358. expect(Object.keys(sio.nsps['/'].sockets).length).to.equal(1);
  359. sio.close();
  360. });
  361. server.once('listening', function() {
  362. // PORT should be free
  363. server.close(function(error){
  364. expect(error).to.be(undefined);
  365. });
  366. });
  367. });
  368. describe('graceful close', function(){
  369. function fixture(filename) {
  370. return '"' + process.execPath + '" "' +
  371. join(__dirname, 'fixtures', filename) + '"';
  372. }
  373. it('should stop socket and timers', function(done){
  374. exec(fixture('server-close.js'), done);
  375. });
  376. });
  377. });
  378. describe('namespaces', function(){
  379. var Socket = require('../lib/socket');
  380. var Namespace = require('../lib/namespace');
  381. it('should be accessible through .sockets', function(){
  382. var sio = io();
  383. expect(sio.sockets).to.be.a(Namespace);
  384. });
  385. it('should be aliased', function(){
  386. var sio = io();
  387. expect(sio.use).to.be.a('function');
  388. expect(sio.to).to.be.a('function');
  389. expect(sio['in']).to.be.a('function');
  390. expect(sio.emit).to.be.a('function');
  391. expect(sio.send).to.be.a('function');
  392. expect(sio.write).to.be.a('function');
  393. expect(sio.clients).to.be.a('function');
  394. expect(sio.compress).to.be.a('function');
  395. expect(sio.json).to.be(sio);
  396. expect(sio.volatile).to.be(sio);
  397. expect(sio.sockets.flags).to.eql({ json: true, volatile: true });
  398. delete sio.sockets.flags;
  399. });
  400. it('should automatically connect', function(done){
  401. var srv = http();
  402. var sio = io(srv);
  403. srv.listen(function(){
  404. var socket = client(srv);
  405. socket.on('connect', function(){
  406. done();
  407. });
  408. });
  409. });
  410. it('should fire a `connection` event', function(done){
  411. var srv = http();
  412. var sio = io(srv);
  413. srv.listen(function(){
  414. var socket = client(srv);
  415. sio.on('connection', function(socket){
  416. expect(socket).to.be.a(Socket);
  417. done();
  418. });
  419. });
  420. });
  421. it('should fire a `connect` event', function(done){
  422. var srv = http();
  423. var sio = io(srv);
  424. srv.listen(function(){
  425. var socket = client(srv);
  426. sio.on('connect', function(socket){
  427. expect(socket).to.be.a(Socket);
  428. done();
  429. });
  430. });
  431. });
  432. it('should work with many sockets', function(done){
  433. var srv = http();
  434. var sio = io(srv);
  435. srv.listen(function(){
  436. sio.of('/chat');
  437. sio.of('/news');
  438. var chat = client(srv, '/chat');
  439. var news = client(srv, '/news');
  440. var total = 2;
  441. chat.on('connect', function(){
  442. --total || done();
  443. });
  444. news.on('connect', function(){
  445. --total || done();
  446. });
  447. });
  448. });
  449. it('should be able to equivalently start with "" or "/" on server', function(done){
  450. var srv = http();
  451. var sio = io(srv);
  452. var total = 2;
  453. sio.of('').on('connection', function(){
  454. --total || done();
  455. });
  456. sio.of('abc').on('connection', function(){
  457. --total || done();
  458. });
  459. var c1 = client(srv, '/');
  460. var c2 = client(srv, '/abc');
  461. });
  462. it('should be equivalent for "" and "/" on client', function(done){
  463. var srv = http();
  464. var sio = io(srv);
  465. sio.of('/').on('connection', function(){
  466. done();
  467. });
  468. var c1 = client(srv, '');
  469. });
  470. it('should work with `of` and many sockets', function(done){
  471. var srv = http();
  472. var sio = io(srv);
  473. srv.listen(function(){
  474. var chat = client(srv, '/chat');
  475. var news = client(srv, '/news');
  476. var total = 2;
  477. sio.of('/news').on('connection', function(socket){
  478. expect(socket).to.be.a(Socket);
  479. --total || done();
  480. });
  481. sio.of('/news').on('connection', function(socket){
  482. expect(socket).to.be.a(Socket);
  483. --total || done();
  484. });
  485. });
  486. });
  487. it('should work with `of` second param', function(done){
  488. var srv = http();
  489. var sio = io(srv);
  490. srv.listen(function(){
  491. var chat = client(srv, '/chat');
  492. var news = client(srv, '/news');
  493. var total = 2;
  494. sio.of('/news', function(socket){
  495. expect(socket).to.be.a(Socket);
  496. --total || done();
  497. });
  498. sio.of('/news', function(socket){
  499. expect(socket).to.be.a(Socket);
  500. --total || done();
  501. });
  502. });
  503. });
  504. it('should disconnect upon transport disconnection', function(done){
  505. var srv = http();
  506. var sio = io(srv);
  507. srv.listen(function(){
  508. var chat = client(srv, '/chat');
  509. var news = client(srv, '/news');
  510. var total = 2;
  511. var totald = 2;
  512. var s;
  513. sio.of('/news', function(socket){
  514. socket.on('disconnect', function(reason){
  515. --totald || done();
  516. });
  517. --total || close();
  518. });
  519. sio.of('/chat', function(socket){
  520. s = socket;
  521. socket.on('disconnect', function(reason){
  522. --totald || done();
  523. });
  524. --total || close();
  525. });
  526. function close(){
  527. s.disconnect(true);
  528. }
  529. });
  530. });
  531. it('should disconnect both default and custom namespace upon disconnect', function(done){
  532. var srv = http();
  533. var sio = io(srv);
  534. srv.listen(function(){
  535. var lolcats = client(srv, '/lolcats');
  536. var total = 2;
  537. var totald = 2;
  538. var s;
  539. sio.of('/', function(socket){
  540. socket.on('disconnect', function(reason){
  541. --totald || done();
  542. });
  543. --total || close();
  544. });
  545. sio.of('/lolcats', function(socket){
  546. s = socket;
  547. socket.on('disconnect', function(reason){
  548. --totald || done();
  549. });
  550. --total || close();
  551. });
  552. function close(){
  553. s.disconnect(true);
  554. }
  555. });
  556. });
  557. it('should not crash while disconnecting socket', function(done){
  558. var srv = http();
  559. var sio = io(srv);
  560. srv.listen(function(){
  561. var socket = client(srv,'/ns');
  562. sio.on('connection', function(socket){
  563. socket.disconnect();
  564. done();
  565. });
  566. });
  567. });
  568. it('should fire a `disconnecting` event just before leaving all rooms', function(done){
  569. var srv = http();
  570. var sio = io(srv);
  571. srv.listen(function(){
  572. var socket = client(srv);
  573. sio.on('connection', function(s){
  574. s.join('a', function(){
  575. s.disconnect();
  576. });
  577. var total = 2;
  578. s.on('disconnecting', function(reason){
  579. expect(Object.keys(s.rooms)).to.eql([s.id, 'a']);
  580. total--;
  581. });
  582. s.on('disconnect', function(reason){
  583. expect(Object.keys(s.rooms)).to.eql([]);
  584. --total || done();
  585. });
  586. });
  587. });
  588. });
  589. it('should return error connecting to non-existent namespace', function(done){
  590. var srv = http();
  591. var sio = io(srv);
  592. srv.listen(function(){
  593. var socket = client(srv,'/doesnotexist');
  594. socket.on('error', function(err) {
  595. expect(err).to.be('Invalid namespace');
  596. done();
  597. });
  598. });
  599. });
  600. it('should not reuse same-namespace connections', function(done){
  601. var srv = http();
  602. var sio = io(srv);
  603. var connections = 0;
  604. srv.listen(function() {
  605. var clientSocket1 = client(srv);
  606. var clientSocket2 = client(srv);
  607. sio.on('connection', function() {
  608. connections++;
  609. if (connections === 2) {
  610. done();
  611. }
  612. });
  613. });
  614. });
  615. it('should find all clients in a namespace', function(done){
  616. var srv = http();
  617. var sio = io(srv);
  618. var chatSids = [];
  619. var otherSid = null;
  620. srv.listen(function(){
  621. var c1 = client(srv, '/chat');
  622. var c2 = client(srv, '/chat', {forceNew: true});
  623. var c3 = client(srv, '/other', {forceNew: true});
  624. var total = 3;
  625. sio.of('/chat').on('connection', function(socket){
  626. chatSids.push(socket.id);
  627. --total || getClients();
  628. });
  629. sio.of('/other').on('connection', function(socket){
  630. otherSid = socket.id;
  631. --total || getClients();
  632. });
  633. });
  634. function getClients() {
  635. sio.of('/chat').clients(function(error, sids) {
  636. expect(error).to.not.be.ok();
  637. expect(sids).to.contain(chatSids[0]);
  638. expect(sids).to.contain(chatSids[1]);
  639. expect(sids).to.not.contain(otherSid);
  640. done();
  641. });
  642. }
  643. });
  644. it('should find all clients in a namespace room', function(done){
  645. var srv = http();
  646. var sio = io(srv);
  647. var chatFooSid = null;
  648. var chatBarSid = null;
  649. var otherSid = null;
  650. srv.listen(function(){
  651. var c1 = client(srv, '/chat');
  652. var c2 = client(srv, '/chat', {forceNew: true});
  653. var c3 = client(srv, '/other', {forceNew: true});
  654. var chatIndex = 0;
  655. var total = 3;
  656. sio.of('/chat').on('connection', function(socket){
  657. if (chatIndex++) {
  658. socket.join('foo', function() {
  659. chatFooSid = socket.id;
  660. --total || getClients();
  661. });
  662. } else {
  663. socket.join('bar', function() {
  664. chatBarSid = socket.id;
  665. --total || getClients();
  666. });
  667. }
  668. });
  669. sio.of('/other').on('connection', function(socket){
  670. socket.join('foo', function() {
  671. otherSid = socket.id;
  672. --total || getClients();
  673. });
  674. });
  675. });
  676. function getClients() {
  677. sio.of('/chat').in('foo').clients(function(error, sids) {
  678. expect(error).to.not.be.ok();
  679. expect(sids).to.contain(chatFooSid);
  680. expect(sids).to.not.contain(chatBarSid);
  681. expect(sids).to.not.contain(otherSid);
  682. done();
  683. });
  684. }
  685. });
  686. it('should find all clients across namespace rooms', function(done){
  687. var srv = http();
  688. var sio = io(srv);
  689. var chatFooSid = null;
  690. var chatBarSid = null;
  691. var otherSid = null;
  692. srv.listen(function(){
  693. var c1 = client(srv, '/chat');
  694. var c2 = client(srv, '/chat', {forceNew: true});
  695. var c3 = client(srv, '/other', {forceNew: true});
  696. var chatIndex = 0;
  697. var total = 3;
  698. sio.of('/chat').on('connection', function(socket){
  699. if (chatIndex++) {
  700. socket.join('foo', function() {
  701. chatFooSid = socket.id;
  702. --total || getClients();
  703. });
  704. } else {
  705. socket.join('bar', function() {
  706. chatBarSid = socket.id;
  707. --total || getClients();
  708. });
  709. }
  710. });
  711. sio.of('/other').on('connection', function(socket){
  712. socket.join('foo', function() {
  713. otherSid = socket.id;
  714. --total || getClients();
  715. });
  716. });
  717. });
  718. function getClients() {
  719. sio.of('/chat').clients(function(error, sids) {
  720. expect(error).to.not.be.ok();
  721. expect(sids).to.contain(chatFooSid);
  722. expect(sids).to.contain(chatBarSid);
  723. expect(sids).to.not.contain(otherSid);
  724. done();
  725. });
  726. }
  727. });
  728. it('should not emit volatile event after regular event', function(done) {
  729. var srv = http();
  730. var sio = io(srv);
  731. var counter = 0;
  732. srv.listen(function(){
  733. sio.of('/chat').on('connection', function(s){
  734. // Wait to make sure there are no packets being sent for opening the connection
  735. setTimeout(function() {
  736. sio.of('/chat').emit('ev', 'data');
  737. sio.of('/chat').volatile.emit('ev', 'data');
  738. }, 50);
  739. });
  740. var socket = client(srv, '/chat');
  741. socket.on('ev', function() {
  742. counter++;
  743. });
  744. });
  745. setTimeout(function() {
  746. expect(counter).to.be(1);
  747. done();
  748. }, 500);
  749. });
  750. it('should emit volatile event', function(done) {
  751. var srv = http();
  752. var sio = io(srv);
  753. var counter = 0;
  754. srv.listen(function(){
  755. sio.of('/chat').on('connection', function(s){
  756. // Wait to make sure there are no packets being sent for opening the connection
  757. setTimeout(function() {
  758. sio.of('/chat').volatile.emit('ev', 'data');
  759. }, 100);
  760. });
  761. var socket = client(srv, '/chat');
  762. socket.on('ev', function() {
  763. counter++;
  764. });
  765. });
  766. setTimeout(function() {
  767. expect(counter).to.be(1);
  768. done();
  769. }, 500);
  770. });
  771. it('should enable compression by default', function(done){
  772. var srv = http();
  773. var sio = io(srv);
  774. srv.listen(function(){
  775. var socket = client(srv, '/chat');
  776. sio.of('/chat').on('connection', function(s){
  777. s.conn.once('packetCreate', function(packet) {
  778. expect(packet.options.compress).to.be(true);
  779. done();
  780. });
  781. sio.of('/chat').emit('woot', 'hi');
  782. });
  783. });
  784. });
  785. it('should disable compression', function(done){
  786. var srv = http();
  787. var sio = io(srv);
  788. srv.listen(function(){
  789. var socket = client(srv, '/chat');
  790. sio.of('/chat').on('connection', function(s){
  791. s.conn.once('packetCreate', function(packet) {
  792. expect(packet.options.compress).to.be(false);
  793. done();
  794. });
  795. sio.of('/chat').compress(false).emit('woot', 'hi');
  796. });
  797. });
  798. });
  799. describe('dynamic namespaces', function () {
  800. it('should allow connections to dynamic namespaces with a regex', function(done){
  801. const srv = http();
  802. const sio = io(srv);
  803. let count = 0;
  804. srv.listen(function(){
  805. const socket = client(srv, '/dynamic-101');
  806. let dynamicNsp = sio.of(/^\/dynamic-\d+$/).on('connect', (socket) => {
  807. expect(socket.nsp.name).to.be('/dynamic-101');
  808. dynamicNsp.emit('hello', 1, '2', { 3: '4'});
  809. if (++count === 4) done();
  810. }).use((socket, next) => {
  811. next();
  812. if (++count === 4) done();
  813. });
  814. socket.on('error', function(err) {
  815. expect().fail();
  816. });
  817. socket.on('connect', () => {
  818. if (++count === 4) done();
  819. });
  820. socket.on('hello', (a, b, c) => {
  821. expect(a).to.eql(1);
  822. expect(b).to.eql('2');
  823. expect(c).to.eql({ 3: '4' });
  824. if (++count === 4) done();
  825. });
  826. });
  827. });
  828. it('should allow connections to dynamic namespaces with a function', function(done){
  829. const srv = http();
  830. const sio = io(srv);
  831. srv.listen(function(){
  832. const socket = client(srv, '/dynamic-101');
  833. sio.of((name, query, next) => next(null, '/dynamic-101' === name));
  834. socket.on('connect', done);
  835. });
  836. });
  837. it('should disallow connections when no dynamic namespace matches', function(done){
  838. const srv = http();
  839. const sio = io(srv);
  840. srv.listen(function(){
  841. const socket = client(srv, '/abc');
  842. sio.of(/^\/dynamic-\d+$/);
  843. sio.of((name, query, next) => next(null, '/dynamic-101' === name));
  844. socket.on('error', (err) => {
  845. expect(err).to.be('Invalid namespace');
  846. done();
  847. });
  848. });
  849. });
  850. });
  851. });
  852. describe('socket', function(){
  853. it('should not fire events more than once after manually reconnecting', function(done) {
  854. var srv = http();
  855. var sio = io(srv);
  856. srv.listen(function(){
  857. var clientSocket = client(srv, { reconnection: false });
  858. clientSocket.on('connect', function init() {
  859. clientSocket.removeListener('connect', init);
  860. clientSocket.io.engine.close();
  861. clientSocket.connect();
  862. clientSocket.on('connect', function() {
  863. done();
  864. });
  865. });
  866. });
  867. });
  868. it('should not fire reconnect_failed event more than once when server closed', function(done) {
  869. var srv = http();
  870. var sio = io(srv);
  871. srv.listen(function(){
  872. var clientSocket = client(srv, { reconnectionAttempts: 3, reconnectionDelay: 10 });
  873. clientSocket.on('connect', function() {
  874. srv.close();
  875. });
  876. clientSocket.on('reconnect_failed', function() {
  877. done();
  878. });
  879. });
  880. });
  881. it('should receive events', function(done){
  882. var srv = http();
  883. var sio = io(srv);
  884. srv.listen(function(){
  885. var socket = client(srv);
  886. sio.on('connection', function(s){
  887. s.on('random', function(a, b, c){
  888. expect(a).to.be(1);
  889. expect(b).to.be('2');
  890. expect(c).to.eql([3]);
  891. done();
  892. });
  893. socket.emit('random', 1, '2', [3]);
  894. });
  895. });
  896. });
  897. it('should receive message events through `send`', function(done){
  898. var srv = http();
  899. var sio = io(srv);
  900. srv.listen(function(){
  901. var socket = client(srv);
  902. sio.on('connection', function(s){
  903. s.on('message', function(a){
  904. expect(a).to.be(1337);
  905. done();
  906. });
  907. socket.send(1337);
  908. });
  909. });
  910. });
  911. it('should error with null messages', function(done){
  912. var srv = http();
  913. var sio = io(srv);
  914. srv.listen(function(){
  915. var socket = client(srv);
  916. sio.on('connection', function(s){
  917. s.on('message', function(a){
  918. expect(a).to.be(null);
  919. done();
  920. });
  921. socket.send(null);
  922. });
  923. });
  924. });
  925. it('should handle transport null messages', function(done){
  926. var srv = http();
  927. var sio = io(srv);
  928. srv.listen(function(){
  929. var socket = client(srv, { reconnection: false });
  930. sio.on('connection', function(s){
  931. s.on('error', function(err){
  932. expect(err).to.be.an(Error);
  933. s.on('disconnect', function(reason){
  934. expect(reason).to.be('forced close');
  935. done();
  936. });
  937. });
  938. s.client.ondata(null);
  939. });
  940. });
  941. });
  942. it('should emit events', function(done){
  943. var srv = http();
  944. var sio = io(srv);
  945. srv.listen(function(){
  946. var socket = client(srv);
  947. socket.on('woot', function(a){
  948. expect(a).to.be('tobi');
  949. done();
  950. });
  951. sio.on('connection', function(s){
  952. s.emit('woot', 'tobi');
  953. });
  954. });
  955. });
  956. it('should emit events with utf8 multibyte character', function(done) {
  957. var srv = http();
  958. var sio = io(srv);
  959. srv.listen(function(){
  960. var socket = client(srv);
  961. var i = 0;
  962. socket.on('hoot', function(a){
  963. expect(a).to.be('utf8 — string');
  964. i++;
  965. if (3 == i) {
  966. done();
  967. }
  968. });
  969. sio.on('connection', function(s){
  970. s.emit('hoot', 'utf8 — string');
  971. s.emit('hoot', 'utf8 — string');
  972. s.emit('hoot', 'utf8 — string');
  973. });
  974. });
  975. });
  976. it('should emit events with binary data', function(done){
  977. var srv = http();
  978. var sio = io(srv);
  979. srv.listen(function(){
  980. var socket = client(srv);
  981. var imageData;
  982. socket.on('doge', function(a){
  983. expect(Buffer.isBuffer(a)).to.be(true);
  984. expect(imageData.length).to.equal(a.length);
  985. expect(imageData[0]).to.equal(a[0]);
  986. expect(imageData[imageData.length - 1]).to.equal(a[a.length - 1]);
  987. done();
  988. });
  989. sio.on('connection', function(s){
  990. fs.readFile(join(__dirname, 'support', 'doge.jpg'), function(err, data){
  991. if (err) return done(err);
  992. imageData = data;
  993. s.emit('doge', data);
  994. });
  995. });
  996. });
  997. });
  998. it('should emit events with several types of data (including binary)', function(done){
  999. var srv = http();
  1000. var sio = io(srv);
  1001. srv.listen(function(){
  1002. var socket = client(srv);
  1003. socket.on('multiple', function(a, b, c, d, e, f){
  1004. expect(a).to.be(1);
  1005. expect(Buffer.isBuffer(b)).to.be(true);
  1006. expect(c).to.be('3');
  1007. expect(d).to.eql([4]);
  1008. expect(Buffer.isBuffer(e)).to.be(true);
  1009. expect(Buffer.isBuffer(f[0])).to.be(true);
  1010. expect(f[1]).to.be('swag');
  1011. expect(Buffer.isBuffer(f[2])).to.be(true);
  1012. done();
  1013. });
  1014. sio.on('connection', function(s){
  1015. fs.readFile(join(__dirname, 'support', 'doge.jpg'), function(err, data){
  1016. if (err) return done(err);
  1017. var buf = Buffer.from('asdfasdf', 'utf8');
  1018. s.emit('multiple', 1, data, '3', [4], buf, [data, 'swag', buf]);
  1019. });
  1020. });
  1021. });
  1022. });
  1023. it('should receive events with binary data', function(done){
  1024. var srv = http();
  1025. var sio = io(srv);
  1026. srv.listen(function(){
  1027. var socket = client(srv);
  1028. sio.on('connection', function(s){
  1029. s.on('buff', function(a){
  1030. expect(Buffer.isBuffer(a)).to.be(true);
  1031. done();
  1032. });
  1033. var buf = Buffer.from('abcdefg', 'utf8');
  1034. socket.emit('buff', buf);
  1035. });
  1036. });
  1037. });
  1038. it('should receive events with several types of data (including binary)', function(done){
  1039. var srv = http();
  1040. var sio = io(srv);
  1041. srv.listen(function(){
  1042. var socket = client(srv);
  1043. sio.on('connection', function(s){
  1044. s.on('multiple', function(a, b, c, d, e, f){
  1045. expect(a).to.be(1);
  1046. expect(Buffer.isBuffer(b)).to.be(true);
  1047. expect(c).to.be('3');
  1048. expect(d).to.eql([4]);
  1049. expect(Buffer.isBuffer(e)).to.be(true);
  1050. expect(Buffer.isBuffer(f[0])).to.be(true);
  1051. expect(f[1]).to.be('swag');
  1052. expect(Buffer.isBuffer(f[2])).to.be(true);
  1053. done();
  1054. });
  1055. fs.readFile(join(__dirname, 'support', 'doge.jpg'), function(err, data){
  1056. if (err) return done(err);
  1057. var buf = Buffer.from('asdfasdf', 'utf8');
  1058. socket.emit('multiple', 1, data, '3', [4], buf, [data, 'swag', buf]);
  1059. });
  1060. });
  1061. });
  1062. });
  1063. it('should not emit volatile event after regular event (polling)', function(done) {
  1064. var srv = http();
  1065. var sio = io(srv, { transports: ['polling'] });
  1066. var counter = 0;
  1067. srv.listen(function(){
  1068. sio.on('connection', function(s){
  1069. s.emit('ev', 'data');
  1070. s.volatile.emit('ev', 'data');
  1071. });
  1072. var socket = client(srv, { transports: ['polling'] });
  1073. socket.on('ev', function() {
  1074. counter++;
  1075. });
  1076. });
  1077. setTimeout(function() {
  1078. expect(counter).to.be(1);
  1079. done();
  1080. }, 200);
  1081. });
  1082. it('should not emit volatile event after regular event (ws)', function(done) {
  1083. var srv = http();
  1084. var sio = io(srv, { transports: ['websocket'] });
  1085. var counter = 0;
  1086. srv.listen(function(){
  1087. sio.on('connection', function(s){
  1088. s.emit('ev', 'data');
  1089. s.volatile.emit('ev', 'data');
  1090. });
  1091. var socket = client(srv, { transports: ['websocket'] });
  1092. socket.on('ev', function() {
  1093. counter++;
  1094. });
  1095. });
  1096. setTimeout(function() {
  1097. expect(counter).to.be(1);
  1098. done();
  1099. }, 200);
  1100. });
  1101. it('should emit volatile event (polling)', function(done) {
  1102. var srv = http();
  1103. var sio = io(srv, { transports: ['polling'] });
  1104. var counter = 0;
  1105. srv.listen(function(){
  1106. sio.on('connection', function(s){
  1107. // Wait to make sure there are no packets being sent for opening the connection
  1108. setTimeout(function() {
  1109. s.volatile.emit('ev', 'data');
  1110. }, 100);
  1111. });
  1112. var socket = client(srv, { transports: ['polling'] });
  1113. socket.on('ev', function() {
  1114. counter++;
  1115. });
  1116. });
  1117. setTimeout(function() {
  1118. expect(counter).to.be(1);
  1119. done();
  1120. }, 500);
  1121. });
  1122. it('should emit volatile event (ws)', function(done) {
  1123. var srv = http();
  1124. var sio = io(srv, { transports: ['websocket'] });
  1125. var counter = 0;
  1126. srv.listen(function(){
  1127. sio.on('connection', function(s){
  1128. // Wait to make sure there are no packets being sent for opening the connection
  1129. setTimeout(function() {
  1130. s.volatile.emit('ev', 'data');
  1131. }, 20);
  1132. });
  1133. var socket = client(srv, { transports: ['websocket'] });
  1134. socket.on('ev', function() {
  1135. counter++;
  1136. });
  1137. });
  1138. setTimeout(function() {
  1139. expect(counter).to.be(1);
  1140. done();
  1141. }, 200);
  1142. });
  1143. it('should emit only one consecutive volatile event (polling)', function(done) {
  1144. var srv = http();
  1145. var sio = io(srv, { transports: ['polling'] });
  1146. var counter = 0;
  1147. srv.listen(function(){
  1148. sio.on('connection', function(s){
  1149. // Wait to make sure there are no packets being sent for opening the connection
  1150. setTimeout(function() {
  1151. s.volatile.emit('ev', 'data');
  1152. s.volatile.emit('ev', 'data');
  1153. }, 100);
  1154. });
  1155. var socket = client(srv, { transports: ['polling'] });
  1156. socket.on('ev', function() {
  1157. counter++;
  1158. });
  1159. });
  1160. setTimeout(function() {
  1161. expect(counter).to.be(1);
  1162. done();
  1163. }, 500);
  1164. });
  1165. it('should emit only one consecutive volatile event (ws)', function(done) {
  1166. var srv = http();
  1167. var sio = io(srv, { transports: ['websocket'] });
  1168. var counter = 0;
  1169. srv.listen(function(){
  1170. sio.on('connection', function(s){
  1171. // Wait to make sure there are no packets being sent for opening the connection
  1172. setTimeout(function() {
  1173. s.volatile.emit('ev', 'data');
  1174. s.volatile.emit('ev', 'data');
  1175. }, 20);
  1176. });
  1177. var socket = client(srv, { transports: ['websocket'] });
  1178. socket.on('ev', function() {
  1179. counter++;
  1180. });
  1181. });
  1182. setTimeout(function() {
  1183. expect(counter).to.be(1);
  1184. done();
  1185. }, 200);
  1186. });
  1187. it('should emit regular events after trying a failed volatile event (polling)', function(done) {
  1188. var srv = http();
  1189. var sio = io(srv, { transports: ['polling'] });
  1190. var counter = 0;
  1191. srv.listen(function(){
  1192. sio.on('connection', function(s){
  1193. // Wait to make sure there are no packets being sent for opening the connection
  1194. setTimeout(function() {
  1195. s.emit('ev', 'data');
  1196. s.volatile.emit('ev', 'data');
  1197. s.emit('ev', 'data');
  1198. }, 20);
  1199. });
  1200. var socket = client(srv, { transports: ['polling'] });
  1201. socket.on('ev', function() {
  1202. counter++;
  1203. });
  1204. });
  1205. setTimeout(function() {
  1206. expect(counter).to.be(2);
  1207. done();
  1208. }, 200);
  1209. });
  1210. it('should emit regular events after trying a failed volatile event (ws)', function(done) {
  1211. var srv = http();
  1212. var sio = io(srv, { transports: ['websocket'] });
  1213. var counter = 0;
  1214. srv.listen(function(){
  1215. sio.on('connection', function(s){
  1216. // Wait to make sure there are no packets being sent for opening the connection
  1217. setTimeout(function() {
  1218. s.emit('ev', 'data');
  1219. s.volatile.emit('ev', 'data');
  1220. s.emit('ev', 'data');
  1221. }, 20);
  1222. });
  1223. var socket = client(srv, { transports: ['websocket'] });
  1224. socket.on('ev', function() {
  1225. counter++;
  1226. });
  1227. });
  1228. setTimeout(function() {
  1229. expect(counter).to.be(2);
  1230. done();
  1231. }, 200);
  1232. });
  1233. it('should emit message events through `send`', function(done){
  1234. var srv = http();
  1235. var sio = io(srv);
  1236. srv.listen(function(){
  1237. var socket = client(srv);
  1238. socket.on('message', function(a){
  1239. expect(a).to.be('a');
  1240. done();
  1241. });
  1242. sio.on('connection', function(s){
  1243. s.send('a');
  1244. });
  1245. });
  1246. });
  1247. it('should receive event with callbacks', function(done){
  1248. var srv = http();
  1249. var sio = io(srv);
  1250. srv.listen(function(){
  1251. var socket = client(srv);
  1252. sio.on('connection', function(s){
  1253. s.on('woot', function(fn){
  1254. fn(1, 2);
  1255. });
  1256. socket.emit('woot', function(a, b){
  1257. expect(a).to.be(1);
  1258. expect(b).to.be(2);
  1259. done();
  1260. });
  1261. });
  1262. });
  1263. });
  1264. it('should receive all events emitted from namespaced client immediately and in order', function(done) {
  1265. var srv = http();
  1266. var sio = io(srv);
  1267. var total = 0;
  1268. srv.listen(function(){
  1269. sio.of('/chat', function(s){
  1270. s.on('hi', function(letter){
  1271. total++;
  1272. if (total == 2 && letter == 'b') {
  1273. done();
  1274. } else if (total == 1 && letter != 'a') {
  1275. throw new Error('events out of order');
  1276. }
  1277. });
  1278. });
  1279. var chat = client(srv, '/chat');
  1280. chat.emit('hi', 'a');
  1281. setTimeout(function() {
  1282. chat.emit('hi', 'b');
  1283. }, 50);
  1284. });
  1285. });
  1286. it('should emit events with callbacks', function(done){
  1287. var srv = http();
  1288. var sio = io(srv);
  1289. srv.listen(function(){
  1290. var socket = client(srv);
  1291. sio.on('connection', function(s){
  1292. socket.on('hi', function(fn){
  1293. fn();
  1294. });
  1295. s.emit('hi', function(){
  1296. done();
  1297. });
  1298. });
  1299. });
  1300. });
  1301. it('should receive events with args and callback', function(done){
  1302. var srv = http();
  1303. var sio = io(srv);
  1304. srv.listen(function(){
  1305. var socket = client(srv);
  1306. sio.on('connection', function(s){
  1307. s.on('woot', function(a, b, fn){
  1308. expect(a).to.be(1);
  1309. expect(b).to.be(2);
  1310. fn();
  1311. });
  1312. socket.emit('woot', 1, 2, function(){
  1313. done();
  1314. });
  1315. });
  1316. });
  1317. });
  1318. it('should emit events with args and callback', function(done){
  1319. var srv = http();
  1320. var sio = io(srv);
  1321. srv.listen(function(){
  1322. var socket = client(srv);
  1323. sio.on('connection', function(s){
  1324. socket.on('hi', function(a, b, fn){
  1325. expect(a).to.be(1);
  1326. expect(b).to.be(2);
  1327. fn();
  1328. });
  1329. s.emit('hi', 1, 2, function(){
  1330. done();
  1331. });
  1332. });
  1333. });
  1334. });
  1335. it('should receive events with binary args and callbacks', function(done) {
  1336. var srv = http();
  1337. var sio = io(srv);
  1338. srv.listen(function(){
  1339. var socket = client(srv);
  1340. sio.on('connection', function(s){
  1341. s.on('woot', function(buf, fn){
  1342. expect(Buffer.isBuffer(buf)).to.be(true);
  1343. fn(1, 2);
  1344. });
  1345. socket.emit('woot', Buffer.alloc(3), function(a, b){
  1346. expect(a).to.be(1);
  1347. expect(b).to.be(2);
  1348. done();
  1349. });
  1350. });
  1351. });
  1352. });
  1353. it('should emit events with binary args and callback', function(done){
  1354. var srv = http();
  1355. var sio = io(srv);
  1356. srv.listen(function(){
  1357. var socket = client(srv);
  1358. sio.on('connection', function(s){
  1359. socket.on('hi', function(a, fn){
  1360. expect(Buffer.isBuffer(a)).to.be(true);
  1361. fn();
  1362. });
  1363. s.emit('hi', Buffer.alloc(4), function(){
  1364. done();
  1365. });
  1366. });
  1367. });
  1368. });
  1369. it('should emit events and receive binary data in a callback', function(done) {
  1370. var srv = http();
  1371. var sio = io(srv);
  1372. srv.listen(function(){
  1373. var socket = client(srv);
  1374. sio.on('connection', function(s){
  1375. socket.on('hi', function(fn){
  1376. fn(Buffer.alloc(1));
  1377. });
  1378. s.emit('hi', function(a){
  1379. expect(Buffer.isBuffer(a)).to.be(true);
  1380. done();
  1381. });
  1382. });
  1383. });
  1384. });
  1385. it('should receive events and pass binary data in a callback', function(done) {
  1386. var srv = http();
  1387. var sio = io(srv);
  1388. srv.listen(function(){
  1389. var socket = client(srv);
  1390. sio.on('connection', function(s){
  1391. s.on('woot', function(fn){
  1392. fn(Buffer.alloc(2));
  1393. });
  1394. socket.emit('woot', function(a){
  1395. expect(Buffer.isBuffer(a)).to.be(true);
  1396. done();
  1397. });
  1398. });
  1399. });
  1400. });
  1401. it('should have access to the client', function(done){
  1402. var srv = http();
  1403. var sio = io(srv);
  1404. srv.listen(function(){
  1405. var socket = client(srv);
  1406. sio.on('connection', function(s){
  1407. expect(s.client).to.be.an('object');
  1408. done();
  1409. });
  1410. });
  1411. });
  1412. it('should have access to the connection', function(done){
  1413. var srv = http();
  1414. var sio = io(srv);
  1415. srv.listen(function(){
  1416. var socket = client(srv);
  1417. sio.on('connection', function(s){
  1418. expect(s.client.conn).to.be.an('object');
  1419. expect(s.conn).to.be.an('object');
  1420. done();
  1421. });
  1422. });
  1423. });
  1424. it('should have access to the request', function(done){
  1425. var srv = http();
  1426. var sio = io(srv);
  1427. srv.listen(function(){
  1428. var socket = client(srv);
  1429. sio.on('connection', function(s){
  1430. expect(s.client.request.headers).to.be.an('object');
  1431. expect(s.request.headers).to.be.an('object');
  1432. done();
  1433. });
  1434. });
  1435. });
  1436. it('should see query parameters in the request', function(done) {
  1437. var srv = http();
  1438. var sio = io(srv);
  1439. srv.listen(function() {
  1440. var socket = client(srv, {query: {key1: 1, key2: 2}});
  1441. sio.on('connection', function(s) {
  1442. var parsed = require('url').parse(s.request.url);
  1443. var query = require('querystring').parse(parsed.query);
  1444. expect(query.key1).to.be('1');
  1445. expect(query.key2).to.be('2');
  1446. done();
  1447. });
  1448. });
  1449. });
  1450. it('should see query parameters sent from secondary namespace connections in handshake object', function(done){
  1451. var srv = http();
  1452. var sio = io(srv);
  1453. var client1 = client(srv);
  1454. var client2 = client(srv, '/connection2', {query: {key1: 'aa', key2: '&=bb'}});
  1455. sio.on('connection', function(s){
  1456. });
  1457. sio.of('/connection2').on('connection', function(s){
  1458. expect(s.handshake.query.key1).to.be('aa');
  1459. expect(s.handshake.query.key2).to.be('&=bb');
  1460. done();
  1461. });
  1462. });
  1463. it('should handle very large json', function(done){
  1464. this.timeout(30000);
  1465. var srv = http();
  1466. var sio = io(srv, { perMessageDeflate: false });
  1467. var received = 0;
  1468. srv.listen(function(){
  1469. var socket = client(srv);
  1470. socket.on('big', function(a){
  1471. expect(Buffer.isBuffer(a.json)).to.be(false);
  1472. if (++received == 3)
  1473. done();
  1474. else
  1475. socket.emit('big', a);
  1476. });
  1477. sio.on('connection', function(s){
  1478. fs.readFile(join(__dirname, 'fixtures', 'big.json'), function(err, data){
  1479. if (err) return done(err);
  1480. data = JSON.parse(data);
  1481. s.emit('big', {hello: 'friend', json: data});
  1482. });
  1483. s.on('big', function(a){
  1484. s.emit('big', a);
  1485. });
  1486. });
  1487. });
  1488. });
  1489. it('should handle very large binary data', function(done){
  1490. this.timeout(30000);
  1491. var srv = http();
  1492. var sio = io(srv, { perMessageDeflate: false });
  1493. var received = 0;
  1494. srv.listen(function(){
  1495. var socket = client(srv);
  1496. socket.on('big', function(a){
  1497. expect(Buffer.isBuffer(a.image)).to.be(true);
  1498. if (++received == 3)
  1499. done();
  1500. else
  1501. socket.emit('big', a);
  1502. });
  1503. sio.on('connection', function(s){
  1504. fs.readFile(join(__dirname, 'fixtures', 'big.jpg'), function(err, data){
  1505. if (err) return done(err);
  1506. s.emit('big', {hello: 'friend', image: data});
  1507. });
  1508. s.on('big', function(a){
  1509. expect(Buffer.isBuffer(a.image)).to.be(true);
  1510. s.emit('big', a);
  1511. });
  1512. });
  1513. });
  1514. });
  1515. it('should be able to emit after server close and restart', function(done){
  1516. var srv = http();
  1517. var sio = io(srv);
  1518. sio.on('connection', function(socket){
  1519. socket.on('ev', function(data){
  1520. expect(data).to.be('payload');
  1521. done();
  1522. });
  1523. });
  1524. srv.listen(function(){
  1525. var port = srv.address().port;
  1526. var clientSocket = client(srv, { reconnectionAttempts: 10, reconnectionDelay: 100 });
  1527. clientSocket.once('connect', function(){
  1528. srv.close(function(){
  1529. clientSocket.on('reconnect', function(){
  1530. clientSocket.emit('ev', 'payload');
  1531. });
  1532. sio.listen(port);
  1533. });
  1534. });
  1535. });
  1536. });
  1537. it('should enable compression by default', function(done){
  1538. var srv = http();
  1539. var sio = io(srv);
  1540. srv.listen(function(){
  1541. var socket = client(srv, '/chat');
  1542. sio.of('/chat').on('connection', function(s){
  1543. s.conn.once('packetCreate', function(packet) {
  1544. expect(packet.options.compress).to.be(true);
  1545. done();
  1546. });
  1547. sio.of('/chat').emit('woot', 'hi');
  1548. });
  1549. });
  1550. });
  1551. it('should disable compression', function(done){
  1552. var srv = http();
  1553. var sio = io(srv);
  1554. srv.listen(function(){
  1555. var socket = client(srv, '/chat');
  1556. sio.of('/chat').on('connection', function(s){
  1557. s.conn.once('packetCreate', function(packet) {
  1558. expect(packet.options.compress).to.be(false);
  1559. done();
  1560. });
  1561. sio.of('/chat').compress(false).emit('woot', 'hi');
  1562. });
  1563. });
  1564. });
  1565. it('should error with raw binary and warn', function(done){
  1566. var srv = http();
  1567. var sio = io(srv);
  1568. srv.listen(function(){
  1569. var socket = client(srv, { reconnection: false });
  1570. sio.on('connection', function(s){
  1571. s.conn.on('upgrade', function(){
  1572. console.log('\u001b[96mNote: warning expected and normal in test.\u001b[39m');
  1573. socket.io.engine.write('5woooot');
  1574. setTimeout(function(){
  1575. done();
  1576. }, 100);
  1577. });
  1578. });
  1579. });
  1580. });
  1581. it('should not crash when receiving an error packet without handler', function(done){
  1582. var srv = http();
  1583. var sio = io(srv);
  1584. srv.listen(function(){
  1585. var socket = client(srv, { reconnection: false });
  1586. sio.on('connection', function(s){
  1587. s.conn.on('upgrade', function(){
  1588. console.log('\u001b[96mNote: warning expected and normal in test.\u001b[39m');
  1589. socket.io.engine.write('44["handle me please"]');
  1590. setTimeout(function(){
  1591. done();
  1592. }, 100);
  1593. });
  1594. });
  1595. });
  1596. });
  1597. it('should not crash with raw binary', function(done){
  1598. var srv = http();
  1599. var sio = io(srv);
  1600. srv.listen(function(){
  1601. var socket = client(srv, { reconnection: false });
  1602. sio.on('connection', function(s){
  1603. s.once('error', function(err){
  1604. expect(err.message).to.match(/Illegal attachments/);
  1605. done();
  1606. });
  1607. s.conn.on('upgrade', function(){
  1608. socket.io.engine.write('5woooot');
  1609. });
  1610. });
  1611. });
  1612. });
  1613. it('should handle empty binary packet', function(done){
  1614. var srv = http();
  1615. var sio = io(srv);
  1616. srv.listen(function(){
  1617. var socket = client(srv, { reconnection: false });
  1618. sio.on('connection', function(s){
  1619. s.once('error', function(err){
  1620. expect(err.message).to.match(/Illegal attachments/);
  1621. done();
  1622. });
  1623. s.conn.on('upgrade', function(){
  1624. socket.io.engine.write('5');
  1625. });
  1626. });
  1627. });
  1628. });
  1629. it('should not crash when messing with Object prototype (and other globals)', function(done){
  1630. Object.prototype.foo = 'bar';
  1631. global.File = '';
  1632. global.Blob = [];
  1633. var srv = http();
  1634. var sio = io(srv);
  1635. srv.listen(function(){
  1636. var socket = client(srv);
  1637. sio.on('connection', function(s){
  1638. s.disconnect(true);
  1639. sio.close();
  1640. setTimeout(function(){
  1641. done();
  1642. }, 100);
  1643. });
  1644. });
  1645. });
  1646. it('should always trigger the callback (if provided) when joining a room', function(done){
  1647. var srv = http();
  1648. var sio = io(srv);
  1649. srv.listen(function(){
  1650. var socket = client(srv);
  1651. sio.on('connection', function(s){
  1652. s.join('a', function(){
  1653. s.join('a', done);
  1654. });
  1655. });
  1656. });
  1657. });
  1658. });
  1659. describe('messaging many', function(){
  1660. it('emits to a namespace', function(done){
  1661. var srv = http();
  1662. var sio = io(srv);
  1663. var total = 2;
  1664. srv.listen(function(){
  1665. var socket1 = client(srv, { multiplex: false });
  1666. var socket2 = client(srv, { multiplex: false });
  1667. var socket3 = client(srv, '/test');
  1668. socket1.on('a', function(a){
  1669. expect(a).to.be('b');
  1670. --total || done();
  1671. });
  1672. socket2.on('a', function(a){
  1673. expect(a).to.be('b');
  1674. --total || done();
  1675. });
  1676. socket3.on('a', function(){ done(new Error('not')); });
  1677. var sockets = 3;
  1678. sio.on('connection', function(socket){
  1679. --sockets || emit();
  1680. });
  1681. sio.of('/test', function(socket){
  1682. --sockets || emit();
  1683. });
  1684. function emit(){
  1685. sio.emit('a', 'b');
  1686. }
  1687. });
  1688. });
  1689. it('emits binary data to a namespace', function(done){
  1690. var srv = http();
  1691. var sio = io(srv);
  1692. var total = 2;
  1693. srv.listen(function(){
  1694. var socket1 = client(srv, { multiplex: false });
  1695. var socket2 = client(srv, { multiplex: false });
  1696. var socket3 = client(srv, '/test');
  1697. socket1.on('bin', function(a){
  1698. expect(Buffer.isBuffer(a)).to.be(true);
  1699. --total || done();
  1700. });
  1701. socket2.on('bin', function(a){
  1702. expect(Buffer.isBuffer(a)).to.be(true);
  1703. --total || done();
  1704. });
  1705. socket3.on('bin', function(){ done(new Error('not')); });
  1706. var sockets = 3;
  1707. sio.on('connection', function(socket){
  1708. --sockets || emit();
  1709. });
  1710. sio.of('/test', function(socket){
  1711. --sockets || emit();
  1712. });
  1713. function emit(){
  1714. sio.emit('bin', Buffer.alloc(10));
  1715. }
  1716. });
  1717. });
  1718. it('emits to the rest', function(done){
  1719. var srv = http();
  1720. var sio = io(srv);
  1721. var total = 2;
  1722. srv.listen(function(){
  1723. var socket1 = client(srv, { multiplex: false });
  1724. var socket2 = client(srv, { multiplex: false });
  1725. var socket3 = client(srv, '/test');
  1726. socket1.on('a', function(a){
  1727. expect(a).to.be('b');
  1728. socket1.emit('finish');
  1729. });
  1730. socket2.emit('broadcast');
  1731. socket2.on('a', function(){ done(new Error('done')); });
  1732. socket3.on('a', function(){ done(new Error('not')); });
  1733. var sockets = 2;
  1734. sio.on('connection', function(socket){
  1735. socket.on('broadcast', function(){
  1736. socket.broadcast.emit('a', 'b');
  1737. });
  1738. socket.on('finish', function(){
  1739. done();
  1740. });
  1741. });
  1742. });
  1743. });
  1744. it('emits to rooms', function(done){
  1745. var srv = http();
  1746. var sio = io(srv);
  1747. var total = 2;
  1748. srv.listen(function(){
  1749. var socket1 = client(srv, { multiplex: false });
  1750. var socket2 = client(srv, { multiplex: false });
  1751. socket2.on('a', function(){
  1752. done(new Error('not'));
  1753. });
  1754. socket1.on('a', function(){
  1755. done();
  1756. });
  1757. socket1.emit('join', 'woot', function(){
  1758. socket1.emit('emit', 'woot');
  1759. });
  1760. sio.on('connection', function(socket){
  1761. socket.on('join', function(room, fn){
  1762. socket.join(room, fn);
  1763. });
  1764. socket.on('emit', function(room){
  1765. sio.in(room).emit('a');
  1766. });
  1767. });
  1768. });
  1769. });
  1770. it('emits to rooms avoiding dupes', function(done){
  1771. var srv = http();
  1772. var sio = io(srv);
  1773. var total = 2;
  1774. srv.listen(function(){
  1775. var socket1 = client(srv, { multiplex: false });
  1776. var socket2 = client(srv, { multiplex: false });
  1777. socket2.on('a', function(){
  1778. done(new Error('not'));
  1779. });
  1780. socket1.on('a', function(){
  1781. --total || done();
  1782. });
  1783. socket2.on('b', function(){
  1784. --total || done();
  1785. });
  1786. socket1.emit('join', 'woot');
  1787. socket1.emit('join', 'test');
  1788. socket2.emit('join', 'third', function(){
  1789. socket2.emit('emit');
  1790. });
  1791. sio.on('connection', function(socket){
  1792. socket.on('join', function(room, fn){
  1793. socket.join(room, fn);
  1794. });
  1795. socket.on('emit', function(room){
  1796. sio.in('woot').in('test').emit('a');
  1797. sio.in('third').emit('b');
  1798. });
  1799. });
  1800. });
  1801. });
  1802. it('broadcasts to rooms', function(done){
  1803. var srv = http();
  1804. var sio = io(srv);
  1805. var total = 2;
  1806. srv.listen(function(){
  1807. var socket1 = client(srv, { multiplex: false });
  1808. var socket2 = client(srv, { multiplex: false });
  1809. var socket3 = client(srv, { multiplex: false });
  1810. socket1.emit('join', 'woot');
  1811. socket2.emit('join', 'test');
  1812. socket3.emit('join', 'test', function(){
  1813. socket3.emit('broadcast');
  1814. });
  1815. socket1.on('a', function(){
  1816. done(new Error('not'));
  1817. });
  1818. socket2.on('a', function(){
  1819. --total || done();
  1820. });
  1821. socket3.on('a', function(){
  1822. done(new Error('not'));
  1823. });
  1824. socket3.on('b', function(){
  1825. --total || done();
  1826. });
  1827. sio.on('connection', function(socket){
  1828. socket.on('join', function(room, fn){
  1829. socket.join(room, fn);
  1830. });
  1831. socket.on('broadcast', function(){
  1832. socket.broadcast.to('test').emit('a');
  1833. socket.emit('b');
  1834. });
  1835. });
  1836. });
  1837. });
  1838. it('broadcasts binary data to rooms', function(done){
  1839. var srv = http();
  1840. var sio = io(srv);
  1841. var total = 2;
  1842. srv.listen(function(){
  1843. var socket1 = client(srv, { multiplex: false });
  1844. var socket2 = client(srv, { multiplex: false });
  1845. var socket3 = client(srv, { multiplex: false });
  1846. socket1.emit('join', 'woot');
  1847. socket2.emit('join', 'test');
  1848. socket3.emit('join', 'test', function(){
  1849. socket3.emit('broadcast');
  1850. });
  1851. socket1.on('bin', function(data){
  1852. throw new Error('got bin in socket1');
  1853. });
  1854. socket2.on('bin', function(data){
  1855. expect(Buffer.isBuffer(data)).to.be(true);
  1856. --total || done();
  1857. });
  1858. socket2.on('bin2', function(data) {
  1859. throw new Error('socket2 got bin2');
  1860. });
  1861. socket3.on('bin', function(data) {
  1862. throw new Error('socket3 got bin');
  1863. });
  1864. socket3.on('bin2', function(data) {
  1865. expect(Buffer.isBuffer(data)).to.be(true);
  1866. --total || done();
  1867. });
  1868. sio.on('connection', function(socket){
  1869. socket.on('join', function(room, fn){
  1870. socket.join(room, fn);
  1871. });
  1872. socket.on('broadcast', function(){
  1873. socket.broadcast.to('test').emit('bin', Buffer.alloc(5));
  1874. socket.emit('bin2', Buffer.alloc(5));
  1875. });
  1876. });
  1877. });
  1878. });
  1879. it('keeps track of rooms', function(done){
  1880. var srv = http();
  1881. var sio = io(srv);
  1882. srv.listen(function(){
  1883. var socket = client(srv);
  1884. sio.on('connection', function(s){
  1885. s.join('a', function(){
  1886. expect(Object.keys(s.rooms)).to.eql([s.id, 'a']);
  1887. s.join('b', function(){
  1888. expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'b']);
  1889. s.join( 'c', function(){
  1890. expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'b', 'c']);
  1891. s.leave('b', function(){
  1892. expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'c']);
  1893. s.leaveAll();
  1894. expect(Object.keys(s.rooms)).to.eql([]);
  1895. done();
  1896. });
  1897. });
  1898. });
  1899. });
  1900. });
  1901. });
  1902. });
  1903. it('deletes empty rooms', function(done) {
  1904. var srv = http();
  1905. var sio = io(srv);
  1906. srv.listen(function(){
  1907. var socket = client(srv);
  1908. sio.on('connection', function(s){
  1909. s.join('a', function(){
  1910. expect(s.nsp.adapter.rooms).to.have.key('a');
  1911. s.leave('a', function(){
  1912. expect(s.nsp.adapter.rooms).to.not.have.key('a');
  1913. done();
  1914. });
  1915. });
  1916. });
  1917. });
  1918. });
  1919. it('should properly cleanup left rooms', function(done){
  1920. var srv = http();
  1921. var sio = io(srv);
  1922. srv.listen(function(){
  1923. var socket = client(srv);
  1924. sio.on('connection', function(s){
  1925. s.join('a', function(){
  1926. expect(Object.keys(s.rooms)).to.eql([s.id, 'a']);
  1927. s.join('b', function(){
  1928. expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'b']);
  1929. s.leave('unknown', function(){
  1930. expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'b']);
  1931. s.leaveAll();
  1932. expect(Object.keys(s.rooms)).to.eql([]);
  1933. done();
  1934. });
  1935. });
  1936. });
  1937. });
  1938. });
  1939. });
  1940. it('allows to join several rooms at once', function(done) {
  1941. var srv = http();
  1942. var sio = io(srv);
  1943. srv.listen(function(){
  1944. var socket = client(srv);
  1945. sio.on('connection', function(s){
  1946. s.join(['a', 'b', 'c'], function(){
  1947. expect(Object.keys(s.rooms)).to.eql([s.id, 'a', 'b', 'c']);
  1948. done();
  1949. });
  1950. });
  1951. });
  1952. });
  1953. });
  1954. describe('middleware', function(done){
  1955. var Socket = require('../lib/socket');
  1956. it('should call functions', function(done){
  1957. var srv = http();
  1958. var sio = io(srv);
  1959. var run = 0;
  1960. sio.use(function(socket, next){
  1961. expect(socket).to.be.a(Socket);
  1962. run++;
  1963. next();
  1964. });
  1965. sio.use(function(socket, next){
  1966. expect(socket).to.be.a(Socket);
  1967. run++;
  1968. next();
  1969. });
  1970. srv.listen(function(){
  1971. var socket = client(srv);
  1972. socket.on('connect', function(){
  1973. expect(run).to.be(2);
  1974. done();
  1975. });
  1976. });
  1977. });
  1978. it('should pass errors', function(done){
  1979. var srv = http();
  1980. var sio = io(srv);
  1981. var run = 0;
  1982. sio.use(function(socket, next){
  1983. next(new Error('Authentication error'));
  1984. });
  1985. sio.use(function(socket, next){
  1986. done(new Error('nope'));
  1987. });
  1988. srv.listen(function(){
  1989. var socket = client(srv);
  1990. socket.on('connect', function(){
  1991. done(new Error('nope'));
  1992. });
  1993. socket.on('error', function(err){
  1994. expect(err).to.be('Authentication error');
  1995. done();
  1996. });
  1997. });
  1998. });
  1999. it('should pass `data` of error object', function(done){
  2000. var srv = http();
  2001. var sio = io(srv);
  2002. var run = 0;
  2003. sio.use(function(socket, next){
  2004. var err = new Error('Authentication error');
  2005. err.data = { a: 'b', c: 3 };
  2006. next(err);
  2007. });
  2008. srv.listen(function(){
  2009. var socket = client(srv);
  2010. socket.on('connect', function(){
  2011. done(new Error('nope'));
  2012. });
  2013. socket.on('error', function(err){
  2014. expect(err).to.eql({ a: 'b', c: 3 });
  2015. done();
  2016. });
  2017. });
  2018. });
  2019. it('should only call connection after fns', function(done){
  2020. var srv = http();
  2021. var sio = io(srv);
  2022. sio.use(function(socket, next){
  2023. socket.name = 'guillermo';
  2024. next();
  2025. });
  2026. srv.listen(function(){
  2027. var socket = client(srv);
  2028. sio.on('connection', function(socket){
  2029. expect(socket.name).to.be('guillermo');
  2030. done();
  2031. });
  2032. });
  2033. });
  2034. it('should only call connection after (lengthy) fns', function(done){
  2035. var srv = http();
  2036. var sio = io(srv);
  2037. var authenticated = false;
  2038. sio.use(function(socket, next){
  2039. setTimeout(function () {
  2040. authenticated = true;
  2041. next();
  2042. }, 300);
  2043. });
  2044. srv.listen(function(){
  2045. var socket = client(srv);
  2046. socket.on('connect', function(){
  2047. expect(authenticated).to.be(true);
  2048. done();
  2049. });
  2050. });
  2051. });
  2052. it('should be ignored if socket gets closed', function(done){
  2053. var srv = http();
  2054. var sio = io(srv);
  2055. var socket;
  2056. sio.use(function(s, next){
  2057. socket.io.engine.on('open', function(){
  2058. socket.io.engine.close();
  2059. s.client.conn.on('close', function(){
  2060. process.nextTick(next);
  2061. setTimeout(function(){
  2062. done();
  2063. }, 50);
  2064. });
  2065. });
  2066. });
  2067. srv.listen(function(){
  2068. socket = client(srv);
  2069. sio.on('connection', function(socket){
  2070. done(new Error('should not fire'));
  2071. });
  2072. });
  2073. });
  2074. it('should call functions in expected order', function(done){
  2075. var srv = http();
  2076. var sio = io(srv);
  2077. var result = [];
  2078. sio.use(function(socket, next) {
  2079. result.push(1);
  2080. setTimeout(next, 50);
  2081. });
  2082. sio.use(function(socket, next) {
  2083. result.push(2);
  2084. setTimeout(next, 50);
  2085. });
  2086. sio.of('/chat').use(function(socket, next) {
  2087. result.push(3);
  2088. setTimeout(next, 50);
  2089. });
  2090. sio.of('/chat').use(function(socket, next) {
  2091. result.push(4);
  2092. setTimeout(next, 50);
  2093. });
  2094. srv.listen(function() {
  2095. var chat = client(srv, '/chat');
  2096. chat.on('connect', function() {
  2097. expect(result).to.eql([1, 2, 3, 4]);
  2098. done();
  2099. });
  2100. });
  2101. });
  2102. it('should disable the merge of handshake packets', function(done){
  2103. var srv = http();
  2104. var sio = io();
  2105. sio.use(function(socket, next){
  2106. next();
  2107. });
  2108. sio.listen(srv);
  2109. var socket = client(srv);
  2110. socket.on('connect', function(){
  2111. done();
  2112. });
  2113. });
  2114. it('should work with a custom namespace', (done) => {
  2115. var srv = http();
  2116. var sio = io();
  2117. sio.listen(srv);
  2118. sio.of('/chat').use(function(socket, next){
  2119. next();
  2120. });
  2121. var count = 0;
  2122. client(srv, '/').on('connect', () => {
  2123. if (++count === 2) done();
  2124. });
  2125. client(srv, '/chat').on('connect', () => {
  2126. if (++count === 2) done();
  2127. });
  2128. });
  2129. });
  2130. describe('socket middleware', function(done){
  2131. var Socket = require('../lib/socket');
  2132. it('should call functions', function(done){
  2133. var srv = http();
  2134. var sio = io(srv);
  2135. var run = 0;
  2136. srv.listen(function(){
  2137. var socket = client(srv, { multiplex: false });
  2138. socket.emit('join', 'woot');
  2139. sio.on('connection', function(socket){
  2140. socket.use(function(event, next){
  2141. expect(event).to.eql(['join', 'woot']);
  2142. event.unshift('wrap');
  2143. run++;
  2144. next();
  2145. });
  2146. socket.use(function(event, next){
  2147. expect(event).to.eql(['wrap', 'join', 'woot']);
  2148. run++;
  2149. next();
  2150. });
  2151. socket.on('wrap', function(data1, data2){
  2152. expect(data1).to.be('join');
  2153. expect(data2).to.be('woot');
  2154. expect(run).to.be(2);
  2155. done();
  2156. });
  2157. });
  2158. });
  2159. });
  2160. it('should pass errors', function(done){
  2161. var srv = http();
  2162. var sio = io(srv);
  2163. srv.listen(function(){
  2164. var clientSocket = client(srv, { multiplex: false });
  2165. clientSocket.emit('join', 'woot');
  2166. clientSocket.on('error', function(err){
  2167. expect(err).to.be('Authentication error');
  2168. done();
  2169. });
  2170. sio.on('connection', function(socket){
  2171. socket.use(function(event, next){
  2172. next(new Error('Authentication error'));
  2173. });
  2174. socket.use(function(event, next){
  2175. done(new Error('nope'));
  2176. });
  2177. socket.on('join', function(){
  2178. done(new Error('nope'));
  2179. });
  2180. });
  2181. });
  2182. });
  2183. it('should pass `data` of error object', function(done){
  2184. var srv = http();
  2185. var sio = io(srv);
  2186. srv.listen(function(){
  2187. var clientSocket = client(srv, { multiplex: false });
  2188. clientSocket.emit('join', 'woot');
  2189. clientSocket.on('error', function(err){
  2190. expect(err).to.eql({ a: 'b', c: 3 });
  2191. done();
  2192. });
  2193. sio.on('connection', function(socket){
  2194. socket.use(function(event, next){
  2195. var err = new Error('Authentication error');
  2196. err.data = { a: 'b', c: 3 };
  2197. next(err);
  2198. });
  2199. socket.on('join', function(){
  2200. done(new Error('nope'));
  2201. });
  2202. });
  2203. });
  2204. });
  2205. });
  2206. });