PageRenderTime 54ms CodeModel.GetById 13ms RepoModel.GetById 0ms 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
Possible License(s): MIT

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

  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'

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