PageRenderTime 55ms CodeModel.GetById 25ms RepoModel.GetById 1ms app.codeStats 0ms

/test/socket.io.js

https://github.com/eiriklv/socket.io
JavaScript | 1166 lines | 1049 code | 112 blank | 5 comment | 34 complexity | c04d56cabb0b5804558ef02b054478b0 MD5 | raw file
Possible License(s): MIT
  1. var http = require('http').Server;
  2. var io = require('..');
  3. var fs = require('fs');
  4. var join = require('path').join;
  5. var ioc = require('socket.io-client');
  6. var request = require('supertest');
  7. var expect = require('expect.js');
  8. // creates a socket.io client for the given server
  9. function client(srv, nsp, opts){
  10. if ('object' == typeof nsp) {
  11. opts = nsp;
  12. nsp = null;
  13. }
  14. var addr = srv.address();
  15. if (!addr) addr = srv.listen().address();
  16. var url = 'ws://' + addr.address + ':' + addr.port + (nsp || '');
  17. return ioc(url, opts);
  18. }
  19. describe('socket.io', function(){
  20. it('should be the same version as client', function(){
  21. var version = require('../package').version;
  22. expect(version).to.be(require('socket.io-client/package').version);
  23. });
  24. describe('set', function() {
  25. it('should be able to set ping timeout to engine.io', function() {
  26. var srv = io(http());
  27. srv.set('heartbeat timeout', 10);
  28. expect(srv.eio.pingTimeout).to.be(10);
  29. });
  30. it('should be able to set ping interval to engine.io', function() {
  31. var srv = io(http());
  32. srv.set('heartbeat interval', 10);
  33. expect(srv.eio.pingInterval).to.be(10);
  34. });
  35. it('should be able to set transports to engine.io', function() {
  36. var srv = io(http());
  37. srv.set('transports', ['polling']);
  38. expect(srv.eio.transports).to.eql(['polling']);
  39. });
  40. it('should be able to set maxHttpBufferSize to engine.io', function() {
  41. var srv = io(http());
  42. srv.set('destroy buffer size', 10);
  43. expect(srv.eio.maxHttpBufferSize).to.eql(10);
  44. });
  45. it('should be able to set path with setting resource', function() {
  46. var srv = io(http());
  47. srv.set('resource', '/random');
  48. expect(srv.path()).to.be('/random');
  49. });
  50. it('should be able to set origins to engine.io', function() {
  51. var srv = io(http());
  52. srv.set('origins', 'http://hostname.com:*');
  53. expect(srv.origins()).to.be('http://hostname.com:*');
  54. });
  55. it('should be able to set authorization and send error packet', function(done) {
  56. var httpSrv = http();
  57. var srv = io(httpSrv);
  58. srv.set('authorization', function(o, f) { f(null, false); });
  59. var socket = client(httpSrv);
  60. socket.on('connect', function(){
  61. expect().fail();
  62. });
  63. socket.on('error', function(err) {
  64. expect(err).to.be('Not authorized');
  65. done();
  66. });
  67. });
  68. it('should be able to set authorization and succeed', function(done) {
  69. var httpSrv = http();
  70. var srv = io(httpSrv);
  71. srv.set('authorization', function(o, f) { f(null, true); });
  72. srv.on('connection', function(s) {
  73. s.on('yoyo', function(data) {
  74. expect(data).to.be('data');
  75. done();
  76. });
  77. });
  78. var socket = client(httpSrv);
  79. socket.on('connect', function(){
  80. socket.emit('yoyo', 'data');
  81. });
  82. socket.on('error', function(err) {
  83. expect().fail();
  84. });
  85. });
  86. it('should set the handshake BC object', function(done){
  87. var httpSrv = http();
  88. var srv = io(httpSrv);
  89. srv.on('connection', function(s) {
  90. expect(s.handshake).to.not.be(undefined);
  91. // Headers set and has some valid properties
  92. expect(s.handshake.headers).to.be.an('object');
  93. expect(s.handshake.headers['user-agent']).to.be('node-XMLHttpRequest');
  94. // Time set and is valid looking string
  95. expect(s.handshake.time).to.be.a('string');
  96. expect(s.handshake.time.split(' ').length > 0); // Is "multipart" string representation
  97. // Address, xdomain, secure, issued and url set
  98. expect(s.handshake.address).to.not.be(undefined);
  99. expect(s.handshake.xdomain).to.be.a('boolean');
  100. expect(s.handshake.secure).to.be.a('boolean');
  101. expect(s.handshake.issued).to.be.a('number');
  102. expect(s.handshake.url).to.be.a('string');
  103. // Query set and has some right properties
  104. expect(s.handshake.query).to.be.an('object');
  105. expect(s.handshake.query.EIO).to.not.be(undefined);
  106. expect(s.handshake.query.transport).to.not.be(undefined);
  107. expect(s.handshake.query.t).to.not.be(undefined);
  108. done();
  109. });
  110. var socket = client(httpSrv);
  111. });
  112. });
  113. describe('server attachment', function(){
  114. describe('http.Server', function(){
  115. var clientVersion = require('socket.io-client/package').version;
  116. it('should serve static files', function(done){
  117. var srv = http();
  118. io(srv);
  119. request(srv)
  120. .get('/socket.io/socket.io.js')
  121. .buffer(true)
  122. .end(function(err, res){
  123. if (err) return done(err);
  124. var ctype = res.headers['content-type'];
  125. expect(ctype).to.be('application/javascript');
  126. expect(res.headers.etag).to.be(clientVersion);
  127. expect(res.text).to.match(/engine\.io/);
  128. expect(res.status).to.be(200);
  129. done();
  130. });
  131. });
  132. it('should handle 304', function(done){
  133. var srv = http();
  134. io(srv);
  135. request(srv)
  136. .get('/socket.io/socket.io.js')
  137. .set('ETag', clientVersion)
  138. .end(function(err, res){
  139. if (err) return done(err);
  140. expect(res.statusCode).to.be(304);
  141. done();
  142. });
  143. });
  144. it('should not serve static files', function(done){
  145. var srv = http();
  146. io(srv, { serveClient: false });
  147. request(srv)
  148. .get('/socket.io/socket.io.js')
  149. .expect(400, done);
  150. });
  151. it('should work with #attach', function(done){
  152. var srv = http(function(req, res){
  153. res.writeHead(404);
  154. res.end();
  155. });
  156. var sockets = io();
  157. sockets.attach(srv);
  158. request(srv)
  159. .get('/socket.io/socket.io.js')
  160. .end(function(err, res){
  161. if (err) return done(err);
  162. expect(res.status).to.be(200);
  163. done();
  164. });
  165. });
  166. });
  167. describe('port', function(done){
  168. it('should be bound', function(done){
  169. var sockets = io(54010);
  170. request('http://localhost:54010')
  171. .get('/socket.io/socket.io.js')
  172. .expect(200, done);
  173. });
  174. it('should be bound as a string', function(done) {
  175. var sockets = io('54020');
  176. request('http://localhost:54020')
  177. .get('/socket.io/socket.io.js')
  178. .expect(200, done);
  179. });
  180. it('with listen', function(done){
  181. var sockets = io().listen(54011);
  182. request('http://localhost:54011')
  183. .get('/socket.io/socket.io.js')
  184. .expect(200, done);
  185. });
  186. it('as a string', function(done){
  187. var sockets = io().listen('54012');
  188. request('http://localhost:54012')
  189. .get('/socket.io/socket.io.js')
  190. .expect(200, done);
  191. });
  192. });
  193. });
  194. describe('handshake', function(){
  195. var request = require('superagent');
  196. it('should disallow request when origin defined and none specified', function(done) {
  197. var sockets = io({ origins: 'http://foo.example:*' }).listen('54013');
  198. request.get('http://localhost:54013/socket.io/default/')
  199. .query({ transport: 'polling' })
  200. .end(function (err, res) {
  201. expect(res.status).to.be(400);
  202. done();
  203. });
  204. });
  205. it('should disallow request when origin defined and a different one specified', function(done) {
  206. var sockets = io({ origins: 'http://foo.example:*' }).listen('54014');
  207. request.get('http://localhost:54014/socket.io/default/')
  208. .query({ transport: 'polling' })
  209. .set('origin', 'http://herp.derp')
  210. .end(function (err, res) {
  211. expect(res.status).to.be(400);
  212. done();
  213. });
  214. });
  215. it('should allow request when origin defined an the same is specified', function(done) {
  216. var sockets = io({ origins: 'http://foo.example:*' }).listen('54015');
  217. request.get('http://localhost:54015/socket.io/default/')
  218. .set('origin', 'http://foo.example')
  219. .query({ transport: 'polling' })
  220. .end(function (err, res) {
  221. expect(res.status).to.be(200);
  222. done();
  223. });
  224. });
  225. });
  226. describe('namespaces', function(){
  227. var Socket = require('../lib/socket');
  228. var Namespace = require('../lib/namespace');
  229. describe('default', function(){
  230. it('should be accessible through .sockets', function(){
  231. var sio = io();
  232. expect(sio.sockets).to.be.a(Namespace);
  233. });
  234. it('should be aliased', function(){
  235. var sio = io();
  236. expect(sio.use).to.be.a('function');
  237. expect(sio.to).to.be.a('function');
  238. expect(sio['in']).to.be.a('function');
  239. expect(sio.emit).to.be.a('function');
  240. expect(sio.send).to.be.a('function');
  241. expect(sio.write).to.be.a('function');
  242. });
  243. it('should automatically connect', function(done){
  244. var srv = http();
  245. var sio = io(srv);
  246. srv.listen(function(){
  247. var socket = client(srv);
  248. socket.on('connect', function(){
  249. done();
  250. });
  251. });
  252. });
  253. it('should fire a `connection` event', function(done){
  254. var srv = http();
  255. var sio = io(srv);
  256. srv.listen(function(){
  257. var socket = client(srv);
  258. sio.on('connection', function(socket){
  259. expect(socket).to.be.a(Socket);
  260. done();
  261. });
  262. });
  263. });
  264. it('should fire a `connect` event', function(done){
  265. var srv = http();
  266. var sio = io(srv);
  267. srv.listen(function(){
  268. var socket = client(srv);
  269. sio.on('connect', function(socket){
  270. expect(socket).to.be.a(Socket);
  271. done();
  272. });
  273. });
  274. });
  275. it('should work with many sockets', function(done){
  276. var srv = http();
  277. var sio = io(srv);
  278. srv.listen(function(){
  279. var chat = client(srv, '/chat');
  280. var news = client(srv, '/news');
  281. var total = 2;
  282. chat.on('connect', function(){
  283. --total || done();
  284. });
  285. news.on('connect', function(){
  286. --total || done();
  287. });
  288. });
  289. });
  290. it('should work with `of` and many sockets', function(done){
  291. var srv = http();
  292. var sio = io(srv);
  293. srv.listen(function(){
  294. var chat = client(srv, '/chat');
  295. var news = client(srv, '/news');
  296. var total = 2;
  297. sio.of('/news').on('connection', function(socket){
  298. expect(socket).to.be.a(Socket);
  299. --total || done();
  300. });
  301. sio.of('/news').on('connection', function(socket){
  302. expect(socket).to.be.a(Socket);
  303. --total || done();
  304. });
  305. });
  306. });
  307. it('should work with `of` second param', function(done){
  308. var srv = http();
  309. var sio = io(srv);
  310. srv.listen(function(){
  311. var chat = client(srv, '/chat');
  312. var news = client(srv, '/news');
  313. var total = 2;
  314. sio.of('/news', function(socket){
  315. expect(socket).to.be.a(Socket);
  316. --total || done();
  317. });
  318. sio.of('/news', function(socket){
  319. expect(socket).to.be.a(Socket);
  320. --total || done();
  321. });
  322. });
  323. });
  324. it('should disconnect upon transport disconnection', function(done){
  325. var srv = http();
  326. var sio = io(srv);
  327. srv.listen(function(){
  328. var chat = client(srv, '/chat');
  329. var news = client(srv, '/news');
  330. var total = 2;
  331. var totald = 2;
  332. var s;
  333. sio.of('/news', function(socket){
  334. socket.on('disconnect', function(reason){
  335. --totald || done();
  336. });
  337. --total || close();
  338. });
  339. sio.of('/chat', function(socket){
  340. s = socket;
  341. socket.on('disconnect', function(reason){
  342. --totald || done();
  343. });
  344. --total || close();
  345. });
  346. function close(){
  347. s.disconnect(true);
  348. }
  349. });
  350. });
  351. });
  352. });
  353. describe('socket', function(){
  354. it('should receive events', function(done){
  355. var srv = http();
  356. var sio = io(srv);
  357. srv.listen(function(){
  358. var socket = client(srv);
  359. sio.on('connection', function(s){
  360. s.on('random', function(a, b, c){
  361. expect(a).to.be(1);
  362. expect(b).to.be('2');
  363. expect(c).to.eql([3]);
  364. done();
  365. });
  366. socket.emit('random', 1, '2', [3]);
  367. });
  368. });
  369. });
  370. it('should receive message events through `send`', function(done){
  371. var srv = http();
  372. var sio = io(srv);
  373. srv.listen(function(){
  374. var socket = client(srv);
  375. sio.on('connection', function(s){
  376. s.on('message', function(a){
  377. expect(a).to.be(1337);
  378. done();
  379. });
  380. socket.send(1337);
  381. });
  382. });
  383. });
  384. it('should emit events', function(done){
  385. var srv = http();
  386. var sio = io(srv);
  387. srv.listen(function(){
  388. var socket = client(srv);
  389. socket.on('woot', function(a){
  390. expect(a).to.be('tobi');
  391. done();
  392. });
  393. sio.on('connection', function(s){
  394. s.emit('woot', 'tobi');
  395. });
  396. });
  397. });
  398. it('should emit events with binary data', function(done){
  399. var srv = http();
  400. var sio = io(srv);
  401. srv.listen(function(){
  402. var socket = client(srv);
  403. var imageData;
  404. socket.on('doge', function(a){
  405. expect(Buffer.isBuffer(a)).to.be(true);
  406. expect(imageData.length).to.equal(a.length);
  407. expect(imageData[0]).to.equal(a[0]);
  408. expect(imageData[imageData.length - 1]).to.equal(a[a.length - 1]);
  409. done();
  410. });
  411. sio.on('connection', function(s){
  412. fs.readFile(join(__dirname, 'support', 'doge.jpg'), function(err, data){
  413. if (err) return done(err);
  414. imageData = data;
  415. s.emit('doge', data);
  416. });
  417. });
  418. });
  419. });
  420. it('should emit events with several types of data (including binary)', function(done){
  421. var srv = http();
  422. var sio = io(srv);
  423. srv.listen(function(){
  424. var socket = client(srv);
  425. socket.on('multiple', function(a, b, c, d, e, f){
  426. expect(a).to.be(1);
  427. expect(Buffer.isBuffer(b)).to.be(true);
  428. expect(c).to.be('3');
  429. expect(d).to.eql([4]);
  430. expect(Buffer.isBuffer(e)).to.be(true);
  431. expect(Buffer.isBuffer(f[0])).to.be(true);
  432. expect(f[1]).to.be('swag');
  433. expect(Buffer.isBuffer(f[2])).to.be(true);
  434. done();
  435. });
  436. sio.on('connection', function(s){
  437. fs.readFile(join(__dirname, 'support', 'doge.jpg'), function(err, data){
  438. if (err) return done(err);
  439. var buf = new Buffer('asdfasdf', 'utf8');
  440. s.emit('multiple', 1, data, '3', [4], buf, [data, 'swag', buf]);
  441. });
  442. });
  443. });
  444. });
  445. it('should receive events with binary data', function(done){
  446. var srv = http();
  447. var sio = io(srv);
  448. srv.listen(function(){
  449. var socket = client(srv);
  450. sio.on('connection', function(s){
  451. s.on('buff', function(a){
  452. expect(Buffer.isBuffer(a)).to.be(true);
  453. done();
  454. });
  455. var buf = new Buffer('abcdefg', 'utf8');
  456. socket.emit('buff', buf);
  457. });
  458. });
  459. });
  460. it('should receive events with several types of data (including binary)', function(done){
  461. var srv = http();
  462. var sio = io(srv);
  463. srv.listen(function(){
  464. var socket = client(srv);
  465. sio.on('connection', function(s){
  466. s.on('multiple', function(a, b, c, d, e, f){
  467. expect(a).to.be(1);
  468. expect(Buffer.isBuffer(b)).to.be(true);
  469. expect(c).to.be('3');
  470. expect(d).to.eql([4]);
  471. expect(Buffer.isBuffer(e)).to.be(true);
  472. expect(Buffer.isBuffer(f[0])).to.be(true);
  473. expect(f[1]).to.be('swag');
  474. expect(Buffer.isBuffer(f[2])).to.be(true);
  475. done();
  476. });
  477. fs.readFile(join(__dirname, 'support', 'doge.jpg'), function(err, data){
  478. if (err) return done(err);
  479. var buf = new Buffer('asdfasdf', 'utf8');
  480. socket.emit('multiple', 1, data, '3', [4], buf, [data, 'swag', buf]);
  481. });
  482. });
  483. });
  484. });
  485. it('should emit message events through `send`', function(done){
  486. var srv = http();
  487. var sio = io(srv);
  488. srv.listen(function(){
  489. var socket = client(srv);
  490. socket.on('message', function(a){
  491. expect(a).to.be('a');
  492. done();
  493. });
  494. sio.on('connection', function(s){
  495. s.send('a');
  496. });
  497. });
  498. });
  499. it('should receive event with callbacks', function(done){
  500. var srv = http();
  501. var sio = io(srv);
  502. srv.listen(function(){
  503. var socket = client(srv);
  504. sio.on('connection', function(s){
  505. s.on('woot', function(fn){
  506. fn(1, 2);
  507. });
  508. socket.emit('woot', function(a, b){
  509. expect(a).to.be(1);
  510. expect(b).to.be(2);
  511. done();
  512. });
  513. });
  514. });
  515. });
  516. it('should emit events with callbacks', function(done){
  517. var srv = http();
  518. var sio = io(srv);
  519. srv.listen(function(){
  520. var socket = client(srv);
  521. sio.on('connection', function(s){
  522. socket.on('hi', function(fn){
  523. fn();
  524. });
  525. s.emit('hi', function(){
  526. done();
  527. });
  528. });
  529. });
  530. });
  531. it('should receive events with args and callback', function(done){
  532. var srv = http();
  533. var sio = io(srv);
  534. srv.listen(function(){
  535. var socket = client(srv);
  536. sio.on('connection', function(s){
  537. s.on('woot', function(a, b, fn){
  538. expect(a).to.be(1);
  539. expect(b).to.be(2);
  540. fn();
  541. });
  542. socket.emit('woot', 1, 2, function(){
  543. done();
  544. });
  545. });
  546. });
  547. });
  548. it('should emit events with args and callback', function(done){
  549. var srv = http();
  550. var sio = io(srv);
  551. srv.listen(function(){
  552. var socket = client(srv);
  553. sio.on('connection', function(s){
  554. socket.on('hi', function(a, b, fn){
  555. expect(a).to.be(1);
  556. expect(b).to.be(2);
  557. fn();
  558. });
  559. s.emit('hi', 1, 2, function(){
  560. done();
  561. });
  562. });
  563. });
  564. });
  565. it('should receive events with binary args and callbacks', function(done) {
  566. var srv = http();
  567. var sio = io(srv);
  568. srv.listen(function(){
  569. var socket = client(srv);
  570. sio.on('connection', function(s){
  571. s.on('woot', function(buf, fn){
  572. expect(Buffer.isBuffer(buf)).to.be(true);
  573. fn(1, 2);
  574. });
  575. socket.emit('woot', new Buffer(3), function(a, b){
  576. expect(a).to.be(1);
  577. expect(b).to.be(2);
  578. done();
  579. });
  580. });
  581. });
  582. });
  583. it('should emit events with binary args and callback', function(done){
  584. var srv = http();
  585. var sio = io(srv);
  586. srv.listen(function(){
  587. var socket = client(srv);
  588. sio.on('connection', function(s){
  589. socket.on('hi', function(a, fn){
  590. expect(Buffer.isBuffer(a)).to.be(true);
  591. fn();
  592. });
  593. s.emit('hi', new Buffer(4), function(){
  594. done();
  595. });
  596. });
  597. });
  598. });
  599. it('should emit events and receive binary data in a callback', function(done) {
  600. var srv = http();
  601. var sio = io(srv);
  602. srv.listen(function(){
  603. var socket = client(srv);
  604. sio.on('connection', function(s){
  605. socket.on('hi', function(fn){
  606. fn(new Buffer(1));
  607. });
  608. s.emit('hi', function(a){
  609. expect(Buffer.isBuffer(a)).to.be(true);
  610. done();
  611. });
  612. });
  613. });
  614. });
  615. it('should receive events and pass binary data in a callback', function(done) {
  616. var srv = http();
  617. var sio = io(srv);
  618. srv.listen(function(){
  619. var socket = client(srv);
  620. sio.on('connection', function(s){
  621. s.on('woot', function(fn){
  622. fn(new Buffer(2));
  623. });
  624. socket.emit('woot', function(a){
  625. expect(Buffer.isBuffer(a)).to.be(true);
  626. done();
  627. });
  628. });
  629. });
  630. });
  631. it('should have access to the client', function(done){
  632. var srv = http();
  633. var sio = io(srv);
  634. srv.listen(function(){
  635. var socket = client(srv);
  636. sio.on('connection', function(s){
  637. expect(s.client).to.be.an('object');
  638. done();
  639. });
  640. });
  641. });
  642. it('should have access to the connection', function(done){
  643. var srv = http();
  644. var sio = io(srv);
  645. srv.listen(function(){
  646. var socket = client(srv);
  647. sio.on('connection', function(s){
  648. expect(s.client.conn).to.be.an('object');
  649. expect(s.conn).to.be.an('object');
  650. done();
  651. });
  652. });
  653. });
  654. it('should have access to the request', function(done){
  655. var srv = http();
  656. var sio = io(srv);
  657. srv.listen(function(){
  658. var socket = client(srv);
  659. sio.on('connection', function(s){
  660. expect(s.client.request.headers).to.be.an('object');
  661. expect(s.request.headers).to.be.an('object');
  662. done();
  663. });
  664. });
  665. });
  666. it('should see query parameters in the request', function(done) {
  667. var srv = http();
  668. var sio = io(srv);
  669. srv.listen(function() {
  670. var addr = srv.listen().address();
  671. var url = 'ws://' + addr.address + ':' + addr.port + '?key1=1&key2=2';
  672. var socket = ioc(url);
  673. sio.on('connection', function(s) {
  674. var parsed = require('url').parse(s.request.url);
  675. var query = require('querystring').parse(parsed.query);
  676. expect(query.key1).to.be('1');
  677. expect(query.key2).to.be('2');
  678. done();
  679. });
  680. });
  681. });
  682. });
  683. describe('messaging many', function(){
  684. it('emits to a namespace', function(done){
  685. var srv = http();
  686. var sio = io(srv);
  687. var total = 2;
  688. srv.listen(function(){
  689. var socket1 = client(srv, { multiplex: false });
  690. var socket2 = client(srv, { multiplex: false });
  691. var socket3 = client(srv, '/test');
  692. socket1.on('a', function(a){
  693. expect(a).to.be('b');
  694. --total || done();
  695. });
  696. socket2.on('a', function(a){
  697. expect(a).to.be('b');
  698. --total || done();
  699. });
  700. socket3.on('a', function(){ done(new Error('not')); });
  701. var sockets = 3;
  702. sio.on('connection', function(socket){
  703. --sockets || emit();
  704. });
  705. sio.of('/test', function(socket){
  706. --sockets || emit();
  707. });
  708. function emit(){
  709. sio.emit('a', 'b');
  710. }
  711. });
  712. });
  713. it('emits binary data to a namespace', function(done){
  714. var srv = http();
  715. var sio = io(srv);
  716. var total = 2;
  717. srv.listen(function(){
  718. var socket1 = client(srv, { multiplex: false });
  719. var socket2 = client(srv, { multiplex: false });
  720. var socket3 = client(srv, '/test');
  721. socket1.on('bin', function(a){
  722. expect(Buffer.isBuffer(a)).to.be(true);
  723. --total || done();
  724. });
  725. socket2.on('bin', function(a){
  726. expect(Buffer.isBuffer(a)).to.be(true);
  727. --total || done();
  728. });
  729. socket3.on('bin', function(){ done(new Error('not')); });
  730. var sockets = 3;
  731. sio.on('connection', function(socket){
  732. --sockets || emit();
  733. });
  734. sio.of('/test', function(socket){
  735. --sockets || emit();
  736. });
  737. function emit(){
  738. sio.emit('bin', new Buffer(10));
  739. }
  740. });
  741. });
  742. it('emits to the rest', function(done){
  743. var srv = http();
  744. var sio = io(srv);
  745. var total = 2;
  746. srv.listen(function(){
  747. var socket1 = client(srv, { multiplex: false });
  748. var socket2 = client(srv, { multiplex: false });
  749. var socket3 = client(srv, '/test');
  750. socket1.on('a', function(a){
  751. expect(a).to.be('b');
  752. socket1.emit('finish');
  753. });
  754. socket2.emit('broadcast');
  755. socket2.on('a', function(){ done(new Error('done')); });
  756. socket3.on('a', function(){ done(new Error('not')); });
  757. var sockets = 2;
  758. sio.on('connection', function(socket){
  759. socket.on('broadcast', function(){
  760. socket.broadcast.emit('a', 'b');
  761. });
  762. socket.on('finish', function(){
  763. done();
  764. });
  765. });
  766. });
  767. });
  768. it('emits to rooms', function(done){
  769. var srv = http();
  770. var sio = io(srv);
  771. var total = 2;
  772. srv.listen(function(){
  773. var socket1 = client(srv, { multiplex: false });
  774. var socket2 = client(srv, { multiplex: false });
  775. socket2.on('a', function(){
  776. done(new Error('not'));
  777. });
  778. socket1.on('a', function(){
  779. done();
  780. });
  781. socket1.emit('join', 'woot', function(){
  782. socket1.emit('emit', 'woot');
  783. });
  784. sio.on('connection', function(socket){
  785. socket.on('join', function(room, fn){
  786. socket.join(room, fn);
  787. });
  788. socket.on('emit', function(room){
  789. sio.in(room).emit('a');
  790. });
  791. });
  792. });
  793. });
  794. it('emits to rooms avoiding dupes', function(done){
  795. var srv = http();
  796. var sio = io(srv);
  797. var total = 2;
  798. srv.listen(function(){
  799. var socket1 = client(srv, { multiplex: false });
  800. var socket2 = client(srv, { multiplex: false });
  801. socket2.on('a', function(){
  802. done(new Error('not'));
  803. });
  804. socket1.on('a', function(){
  805. --total || done();
  806. });
  807. socket2.on('b', function(){
  808. --total || done();
  809. });
  810. socket1.emit('join', 'woot');
  811. socket1.emit('join', 'test');
  812. socket2.emit('join', 'third', function(){
  813. socket2.emit('emit');
  814. });
  815. sio.on('connection', function(socket){
  816. socket.on('join', function(room, fn){
  817. socket.join(room, fn);
  818. });
  819. socket.on('emit', function(room){
  820. sio.in('woot').in('test').emit('a');
  821. sio.in('third').emit('b');
  822. });
  823. });
  824. });
  825. });
  826. it('broadcasts to rooms', function(done){
  827. var srv = http();
  828. var sio = io(srv);
  829. var total = 2;
  830. srv.listen(function(){
  831. var socket1 = client(srv, { multiplex: false });
  832. var socket2 = client(srv, { multiplex: false });
  833. var socket3 = client(srv, { multiplex: false });
  834. socket1.emit('join', 'woot');
  835. socket2.emit('join', 'test');
  836. socket3.emit('join', 'test', function(){
  837. socket3.emit('broadcast');
  838. });
  839. socket1.on('a', function(){
  840. done(new Error('not'));
  841. });
  842. socket2.on('a', function(){
  843. --total || done();
  844. });
  845. socket3.on('a', function(){
  846. done(new Error('not'));
  847. });
  848. socket3.on('b', function(){
  849. --total || done();
  850. });
  851. sio.on('connection', function(socket){
  852. socket.on('join', function(room, fn){
  853. socket.join(room, fn);
  854. });
  855. socket.on('broadcast', function(){
  856. socket.broadcast.to('test').emit('a');
  857. socket.emit('b');
  858. });
  859. });
  860. });
  861. });
  862. it('broadcasts binary data to rooms', function(done){
  863. var srv = http();
  864. var sio = io(srv);
  865. var total = 2;
  866. srv.listen(function(){
  867. var socket1 = client(srv, { multiplex: false });
  868. var socket2 = client(srv, { multiplex: false });
  869. var socket3 = client(srv, { multiplex: false });
  870. socket1.emit('join', 'woot');
  871. socket2.emit('join', 'test');
  872. socket3.emit('join', 'test', function(){
  873. socket3.emit('broadcast');
  874. });
  875. socket1.on('bin', function(data){
  876. throw new Error('got bin in socket1');
  877. });
  878. socket2.on('bin', function(data){
  879. expect(Buffer.isBuffer(data)).to.be(true);
  880. --total || done();
  881. });
  882. socket2.on('bin2', function(data) {
  883. throw new Error('socket2 got bin2');
  884. });
  885. socket3.on('bin', function(data) {
  886. throw new Error('socket3 got bin');
  887. });
  888. socket3.on('bin2', function(data) {
  889. expect(Buffer.isBuffer(data)).to.be(true);
  890. --total || done();
  891. });
  892. sio.on('connection', function(socket){
  893. socket.on('join', function(room, fn){
  894. socket.join(room, fn);
  895. });
  896. socket.on('broadcast', function(){
  897. socket.broadcast.to('test').emit('bin', new Buffer(5));
  898. socket.emit('bin2', new Buffer(5));
  899. });
  900. });
  901. });
  902. });
  903. it('keeps track of rooms', function(done){
  904. var srv = http();
  905. var sio = io(srv);
  906. srv.listen(function(){
  907. var socket = client(srv);
  908. sio.on('connection', function(s){
  909. s.join('a', function(){
  910. expect(s.rooms).to.eql([s.id, 'a']);
  911. s.join('b', function(){
  912. expect(s.rooms).to.eql([s.id, 'a', 'b']);
  913. s.leave('b', function(){
  914. expect(s.rooms).to.eql([s.id, 'a']);
  915. done();
  916. });
  917. });
  918. });
  919. });
  920. });
  921. });
  922. });
  923. describe('middleware', function(done){
  924. var Socket = require('../lib/socket');
  925. it('should call functions', function(done){
  926. var srv = http();
  927. var sio = io(srv);
  928. var run = 0;
  929. sio.use(function(socket, next){
  930. expect(socket).to.be.a(Socket);
  931. run++;
  932. next();
  933. });
  934. sio.use(function(socket, next){
  935. expect(socket).to.be.a(Socket);
  936. run++;
  937. next();
  938. });
  939. srv.listen(function(){
  940. var socket = client(srv);
  941. socket.on('connect', function(){
  942. expect(run).to.be(2);
  943. done();
  944. });
  945. });
  946. });
  947. it('should pass errors', function(done){
  948. var srv = http();
  949. var sio = io(srv);
  950. var run = 0;
  951. sio.use(function(socket, next){
  952. next(new Error('Authentication error'));
  953. });
  954. sio.use(function(socket, next){
  955. done(new Error('nope'));
  956. });
  957. srv.listen(function(){
  958. var socket = client(srv);
  959. socket.on('connect', function(){
  960. done(new Error('nope'));
  961. });
  962. socket.on('error', function(err){
  963. expect(err).to.be('Authentication error');
  964. done();
  965. });
  966. });
  967. });
  968. it('should pass `data` of error object', function(done){
  969. var srv = http();
  970. var sio = io(srv);
  971. var run = 0;
  972. sio.use(function(socket, next){
  973. var err = new Error('Authentication error');
  974. err.data = { a: 'b', c: 3 };
  975. next(err);
  976. });
  977. srv.listen(function(){
  978. var socket = client(srv);
  979. socket.on('connect', function(){
  980. done(new Error('nope'));
  981. });
  982. socket.on('error', function(err){
  983. expect(err).to.eql({ a: 'b', c: 3 });
  984. done();
  985. });
  986. });
  987. });
  988. it('should only call connection after fns', function(done){
  989. var srv = http();
  990. var sio = io(srv);
  991. sio.use(function(socket, next){
  992. socket.name = 'guillermo';
  993. next();
  994. });
  995. srv.listen(function(){
  996. var socket = client(srv);
  997. sio.on('connection', function(socket){
  998. expect(socket.name).to.be('guillermo');
  999. done();
  1000. });
  1001. });
  1002. });
  1003. it('should be ignored if socket gets closed', function(done){
  1004. var srv = http();
  1005. var sio = io(srv);
  1006. var socket;
  1007. sio.use(function(s, next){
  1008. socket.io.engine.on('open', function(){
  1009. socket.io.engine.close();
  1010. s.client.conn.on('close', function(){
  1011. process.nextTick(next);
  1012. setTimeout(function(){
  1013. done();
  1014. }, 50);
  1015. });
  1016. });
  1017. });
  1018. srv.listen(function(){
  1019. socket = client(srv);
  1020. sio.on('connection', function(socket){
  1021. done(new Error('should not fire'));
  1022. });
  1023. });
  1024. });
  1025. it('should call functions in expected order', function(done){
  1026. var srv = http();
  1027. var sio = io(srv);
  1028. var result = [];
  1029. sio.use(function(socket, next) {
  1030. result.push(1);
  1031. setTimeout(next, 50);
  1032. });
  1033. sio.use(function(socket, next) {
  1034. result.push(2);
  1035. setTimeout(next, 50);
  1036. });
  1037. sio.of('/chat').use(function(socket, next) {
  1038. result.push(3);
  1039. setTimeout(next, 50);
  1040. });
  1041. sio.of('/chat').use(function(socket, next) {
  1042. result.push(4);
  1043. setTimeout(next, 50);
  1044. });
  1045. srv.listen(function() {
  1046. var chat = client(srv, '/chat');
  1047. chat.on('connect', function() {
  1048. expect(result).to.eql([1, 2, 3, 4]);
  1049. done();
  1050. });
  1051. });
  1052. });
  1053. });
  1054. });