PageRenderTime 49ms CodeModel.GetById 19ms RepoModel.GetById 1ms app.codeStats 0ms

/test/socket.io.js

https://github.com/narcisoguillen/socket.io
JavaScript | 1325 lines | 1196 code | 124 blank | 5 comment | 55 complexity | 0b051850d7629b5e93df412375ae4e8b 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. it('should disconnect both default and custom namespace upon disconnect', function(done){
  352. var srv = http();
  353. var sio = io(srv);
  354. srv.listen(function(){
  355. var lolcats = client(srv, '/lolcats');
  356. var total = 2;
  357. var totald = 2;
  358. var s;
  359. sio.of('/', function(socket){
  360. socket.on('disconnect', function(reason){
  361. --totald || done();
  362. });
  363. --total || close();
  364. });
  365. sio.of('/lolcats', function(socket){
  366. s = socket;
  367. socket.on('disconnect', function(reason){
  368. --totald || done();
  369. });
  370. --total || close();
  371. });
  372. function close(){
  373. s.disconnect(true);
  374. }
  375. });
  376. });
  377. });
  378. });
  379. describe('socket', function(){
  380. it('should not fire events more than once after manually reconnecting', function(done) {
  381. var srv = http();
  382. var sio = io(srv);
  383. srv.listen(function(){
  384. var clientSocket = client(srv, { reconnection: false });
  385. clientSocket.on('connect', function init() {
  386. clientSocket.removeListener('connect', init);
  387. clientSocket.io.engine.close();
  388. clientSocket.connect();
  389. clientSocket.on('connect', function() {
  390. done();
  391. });
  392. });
  393. });
  394. });
  395. it('should not fire reconnect_failed event more than once when server closed', function(done) {
  396. var srv = http();
  397. var sio = io(srv);
  398. srv.listen(function(){
  399. var clientSocket = client(srv, { reconnectionAttempts: 3, reconnectionDelay: 10 });
  400. clientSocket.on('connect', function() {
  401. srv.close();
  402. });
  403. clientSocket.on('reconnect_failed', function() {
  404. done();
  405. });
  406. });
  407. });
  408. it('should receive events', function(done){
  409. var srv = http();
  410. var sio = io(srv);
  411. srv.listen(function(){
  412. var socket = client(srv);
  413. sio.on('connection', function(s){
  414. s.on('random', function(a, b, c){
  415. expect(a).to.be(1);
  416. expect(b).to.be('2');
  417. expect(c).to.eql([3]);
  418. done();
  419. });
  420. socket.emit('random', 1, '2', [3]);
  421. });
  422. });
  423. });
  424. it('should receive message events through `send`', function(done){
  425. var srv = http();
  426. var sio = io(srv);
  427. srv.listen(function(){
  428. var socket = client(srv);
  429. sio.on('connection', function(s){
  430. s.on('message', function(a){
  431. expect(a).to.be(1337);
  432. done();
  433. });
  434. socket.send(1337);
  435. });
  436. });
  437. });
  438. it('should emit events', function(done){
  439. var srv = http();
  440. var sio = io(srv);
  441. srv.listen(function(){
  442. var socket = client(srv);
  443. socket.on('woot', function(a){
  444. expect(a).to.be('tobi');
  445. done();
  446. });
  447. sio.on('connection', function(s){
  448. s.emit('woot', 'tobi');
  449. });
  450. });
  451. });
  452. it('should emit events with utf8 multibyte character', function(done) {
  453. var srv = http();
  454. var sio = io(srv);
  455. srv.listen(function(){
  456. var socket = client(srv);
  457. var i = 0;
  458. socket.on('hoot', function(a){
  459. expect(a).to.be('utf8 — string');
  460. i++;
  461. if (3 == i) {
  462. done();
  463. }
  464. });
  465. sio.on('connection', function(s){
  466. s.emit('hoot', 'utf8 — string');
  467. s.emit('hoot', 'utf8 — string');
  468. s.emit('hoot', 'utf8 — string');
  469. });
  470. });
  471. });
  472. it('should emit events with binary data', function(done){
  473. var srv = http();
  474. var sio = io(srv);
  475. srv.listen(function(){
  476. var socket = client(srv);
  477. var imageData;
  478. socket.on('doge', function(a){
  479. expect(Buffer.isBuffer(a)).to.be(true);
  480. expect(imageData.length).to.equal(a.length);
  481. expect(imageData[0]).to.equal(a[0]);
  482. expect(imageData[imageData.length - 1]).to.equal(a[a.length - 1]);
  483. done();
  484. });
  485. sio.on('connection', function(s){
  486. fs.readFile(join(__dirname, 'support', 'doge.jpg'), function(err, data){
  487. if (err) return done(err);
  488. imageData = data;
  489. s.emit('doge', data);
  490. });
  491. });
  492. });
  493. });
  494. it('should emit events with several types of data (including binary)', function(done){
  495. var srv = http();
  496. var sio = io(srv);
  497. srv.listen(function(){
  498. var socket = client(srv);
  499. socket.on('multiple', function(a, b, c, d, e, f){
  500. expect(a).to.be(1);
  501. expect(Buffer.isBuffer(b)).to.be(true);
  502. expect(c).to.be('3');
  503. expect(d).to.eql([4]);
  504. expect(Buffer.isBuffer(e)).to.be(true);
  505. expect(Buffer.isBuffer(f[0])).to.be(true);
  506. expect(f[1]).to.be('swag');
  507. expect(Buffer.isBuffer(f[2])).to.be(true);
  508. done();
  509. });
  510. sio.on('connection', function(s){
  511. fs.readFile(join(__dirname, 'support', 'doge.jpg'), function(err, data){
  512. if (err) return done(err);
  513. var buf = new Buffer('asdfasdf', 'utf8');
  514. s.emit('multiple', 1, data, '3', [4], buf, [data, 'swag', buf]);
  515. });
  516. });
  517. });
  518. });
  519. it('should receive events with binary data', function(done){
  520. var srv = http();
  521. var sio = io(srv);
  522. srv.listen(function(){
  523. var socket = client(srv);
  524. sio.on('connection', function(s){
  525. s.on('buff', function(a){
  526. expect(Buffer.isBuffer(a)).to.be(true);
  527. done();
  528. });
  529. var buf = new Buffer('abcdefg', 'utf8');
  530. socket.emit('buff', buf);
  531. });
  532. });
  533. });
  534. it('should receive events with several types of data (including binary)', function(done){
  535. var srv = http();
  536. var sio = io(srv);
  537. srv.listen(function(){
  538. var socket = client(srv);
  539. sio.on('connection', function(s){
  540. s.on('multiple', function(a, b, c, d, e, f){
  541. expect(a).to.be(1);
  542. expect(Buffer.isBuffer(b)).to.be(true);
  543. expect(c).to.be('3');
  544. expect(d).to.eql([4]);
  545. expect(Buffer.isBuffer(e)).to.be(true);
  546. expect(Buffer.isBuffer(f[0])).to.be(true);
  547. expect(f[1]).to.be('swag');
  548. expect(Buffer.isBuffer(f[2])).to.be(true);
  549. done();
  550. });
  551. fs.readFile(join(__dirname, 'support', 'doge.jpg'), function(err, data){
  552. if (err) return done(err);
  553. var buf = new Buffer('asdfasdf', 'utf8');
  554. socket.emit('multiple', 1, data, '3', [4], buf, [data, 'swag', buf]);
  555. });
  556. });
  557. });
  558. });
  559. it('should emit message events through `send`', function(done){
  560. var srv = http();
  561. var sio = io(srv);
  562. srv.listen(function(){
  563. var socket = client(srv);
  564. socket.on('message', function(a){
  565. expect(a).to.be('a');
  566. done();
  567. });
  568. sio.on('connection', function(s){
  569. s.send('a');
  570. });
  571. });
  572. });
  573. it('should receive event with callbacks', function(done){
  574. var srv = http();
  575. var sio = io(srv);
  576. srv.listen(function(){
  577. var socket = client(srv);
  578. sio.on('connection', function(s){
  579. s.on('woot', function(fn){
  580. fn(1, 2);
  581. });
  582. socket.emit('woot', function(a, b){
  583. expect(a).to.be(1);
  584. expect(b).to.be(2);
  585. done();
  586. });
  587. });
  588. });
  589. });
  590. it('should receive all events emitted from namespaced client immediately and in order', function(done) {
  591. var srv = http();
  592. var sio = io(srv);
  593. var total = 0;
  594. srv.listen(function(){
  595. sio.of('/chat', function(s){
  596. s.on('hi', function(letter){
  597. total++;
  598. if (total == 2 && letter == 'b') {
  599. done();
  600. } else if (total == 1 && letter != 'a') {
  601. throw new Error('events out of order');
  602. }
  603. });
  604. });
  605. var chat = client(srv, '/chat');
  606. chat.emit('hi', 'a');
  607. setTimeout(function() {
  608. chat.emit('hi', 'b');
  609. }, 50);
  610. });
  611. });
  612. it('should emit events with callbacks', function(done){
  613. var srv = http();
  614. var sio = io(srv);
  615. srv.listen(function(){
  616. var socket = client(srv);
  617. sio.on('connection', function(s){
  618. socket.on('hi', function(fn){
  619. fn();
  620. });
  621. s.emit('hi', function(){
  622. done();
  623. });
  624. });
  625. });
  626. });
  627. it('should receive events with args and callback', function(done){
  628. var srv = http();
  629. var sio = io(srv);
  630. srv.listen(function(){
  631. var socket = client(srv);
  632. sio.on('connection', function(s){
  633. s.on('woot', function(a, b, fn){
  634. expect(a).to.be(1);
  635. expect(b).to.be(2);
  636. fn();
  637. });
  638. socket.emit('woot', 1, 2, function(){
  639. done();
  640. });
  641. });
  642. });
  643. });
  644. it('should emit events with args and callback', function(done){
  645. var srv = http();
  646. var sio = io(srv);
  647. srv.listen(function(){
  648. var socket = client(srv);
  649. sio.on('connection', function(s){
  650. socket.on('hi', function(a, b, fn){
  651. expect(a).to.be(1);
  652. expect(b).to.be(2);
  653. fn();
  654. });
  655. s.emit('hi', 1, 2, function(){
  656. done();
  657. });
  658. });
  659. });
  660. });
  661. it('should receive events with binary args and callbacks', function(done) {
  662. var srv = http();
  663. var sio = io(srv);
  664. srv.listen(function(){
  665. var socket = client(srv);
  666. sio.on('connection', function(s){
  667. s.on('woot', function(buf, fn){
  668. expect(Buffer.isBuffer(buf)).to.be(true);
  669. fn(1, 2);
  670. });
  671. socket.emit('woot', new Buffer(3), function(a, b){
  672. expect(a).to.be(1);
  673. expect(b).to.be(2);
  674. done();
  675. });
  676. });
  677. });
  678. });
  679. it('should emit events with binary args and callback', function(done){
  680. var srv = http();
  681. var sio = io(srv);
  682. srv.listen(function(){
  683. var socket = client(srv);
  684. sio.on('connection', function(s){
  685. socket.on('hi', function(a, fn){
  686. expect(Buffer.isBuffer(a)).to.be(true);
  687. fn();
  688. });
  689. s.emit('hi', new Buffer(4), function(){
  690. done();
  691. });
  692. });
  693. });
  694. });
  695. it('should emit events and receive binary data in a callback', function(done) {
  696. var srv = http();
  697. var sio = io(srv);
  698. srv.listen(function(){
  699. var socket = client(srv);
  700. sio.on('connection', function(s){
  701. socket.on('hi', function(fn){
  702. fn(new Buffer(1));
  703. });
  704. s.emit('hi', function(a){
  705. expect(Buffer.isBuffer(a)).to.be(true);
  706. done();
  707. });
  708. });
  709. });
  710. });
  711. it('should receive events and pass binary data in a callback', function(done) {
  712. var srv = http();
  713. var sio = io(srv);
  714. srv.listen(function(){
  715. var socket = client(srv);
  716. sio.on('connection', function(s){
  717. s.on('woot', function(fn){
  718. fn(new Buffer(2));
  719. });
  720. socket.emit('woot', function(a){
  721. expect(Buffer.isBuffer(a)).to.be(true);
  722. done();
  723. });
  724. });
  725. });
  726. });
  727. it('should have access to the client', function(done){
  728. var srv = http();
  729. var sio = io(srv);
  730. srv.listen(function(){
  731. var socket = client(srv);
  732. sio.on('connection', function(s){
  733. expect(s.client).to.be.an('object');
  734. done();
  735. });
  736. });
  737. });
  738. it('should have access to the connection', function(done){
  739. var srv = http();
  740. var sio = io(srv);
  741. srv.listen(function(){
  742. var socket = client(srv);
  743. sio.on('connection', function(s){
  744. expect(s.client.conn).to.be.an('object');
  745. expect(s.conn).to.be.an('object');
  746. done();
  747. });
  748. });
  749. });
  750. it('should have access to the request', function(done){
  751. var srv = http();
  752. var sio = io(srv);
  753. srv.listen(function(){
  754. var socket = client(srv);
  755. sio.on('connection', function(s){
  756. expect(s.client.request.headers).to.be.an('object');
  757. expect(s.request.headers).to.be.an('object');
  758. done();
  759. });
  760. });
  761. });
  762. it('should see query parameters in the request', function(done) {
  763. var srv = http();
  764. var sio = io(srv);
  765. srv.listen(function() {
  766. var addr = srv.listen().address();
  767. var url = 'ws://' + addr.address + ':' + addr.port + '?key1=1&key2=2';
  768. var socket = ioc(url);
  769. sio.on('connection', function(s) {
  770. var parsed = require('url').parse(s.request.url);
  771. var query = require('querystring').parse(parsed.query);
  772. expect(query.key1).to.be('1');
  773. expect(query.key2).to.be('2');
  774. done();
  775. });
  776. });
  777. });
  778. it('should handle very large json', function(done){
  779. this.timeout();
  780. var srv = http();
  781. var sio = io(srv);
  782. var received = 0;
  783. srv.listen(function(){
  784. var socket = client(srv);
  785. socket.on('big', function(a){
  786. expect(Buffer.isBuffer(a.json)).to.be(false);
  787. if (++received == 3)
  788. done();
  789. else
  790. socket.emit('big', a);
  791. });
  792. sio.on('connection', function(s){
  793. fs.readFile(join(__dirname, 'fixtures', 'big.json'), function(err, data){
  794. if (err) return done(err);
  795. data = JSON.parse(data);
  796. s.emit('big', {hello: 'friend', json: data});
  797. });
  798. s.on('big', function(a){
  799. s.emit('big', a);
  800. });
  801. });
  802. });
  803. });
  804. it('should handle very large binary data', function(done){
  805. var srv = http();
  806. var sio = io(srv);
  807. var received = 0;
  808. srv.listen(function(){
  809. var socket = client(srv);
  810. socket.on('big', function(a){
  811. expect(Buffer.isBuffer(a.image)).to.be(true);
  812. if (++received == 3)
  813. done();
  814. else
  815. socket.emit('big', a);
  816. });
  817. sio.on('connection', function(s){
  818. fs.readFile(join(__dirname, 'fixtures', 'big.jpg'), function(err, data){
  819. if (err) return done(err);
  820. s.emit('big', {hello: 'friend', image: data});
  821. });
  822. s.on('big', function(a){
  823. expect(Buffer.isBuffer(a.image)).to.be(true);
  824. s.emit('big', a);
  825. });
  826. });
  827. });
  828. });
  829. });
  830. describe('messaging many', function(){
  831. it('emits to a namespace', function(done){
  832. var srv = http();
  833. var sio = io(srv);
  834. var total = 2;
  835. srv.listen(function(){
  836. var socket1 = client(srv, { multiplex: false });
  837. var socket2 = client(srv, { multiplex: false });
  838. var socket3 = client(srv, '/test');
  839. socket1.on('a', function(a){
  840. expect(a).to.be('b');
  841. --total || done();
  842. });
  843. socket2.on('a', function(a){
  844. expect(a).to.be('b');
  845. --total || done();
  846. });
  847. socket3.on('a', function(){ done(new Error('not')); });
  848. var sockets = 3;
  849. sio.on('connection', function(socket){
  850. --sockets || emit();
  851. });
  852. sio.of('/test', function(socket){
  853. --sockets || emit();
  854. });
  855. function emit(){
  856. sio.emit('a', 'b');
  857. }
  858. });
  859. });
  860. it('emits binary data to a namespace', function(done){
  861. var srv = http();
  862. var sio = io(srv);
  863. var total = 2;
  864. srv.listen(function(){
  865. var socket1 = client(srv, { multiplex: false });
  866. var socket2 = client(srv, { multiplex: false });
  867. var socket3 = client(srv, '/test');
  868. socket1.on('bin', function(a){
  869. expect(Buffer.isBuffer(a)).to.be(true);
  870. --total || done();
  871. });
  872. socket2.on('bin', function(a){
  873. expect(Buffer.isBuffer(a)).to.be(true);
  874. --total || done();
  875. });
  876. socket3.on('bin', function(){ done(new Error('not')); });
  877. var sockets = 3;
  878. sio.on('connection', function(socket){
  879. --sockets || emit();
  880. });
  881. sio.of('/test', function(socket){
  882. --sockets || emit();
  883. });
  884. function emit(){
  885. sio.emit('bin', new Buffer(10));
  886. }
  887. });
  888. });
  889. it('emits to the rest', function(done){
  890. var srv = http();
  891. var sio = io(srv);
  892. var total = 2;
  893. srv.listen(function(){
  894. var socket1 = client(srv, { multiplex: false });
  895. var socket2 = client(srv, { multiplex: false });
  896. var socket3 = client(srv, '/test');
  897. socket1.on('a', function(a){
  898. expect(a).to.be('b');
  899. socket1.emit('finish');
  900. });
  901. socket2.emit('broadcast');
  902. socket2.on('a', function(){ done(new Error('done')); });
  903. socket3.on('a', function(){ done(new Error('not')); });
  904. var sockets = 2;
  905. sio.on('connection', function(socket){
  906. socket.on('broadcast', function(){
  907. socket.broadcast.emit('a', 'b');
  908. });
  909. socket.on('finish', function(){
  910. done();
  911. });
  912. });
  913. });
  914. });
  915. it('emits to rooms', function(done){
  916. var srv = http();
  917. var sio = io(srv);
  918. var total = 2;
  919. srv.listen(function(){
  920. var socket1 = client(srv, { multiplex: false });
  921. var socket2 = client(srv, { multiplex: false });
  922. socket2.on('a', function(){
  923. done(new Error('not'));
  924. });
  925. socket1.on('a', function(){
  926. done();
  927. });
  928. socket1.emit('join', 'woot', function(){
  929. socket1.emit('emit', 'woot');
  930. });
  931. sio.on('connection', function(socket){
  932. socket.on('join', function(room, fn){
  933. socket.join(room, fn);
  934. });
  935. socket.on('emit', function(room){
  936. sio.in(room).emit('a');
  937. });
  938. });
  939. });
  940. });
  941. it('emits to rooms avoiding dupes', function(done){
  942. var srv = http();
  943. var sio = io(srv);
  944. var total = 2;
  945. srv.listen(function(){
  946. var socket1 = client(srv, { multiplex: false });
  947. var socket2 = client(srv, { multiplex: false });
  948. socket2.on('a', function(){
  949. done(new Error('not'));
  950. });
  951. socket1.on('a', function(){
  952. --total || done();
  953. });
  954. socket2.on('b', function(){
  955. --total || done();
  956. });
  957. socket1.emit('join', 'woot');
  958. socket1.emit('join', 'test');
  959. socket2.emit('join', 'third', function(){
  960. socket2.emit('emit');
  961. });
  962. sio.on('connection', function(socket){
  963. socket.on('join', function(room, fn){
  964. socket.join(room, fn);
  965. });
  966. socket.on('emit', function(room){
  967. sio.in('woot').in('test').emit('a');
  968. sio.in('third').emit('b');
  969. });
  970. });
  971. });
  972. });
  973. it('broadcasts to rooms', function(done){
  974. var srv = http();
  975. var sio = io(srv);
  976. var total = 2;
  977. srv.listen(function(){
  978. var socket1 = client(srv, { multiplex: false });
  979. var socket2 = client(srv, { multiplex: false });
  980. var socket3 = client(srv, { multiplex: false });
  981. socket1.emit('join', 'woot');
  982. socket2.emit('join', 'test');
  983. socket3.emit('join', 'test', function(){
  984. socket3.emit('broadcast');
  985. });
  986. socket1.on('a', function(){
  987. done(new Error('not'));
  988. });
  989. socket2.on('a', function(){
  990. --total || done();
  991. });
  992. socket3.on('a', function(){
  993. done(new Error('not'));
  994. });
  995. socket3.on('b', function(){
  996. --total || done();
  997. });
  998. sio.on('connection', function(socket){
  999. socket.on('join', function(room, fn){
  1000. socket.join(room, fn);
  1001. });
  1002. socket.on('broadcast', function(){
  1003. socket.broadcast.to('test').emit('a');
  1004. socket.emit('b');
  1005. });
  1006. });
  1007. });
  1008. });
  1009. it('broadcasts binary data to rooms', function(done){
  1010. var srv = http();
  1011. var sio = io(srv);
  1012. var total = 2;
  1013. srv.listen(function(){
  1014. var socket1 = client(srv, { multiplex: false });
  1015. var socket2 = client(srv, { multiplex: false });
  1016. var socket3 = client(srv, { multiplex: false });
  1017. socket1.emit('join', 'woot');
  1018. socket2.emit('join', 'test');
  1019. socket3.emit('join', 'test', function(){
  1020. socket3.emit('broadcast');
  1021. });
  1022. socket1.on('bin', function(data){
  1023. throw new Error('got bin in socket1');
  1024. });
  1025. socket2.on('bin', function(data){
  1026. expect(Buffer.isBuffer(data)).to.be(true);
  1027. --total || done();
  1028. });
  1029. socket2.on('bin2', function(data) {
  1030. throw new Error('socket2 got bin2');
  1031. });
  1032. socket3.on('bin', function(data) {
  1033. throw new Error('socket3 got bin');
  1034. });
  1035. socket3.on('bin2', function(data) {
  1036. expect(Buffer.isBuffer(data)).to.be(true);
  1037. --total || done();
  1038. });
  1039. sio.on('connection', function(socket){
  1040. socket.on('join', function(room, fn){
  1041. socket.join(room, fn);
  1042. });
  1043. socket.on('broadcast', function(){
  1044. socket.broadcast.to('test').emit('bin', new Buffer(5));
  1045. socket.emit('bin2', new Buffer(5));
  1046. });
  1047. });
  1048. });
  1049. });
  1050. it('keeps track of rooms', function(done){
  1051. var srv = http();
  1052. var sio = io(srv);
  1053. srv.listen(function(){
  1054. var socket = client(srv);
  1055. sio.on('connection', function(s){
  1056. s.join('a', function(){
  1057. expect(s.rooms).to.eql([s.id, 'a']);
  1058. s.join('b', function(){
  1059. expect(s.rooms).to.eql([s.id, 'a', 'b']);
  1060. s.leave('b', function(){
  1061. expect(s.rooms).to.eql([s.id, 'a']);
  1062. done();
  1063. });
  1064. });
  1065. });
  1066. });
  1067. });
  1068. });
  1069. });
  1070. describe('middleware', function(done){
  1071. var Socket = require('../lib/socket');
  1072. it('should call functions', function(done){
  1073. var srv = http();
  1074. var sio = io(srv);
  1075. var run = 0;
  1076. sio.use(function(socket, next){
  1077. expect(socket).to.be.a(Socket);
  1078. run++;
  1079. next();
  1080. });
  1081. sio.use(function(socket, next){
  1082. expect(socket).to.be.a(Socket);
  1083. run++;
  1084. next();
  1085. });
  1086. srv.listen(function(){
  1087. var socket = client(srv);
  1088. socket.on('connect', function(){
  1089. expect(run).to.be(2);
  1090. done();
  1091. });
  1092. });
  1093. });
  1094. it('should pass errors', function(done){
  1095. var srv = http();
  1096. var sio = io(srv);
  1097. var run = 0;
  1098. sio.use(function(socket, next){
  1099. next(new Error('Authentication error'));
  1100. });
  1101. sio.use(function(socket, next){
  1102. done(new Error('nope'));
  1103. });
  1104. srv.listen(function(){
  1105. var socket = client(srv);
  1106. socket.on('connect', function(){
  1107. done(new Error('nope'));
  1108. });
  1109. socket.on('error', function(err){
  1110. expect(err).to.be('Authentication error');
  1111. done();
  1112. });
  1113. });
  1114. });
  1115. it('should pass `data` of error object', function(done){
  1116. var srv = http();
  1117. var sio = io(srv);
  1118. var run = 0;
  1119. sio.use(function(socket, next){
  1120. var err = new Error('Authentication error');
  1121. err.data = { a: 'b', c: 3 };
  1122. next(err);
  1123. });
  1124. srv.listen(function(){
  1125. var socket = client(srv);
  1126. socket.on('connect', function(){
  1127. done(new Error('nope'));
  1128. });
  1129. socket.on('error', function(err){
  1130. expect(err).to.eql({ a: 'b', c: 3 });
  1131. done();
  1132. });
  1133. });
  1134. });
  1135. it('should only call connection after fns', function(done){
  1136. var srv = http();
  1137. var sio = io(srv);
  1138. sio.use(function(socket, next){
  1139. socket.name = 'guillermo';
  1140. next();
  1141. });
  1142. srv.listen(function(){
  1143. var socket = client(srv);
  1144. sio.on('connection', function(socket){
  1145. expect(socket.name).to.be('guillermo');
  1146. done();
  1147. });
  1148. });
  1149. });
  1150. it('should be ignored if socket gets closed', function(done){
  1151. var srv = http();
  1152. var sio = io(srv);
  1153. var socket;
  1154. sio.use(function(s, next){
  1155. socket.io.engine.on('open', function(){
  1156. socket.io.engine.close();
  1157. s.client.conn.on('close', function(){
  1158. process.nextTick(next);
  1159. setTimeout(function(){
  1160. done();
  1161. }, 50);
  1162. });
  1163. });
  1164. });
  1165. srv.listen(function(){
  1166. socket = client(srv);
  1167. sio.on('connection', function(socket){
  1168. done(new Error('should not fire'));
  1169. });
  1170. });
  1171. });
  1172. it('should call functions in expected order', function(done){
  1173. var srv = http();
  1174. var sio = io(srv);
  1175. var result = [];
  1176. sio.use(function(socket, next) {
  1177. result.push(1);
  1178. setTimeout(next, 50);
  1179. });
  1180. sio.use(function(socket, next) {
  1181. result.push(2);
  1182. setTimeout(next, 50);
  1183. });
  1184. sio.of('/chat').use(function(socket, next) {
  1185. result.push(3);
  1186. setTimeout(next, 50);
  1187. });
  1188. sio.of('/chat').use(function(socket, next) {
  1189. result.push(4);
  1190. setTimeout(next, 50);
  1191. });
  1192. srv.listen(function() {
  1193. var chat = client(srv, '/chat');
  1194. chat.on('connect', function() {
  1195. expect(result).to.eql([1, 2, 3, 4]);
  1196. done();
  1197. });
  1198. });
  1199. });
  1200. });
  1201. });