PageRenderTime 71ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/socket.io/test/transports.websocket.test.js

https://bitbucket.org/odle3536/480-secure-chat
JavaScript | 1891 lines | 1616 code | 260 blank | 15 comment | 159 complexity | b8e86b3ed6021cdcf0ac123dd3a8d707 MD5 | raw file
Possible License(s): MIT, BSD-3-Clause
  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. /**
  7. * Test dependencies.
  8. */
  9. var sio = require('../')
  10. , should = require('./common')
  11. , parser = sio.parser
  12. , ports = 15800;
  13. /**
  14. * Tests.
  15. */
  16. module.exports = {
  17. 'websocket identifies as websocket': function (done) {
  18. var cl = client(++ports)
  19. , io = create(cl)
  20. , ws;
  21. io.set('transports', ['websocket']);
  22. io.on('connection', function (socket) {
  23. socket.manager.transports[socket.id].name.should.equal('websocket');
  24. ws.finishClose();
  25. cl.end();
  26. io.server.close();
  27. done();
  28. });
  29. cl.handshake(function (sid) {
  30. ws = websocket(cl, sid);
  31. });
  32. },
  33. 'default websocket draft parser is used for unknown sec-websocket-version': function (done) {
  34. var cl = client(++ports)
  35. , io = create(cl)
  36. , ws;
  37. io.set('transports', ['websocket']);
  38. io.sockets.on('connection', function (socket) {
  39. ws.finishClose();
  40. cl.end();
  41. io.server.close();
  42. done();
  43. });
  44. cl.handshake(function (sid) {
  45. ws = websocket(cl, sid);
  46. });
  47. },
  48. 'hybi-07-12 websocket draft parser is used for sec-websocket-version: 8': function (done) {
  49. var cl = client(++ports)
  50. , io = create(cl);
  51. io.set('transports', ['websocket']);
  52. io.sockets.on('connection', function (socket) {
  53. cl.end();
  54. io.server.close();
  55. done();
  56. });
  57. var headers = {
  58. 'sec-websocket-version': 8,
  59. 'upgrade': 'websocket',
  60. 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
  61. }
  62. cl.get('/socket.io/{protocol}', {}, function (res, data) {
  63. var sid = data.split(':')[0];
  64. var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
  65. cl.get(url, {headers: headers}, function (res, data) {});
  66. });
  67. },
  68. 'hybi-16 websocket draft parser is used for sec-websocket-version: 13': function (done) {
  69. var cl = client(++ports)
  70. , io = create(cl)
  71. io.set('transports', ['websocket']);
  72. io.sockets.on('connection', function (socket) {
  73. cl.end();
  74. io.server.close();
  75. done();
  76. });
  77. var headers = {
  78. 'sec-websocket-version': 13,
  79. 'upgrade': 'websocket',
  80. 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
  81. }
  82. cl.get('/socket.io/{protocol}', {}, function (res, data) {
  83. var sid = data.split(':')[0];
  84. var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
  85. cl.get(url, {headers: headers}, function (res, data) {});
  86. });
  87. },
  88. 'hybi-07-12 origin filter blocks access for mismatched sec-websocket-origin': function (done) {
  89. var cl = client(++ports)
  90. , io = create(cl)
  91. io.set('transports', ['websocket']);
  92. io.set('origins', 'foo.bar.com:*');
  93. var notConnected = true;
  94. io.sockets.on('connection', function() {
  95. notConnected = false;
  96. });
  97. var headers = {
  98. 'sec-websocket-version': 8,
  99. 'upgrade': 'websocket',
  100. 'Sec-WebSocket-Origin': 'http://baz.bar.com',
  101. 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
  102. }
  103. // handshake uses correct origin -- we want to block the actual websocket call
  104. cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) {
  105. var sid = data.split(':')[0];
  106. var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
  107. var req = cl.get(url, {headers: headers}, function (res, data) {});
  108. var closed = false;
  109. (req.socket || req).on('close', function() {
  110. if (closed) return;
  111. closed = true;
  112. notConnected.should.be.true;
  113. cl.end();
  114. try {
  115. io.server.close();
  116. }
  117. catch (e) {}
  118. done();
  119. });
  120. });
  121. },
  122. 'hybi-16 origin filter blocks access for mismatched sec-websocket-origin': function (done) {
  123. var cl = client(++ports)
  124. , io = create(cl);
  125. io.set('transports', ['websocket']);
  126. io.set('origins', 'foo.bar.com:*');
  127. var notConnected = true;
  128. io.sockets.on('connection', function() {
  129. notConnected = false;
  130. });
  131. var headers = {
  132. 'sec-websocket-version': 13,
  133. 'upgrade': 'websocket',
  134. 'origin': 'http://baz.bar.com',
  135. 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
  136. }
  137. // handshake uses correct origin -- we want to block the actual websocket call
  138. cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) {
  139. var sid = data.split(':')[0];
  140. var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
  141. var req = cl.get(url, {headers: headers}, function (res, data) {});
  142. var closed = false;
  143. (req.socket || req).on('close', function() {
  144. if (closed) return;
  145. closed = true;
  146. notConnected.should.be.true;
  147. cl.end();
  148. try {
  149. io.server.close();
  150. }
  151. catch (e) {}
  152. done();
  153. });
  154. });
  155. },
  156. 'hybi-07-12 origin filter accepts implicit port 80 for sec-websocket-origin': function (done) {
  157. done();return;
  158. var cl = client(++ports)
  159. , io = create(cl)
  160. io.set('transports', ['websocket']);
  161. io.set('origins', 'foo.bar.com:80');
  162. var headers = {
  163. 'sec-websocket-version': 8,
  164. 'upgrade': 'websocket',
  165. 'Sec-WebSocket-Origin': 'http://foo.bar.com',
  166. 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
  167. }
  168. io.sockets.on('connection', function() {
  169. cl.end();
  170. io.server.close();
  171. done();
  172. });
  173. // handshake uses correct origin -- we want to block the actual websocket call
  174. cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) {
  175. var sid = data.split(':')[0];
  176. var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
  177. cl.get(url, {headers: headers}, function (res, data) {});
  178. });
  179. },
  180. 'hybi-16 origin filter accepts implicit port 80 for sec-websocket-origin': function (done) {
  181. var cl = client(++ports)
  182. , io = create(cl)
  183. io.set('transports', ['websocket']);
  184. io.set('origins', 'foo.bar.com:80');
  185. var headers = {
  186. 'sec-websocket-version': 13,
  187. 'upgrade': 'websocket',
  188. 'origin': 'http://foo.bar.com',
  189. 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
  190. }
  191. io.sockets.on('connection', function() {
  192. cl.end();
  193. io.server.close();
  194. done();
  195. });
  196. // handshake uses correct origin -- we want to block the actual websocket call
  197. cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) {
  198. var sid = data.split(':')[0];
  199. var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
  200. cl.get(url, {headers: headers}, function (res, data) {});
  201. });
  202. },
  203. 'test that not responding to a heartbeat drops client': function (done) {
  204. var cl = client(++ports)
  205. , io = create(cl)
  206. , messages = 0
  207. , ws;
  208. io.configure(function () {
  209. io.set('heartbeat interval', .05);
  210. io.set('heartbeat timeout', .05);
  211. io.set('close timeout', 0);
  212. });
  213. io.sockets.on('connection', function (socket) {
  214. socket.on('disconnect', function (reason) {
  215. beat.should.be.true;
  216. reason.should.eql('heartbeat timeout');
  217. cl.end();
  218. ws.finishClose();
  219. io.server.close();
  220. done();
  221. });
  222. });
  223. cl.handshake(function (sid) {
  224. ws = websocket(cl, sid);
  225. ws.on('message', function (packet) {
  226. if (++messages == 1) {
  227. packet.type.should.eql('connect');
  228. } else {
  229. packet.type.should.eql('heartbeat');
  230. beat = true;
  231. }
  232. });
  233. });
  234. },
  235. 'test that responding to a heartbeat maintains session': function (done) {
  236. var cl = client(++ports)
  237. , io = create(cl)
  238. , messages = 0
  239. , heartbeats = 0
  240. , ws;
  241. io.configure(function () {
  242. io.set('heartbeat interval', .05);
  243. io.set('heartbeat timeout', .05);
  244. io.set('close timeout', 0);
  245. });
  246. io.sockets.on('connection', function (socket) {
  247. socket.on('disconnect', function (reason) {
  248. heartbeats.should.eql(2);
  249. reason.should.eql('heartbeat timeout');
  250. cl.end();
  251. ws.finishClose();
  252. io.server.close();
  253. done();
  254. });
  255. });
  256. cl.handshake(function (sid) {
  257. ws = websocket(cl, sid);
  258. ws.on('message', function (packet) {
  259. if (++messages == 1) {
  260. packet.type.should.eql('connect');
  261. } else {
  262. packet.type.should.eql('heartbeat');
  263. heartbeats++;
  264. if (heartbeats == 1) {
  265. ws.packet({ type: 'heartbeat' });
  266. }
  267. }
  268. });
  269. });
  270. },
  271. 'test sending deliverable volatile messages': function (done) {
  272. var cl = client(++ports)
  273. , io = create(cl)
  274. , messages = 0
  275. , messaged = false;
  276. io.configure(function () {
  277. io.set('close timeout', .05);
  278. });
  279. io.sockets.on('connection', function (socket) {
  280. socket.volatile.send('tobi');
  281. socket.on('disconnect', function () {
  282. messaged.should.be.true;
  283. cl.end();
  284. io.server.close();
  285. done();
  286. });
  287. });
  288. cl.handshake(function (sid) {
  289. var ws = websocket(cl, sid);
  290. ws.on('message', function (msg) {
  291. if (++messages == 1) {
  292. msg.type.should.eql('connect');
  293. } else {
  294. msg.should.eql({
  295. type: 'message'
  296. , data: 'tobi'
  297. , endpoint: ''
  298. });
  299. messaged = true;
  300. ws.finishClose();
  301. }
  302. });
  303. });
  304. },
  305. 'test sending deliverable volatile json': function (done) {
  306. var cl = client(++ports)
  307. , io = create(cl)
  308. , messaged = false;
  309. io.configure(function () {
  310. io.set('close timeout', .05);
  311. });
  312. io.sockets.on('connection', function (socket) {
  313. socket.volatile.json.send([1, 2, 3]);
  314. socket.on('disconnect', function () {
  315. messaged.should.be.true;
  316. cl.end();
  317. io.server.close();
  318. done();
  319. });
  320. });
  321. cl.handshake(function (sid) {
  322. var ws = websocket(cl, sid);
  323. ws.on('message', function (msg) {
  324. if (!ws.connected) {
  325. msg.type.should.eql('connect');
  326. ws.connected = true;
  327. } else {
  328. msg.should.eql({
  329. type: 'json'
  330. , data: [1, 2, 3]
  331. , endpoint: ''
  332. });
  333. messaged = true;
  334. ws.finishClose();
  335. }
  336. });
  337. });
  338. },
  339. 'test sending deliverable volatile events': function (done) {
  340. var cl = client(++ports)
  341. , io = create(cl)
  342. , messaged = false;
  343. io.configure(function () {
  344. io.set('close timeout', .05);
  345. });
  346. io.sockets.on('connection', function (socket) {
  347. socket.volatile.emit('tobi');
  348. socket.on('disconnect', function () {
  349. messaged.should.be.true;
  350. cl.end();
  351. io.server.close();
  352. done();
  353. });
  354. });
  355. cl.handshake(function (sid) {
  356. var ws = websocket(cl, sid);
  357. ws.on('message', function (msg) {
  358. if (!ws.connected) {
  359. msg.type.should.eql('connect');
  360. ws.connected = true;
  361. } else {
  362. msg.should.eql({
  363. type: 'event'
  364. , name: 'tobi'
  365. , endpoint: ''
  366. , args: []
  367. });
  368. messaged = true;
  369. ws.finishClose();
  370. }
  371. });
  372. });
  373. },
  374. 'test sending to all clients in a namespace': function (done) {
  375. var port = ++ports
  376. , cl1 = client(port)
  377. , cl2 = client(port)
  378. , io = create(cl1)
  379. , messages = 0
  380. , connections = 0
  381. , disconnections = 0;
  382. io.configure(function () {
  383. io.set('close timeout', 0);
  384. });
  385. io.sockets.on('connection', function (socket) {
  386. connections++;
  387. if (connections == 2) {
  388. io.sockets.send('yup');
  389. }
  390. socket.on('disconnect', function () {
  391. disconnections++;
  392. if (disconnections == 2) {
  393. messages.should.eql(2);
  394. cl1.end();
  395. cl2.end();
  396. io.server.close();
  397. done();
  398. }
  399. });
  400. });
  401. cl1.handshake(function (sid) {
  402. var ws1 = websocket(cl1, sid);
  403. ws1.on('message', function (msg) {
  404. if (!ws1.connected) {
  405. msg.type.should.eql('connect');
  406. ws1.connected = true;
  407. } else {
  408. msg.should.eql({
  409. type: 'message'
  410. , data: 'yup'
  411. , endpoint: ''
  412. });
  413. messages++;
  414. ws1.finishClose();
  415. }
  416. });
  417. });
  418. cl2.handshake(function (sid) {
  419. var ws2 = websocket(cl2, sid);
  420. ws2.on('message', function (msg) {
  421. if (!ws2.connected) {
  422. msg.type.should.eql('connect');
  423. ws2.connected = true;
  424. } else {
  425. msg.should.eql({
  426. type: 'message'
  427. , data: 'yup'
  428. , endpoint: ''
  429. });
  430. messages++;
  431. ws2.finishClose();
  432. }
  433. });
  434. });
  435. },
  436. 'test sending json to all clients in a namespace': function (done) {
  437. var port = ++ports
  438. , cl1 = client(port)
  439. , cl2 = client(port)
  440. , io = create(cl1)
  441. , messages = 0
  442. , connections = 0
  443. , disconnections = 0;
  444. io.configure(function () {
  445. io.set('close timeout', 0);
  446. });
  447. io.sockets.on('connection', function (socket) {
  448. connections++;
  449. if (connections == 2) {
  450. io.sockets.json.send({ a: 'b' });
  451. }
  452. socket.on('disconnect', function () {
  453. disconnections++;
  454. if (disconnections == 2) {
  455. messages.should.eql(2);
  456. cl1.end();
  457. cl2.end();
  458. io.server.close();
  459. done();
  460. }
  461. });
  462. });
  463. cl1.handshake(function (sid) {
  464. var ws1 = websocket(cl1, sid);
  465. ws1.on('message', function (msg) {
  466. if (!ws1.connected) {
  467. msg.type.should.eql('connect');
  468. ws1.connected = true;
  469. } else {
  470. msg.should.eql({
  471. type: 'json'
  472. , data: { a: 'b' }
  473. , endpoint: ''
  474. });
  475. messages++;
  476. ws1.finishClose();
  477. }
  478. });
  479. });
  480. cl2.handshake(function (sid) {
  481. var ws2 = websocket(cl2, sid);
  482. ws2.on('message', function (msg) {
  483. if (!ws2.connected) {
  484. msg.type.should.eql('connect');
  485. ws2.connected = true;
  486. } else {
  487. msg.should.eql({
  488. type: 'json'
  489. , data: { a: 'b' }
  490. , endpoint: ''
  491. });
  492. messages++;
  493. ws2.finishClose();
  494. }
  495. });
  496. });
  497. },
  498. 'test emitting to all clients in a namespace': function (done) {
  499. var port = ++ports
  500. , cl1 = client(port)
  501. , cl2 = client(port)
  502. , io = create(cl1)
  503. , messages = 0
  504. , connections = 0
  505. , disconnections = 0;
  506. io.configure(function () {
  507. io.set('close timeout', 0);
  508. });
  509. io.sockets.on('connection', function (socket) {
  510. connections++;
  511. if (connections == 2) {
  512. io.sockets.emit('tobi', 'rapture');
  513. }
  514. socket.on('disconnect', function () {
  515. disconnections++;
  516. if (disconnections == 2) {
  517. messages.should.eql(2);
  518. cl1.end();
  519. cl2.end();
  520. io.server.close();
  521. done();
  522. }
  523. });
  524. });
  525. cl1.handshake(function (sid) {
  526. var ws1 = websocket(cl1, sid);
  527. ws1.on('message', function (msg) {
  528. if (!ws1.connected) {
  529. msg.type.should.eql('connect');
  530. ws1.connected = true;
  531. } else {
  532. msg.should.eql({
  533. type: 'event'
  534. , name: 'tobi'
  535. , args: ['rapture']
  536. , endpoint: ''
  537. });
  538. messages++;
  539. ws1.finishClose();
  540. }
  541. });
  542. });
  543. cl2.handshake(function (sid) {
  544. var ws2 = websocket(cl2, sid);
  545. ws2.on('message', function (msg) {
  546. if (!ws2.connected) {
  547. msg.type.should.eql('connect');
  548. ws2.connected = true;
  549. } else {
  550. msg.should.eql({
  551. type: 'event'
  552. , name: 'tobi'
  553. , args: ['rapture']
  554. , endpoint: ''
  555. });
  556. messages++;
  557. ws2.finishClose();
  558. }
  559. });
  560. });
  561. },
  562. 'test sending to all clients in a room': function (done) {
  563. var port = ++ports
  564. , cl1 = client(port)
  565. , cl2 = client(port)
  566. , cl3 = client(port)
  567. , io = create(cl1)
  568. , messages = 0
  569. , joins = 0
  570. , connections = 0
  571. , disconnections = 0;
  572. io.configure(function () {
  573. io.set('close timeout', 0);
  574. });
  575. io.sockets.on('connection', function (socket) {
  576. connections++;
  577. if (connections != 3) {
  578. socket.join('woot');
  579. joins++;
  580. if (joins == 2) {
  581. setTimeout(function () {
  582. connections.should.eql(3);
  583. io.sockets.in('woot').send('hahaha');
  584. }, 20);
  585. }
  586. }
  587. socket.on('disconnect', function () {
  588. disconnections++;
  589. if (disconnections == 3) {
  590. messages.should.eql(2);
  591. cl1.end();
  592. cl2.end();
  593. cl3.end();
  594. io.server.close();
  595. done();
  596. }
  597. });
  598. });
  599. cl1.handshake(function (sid) {
  600. var ws1 = websocket(cl1, sid);
  601. ws1.on('message', function (msg) {
  602. if (!ws1.connected) {
  603. msg.type.should.eql('connect');
  604. ws1.connected = true;
  605. } else {
  606. msg.should.eql({
  607. type: 'message'
  608. , data: 'hahaha'
  609. , endpoint: ''
  610. });
  611. messages++;
  612. }
  613. });
  614. setTimeout(function () {
  615. ws1.finishClose();
  616. }, 50);
  617. });
  618. cl2.handshake(function (sid) {
  619. var ws2 = websocket(cl2, sid);
  620. ws2.on('message', function (msg) {
  621. if (!ws2.connected) {
  622. msg.type.should.eql('connect');
  623. ws2.connected = true;
  624. } else {
  625. msg.should.eql({
  626. type: 'message'
  627. , data: 'hahaha'
  628. , endpoint: ''
  629. });
  630. messages++;
  631. }
  632. });
  633. setTimeout(function () {
  634. ws2.finishClose();
  635. }, 50);
  636. });
  637. cl3.handshake(function (sid) {
  638. var ws3 = websocket(cl3, sid);
  639. ws3.on('message', function (msg) {
  640. if (!ws3.connected) {
  641. msg.type.should.eql('connect');
  642. ws3.connected = true;
  643. } else {
  644. msg.should.eql({
  645. type: 'message'
  646. , data: 'hahaha'
  647. , endpoint: ''
  648. });
  649. messages++;
  650. }
  651. });
  652. setTimeout(function () {
  653. ws3.finishClose();
  654. }, 50);
  655. });
  656. },
  657. 'test sending json to all clients in a room': function (done) {
  658. var port = ++ports
  659. , cl1 = client(port)
  660. , cl2 = client(port)
  661. , cl3 = client(port)
  662. , io = create(cl1)
  663. , messages = 0
  664. , joins = 0
  665. , connections = 0
  666. , disconnections = 0;
  667. io.configure(function () {
  668. io.set('close timeout', 0);
  669. });
  670. io.sockets.on('connection', function (socket) {
  671. connections++;
  672. if (connections != 3) {
  673. socket.join('woot');
  674. joins++;
  675. if (joins == 2) {
  676. setTimeout(function () {
  677. connections.should.eql(3);
  678. io.sockets.in('woot').json.send(123);
  679. }, 20);
  680. }
  681. }
  682. socket.on('disconnect', function () {
  683. disconnections++;
  684. if (disconnections == 3) {
  685. messages.should.eql(2);
  686. cl1.end();
  687. cl2.end();
  688. cl3.end();
  689. io.server.close();
  690. done();
  691. }
  692. });
  693. });
  694. cl1.handshake(function (sid) {
  695. var ws1 = websocket(cl1, sid);
  696. ws1.on('message', function (msg) {
  697. if (!ws1.connected) {
  698. msg.type.should.eql('connect');
  699. ws1.connected = true;
  700. } else {
  701. msg.should.eql({
  702. type: 'json'
  703. , data: 123
  704. , endpoint: ''
  705. });
  706. messages++;
  707. }
  708. });
  709. setTimeout(function () {
  710. ws1.finishClose();
  711. }, 50);
  712. });
  713. cl2.handshake(function (sid) {
  714. var ws2 = websocket(cl2, sid);
  715. ws2.on('message', function (msg) {
  716. if (!ws2.connected) {
  717. msg.type.should.eql('connect');
  718. ws2.connected = true;
  719. } else {
  720. msg.should.eql({
  721. type: 'json'
  722. , data: 123
  723. , endpoint: ''
  724. });
  725. messages++;
  726. }
  727. });
  728. setTimeout(function () {
  729. ws2.finishClose();
  730. }, 50);
  731. });
  732. cl3.handshake(function (sid) {
  733. var ws3 = websocket(cl3, sid);
  734. ws3.on('message', function (msg) {
  735. if (!ws3.connected) {
  736. msg.type.should.eql('connect');
  737. ws3.connected = true;
  738. } else {
  739. msg.should.eql({
  740. type: 'json'
  741. , data: 123
  742. , endpoint: ''
  743. });
  744. messages++;
  745. }
  746. });
  747. setTimeout(function () {
  748. ws3.finishClose();
  749. }, 50);
  750. });
  751. },
  752. 'test emitting to all clients in a room': function (done) {
  753. var port = ++ports
  754. , cl1 = client(port)
  755. , cl2 = client(port)
  756. , cl3 = client(port)
  757. , io = create(cl1)
  758. , messages = 0
  759. , joins = 0
  760. , connections = 0
  761. , disconnections = 0;
  762. io.configure(function () {
  763. io.set('close timeout', 0);
  764. });
  765. io.sockets.on('connection', function (socket) {
  766. connections++;
  767. if (connections != 3) {
  768. socket.join('woot');
  769. joins++;
  770. if (joins == 2) {
  771. setTimeout(function () {
  772. connections.should.eql(3);
  773. io.sockets.in('woot').emit('locki');
  774. }, 20);
  775. }
  776. }
  777. socket.on('disconnect', function () {
  778. disconnections++;
  779. if (disconnections == 3) {
  780. messages.should.eql(2);
  781. cl1.end();
  782. cl2.end();
  783. cl3.end();
  784. io.server.close();
  785. done();
  786. }
  787. });
  788. });
  789. cl1.handshake(function (sid) {
  790. var ws1 = websocket(cl1, sid);
  791. ws1.on('message', function (msg) {
  792. if (!ws1.connected) {
  793. msg.type.should.eql('connect');
  794. ws1.connected = true;
  795. } else {
  796. msg.should.eql({
  797. type: 'event'
  798. , name: 'locki'
  799. , args: []
  800. , endpoint: ''
  801. });
  802. messages++;
  803. }
  804. });
  805. setTimeout(function () {
  806. ws1.finishClose();
  807. }, 50);
  808. });
  809. cl2.handshake(function (sid) {
  810. var ws2 = websocket(cl2, sid);
  811. ws2.on('message', function (msg) {
  812. if (!ws2.connected) {
  813. msg.type.should.eql('connect');
  814. ws2.connected = true;
  815. } else {
  816. msg.should.eql({
  817. type: 'event'
  818. , name: 'locki'
  819. , args: []
  820. , endpoint: ''
  821. });
  822. messages++;
  823. }
  824. });
  825. setTimeout(function () {
  826. ws2.finishClose();
  827. }, 50);
  828. });
  829. cl3.handshake(function (sid) {
  830. var ws3 = websocket(cl3, sid);
  831. ws3.on('message', function (msg) {
  832. if (!ws3.connected) {
  833. msg.type.should.eql('connect');
  834. ws3.connected = true;
  835. } else {
  836. msg.should.eql({
  837. type: 'event'
  838. , name: 'locki'
  839. , args: []
  840. , endpoint: ''
  841. });
  842. messages++;
  843. }
  844. });
  845. setTimeout(function () {
  846. ws3.finishClose();
  847. }, 50);
  848. });
  849. },
  850. 'test leaving a room': function (done) {
  851. var port = ++ports
  852. , cl1 = client(port)
  853. , cl2 = client(port)
  854. , io = create(cl1)
  855. , joins = 0
  856. , disconnects = 0;
  857. io.set('close timeout', 0);
  858. io.sockets.on('connection', function (socket) {
  859. socket.join('foo');
  860. io.sockets.clients('foo').should.have.length(++joins);
  861. socket.on('disconnect', function () {
  862. socket.leave('foo');
  863. socket.leave('foo');
  864. socket.leave('foo');
  865. io.sockets.clients('foo').should.have.length(--joins);
  866. if (++disconnects == 2) {
  867. io.server.close();
  868. cl1.end();
  869. cl2.end();
  870. done();
  871. }
  872. })
  873. });
  874. cl1.handshake(function (sid) {
  875. var ws1 = websocket(cl1, sid);
  876. ws1.on('message', function (msg) {
  877. if (!ws1.connected) {
  878. msg.type.should.eql('connect');
  879. ws1.connected = true;
  880. ws1.finishClose();
  881. }
  882. });
  883. });
  884. cl2.handshake(function (sid) {
  885. var ws2 = websocket(cl2, sid);
  886. ws2.on('message', function (msg) {
  887. if (!ws2.connected) {
  888. msg.type.should.eql('connect');
  889. ws2.connected = true;
  890. ws2.finishClose();
  891. }
  892. });
  893. });
  894. },
  895. 'test message with broadcast flag': function (done) {
  896. var port = ++ports
  897. , cl1 = client(port)
  898. , cl2 = client(port)
  899. , cl3 = client(port)
  900. , io = create(cl1)
  901. , messages = 0
  902. , disconnections = 0;
  903. io.configure(function () {
  904. io.set('close timeout', 0);
  905. });
  906. io.sockets.on('connection', function (socket) {
  907. socket.on('trigger broadcast', function () {
  908. socket.broadcast.send('boom');
  909. });
  910. socket.on('disconnect', function () {
  911. disconnections++;
  912. if (disconnections == 3) {
  913. messages.should.eql(2);
  914. cl1.end();
  915. cl2.end();
  916. cl3.end();
  917. io.server.close();
  918. done();
  919. }
  920. });
  921. });
  922. cl1.handshake(function (sid) {
  923. var ws1 = websocket(cl1, sid);
  924. ws1.on('message', function (msg) {
  925. if (!ws1.connected) {
  926. msg.type.should.eql('connect');
  927. ws1.connected = true;
  928. } else {
  929. msg.should.eql({
  930. type: 'message'
  931. , data: 'boom'
  932. , endpoint: ''
  933. });
  934. messages++;
  935. ws1.finishClose();
  936. }
  937. });
  938. });
  939. cl2.handshake(function (sid) {
  940. var ws2 = websocket(cl2, sid);
  941. ws2.on('message', function (msg) {
  942. if (!ws2.connected) {
  943. msg.type.should.eql('connect');
  944. ws2.connected = true;
  945. } else {
  946. msg.should.eql({
  947. type: 'message'
  948. , data: 'boom'
  949. , endpoint: ''
  950. });
  951. messages++;
  952. ws2.finishClose();
  953. }
  954. });
  955. });
  956. cl3.handshake(function (sid) {
  957. var ws3 = websocket(cl3, sid);
  958. ws3.on('open', function () {
  959. ws3.packet({
  960. type: 'event'
  961. , name: 'trigger broadcast'
  962. , endpoint: ''
  963. });
  964. setTimeout(function () {
  965. ws3.finishClose();
  966. }, 50);
  967. });
  968. ws3.on('message', function (msg) {
  969. if (!ws3.connected) {
  970. msg.type.should.eql('connect');
  971. ws3.connected = true;
  972. } else {
  973. throw new Error('we shouldnt get a message here');
  974. }
  975. });
  976. });
  977. },
  978. 'test json with broadcast flag': function (done) {
  979. var port = ++ports
  980. , cl1 = client(port)
  981. , cl2 = client(port)
  982. , cl3 = client(port)
  983. , io = create(cl1)
  984. , messages = 0
  985. , disconnections = 0;
  986. io.configure(function () {
  987. io.set('close timeout', 0);
  988. });
  989. io.sockets.on('connection', function (socket) {
  990. socket.on('trigger broadcast', function () {
  991. socket.broadcast.json.send([1, 2, 3]);
  992. });
  993. socket.on('disconnect', function () {
  994. disconnections++;
  995. if (disconnections == 3) {
  996. messages.should.eql(2);
  997. cl1.end();
  998. cl2.end();
  999. cl3.end();
  1000. io.server.close();
  1001. done();
  1002. }
  1003. });
  1004. });
  1005. cl1.handshake(function (sid) {
  1006. var ws1 = websocket(cl1, sid);
  1007. ws1.on('message', function (msg) {
  1008. if (!ws1.connected) {
  1009. msg.type.should.eql('connect');
  1010. ws1.connected = true;
  1011. } else {
  1012. msg.should.eql({
  1013. type: 'json'
  1014. , data: [1, 2, 3]
  1015. , endpoint: ''
  1016. });
  1017. messages++;
  1018. ws1.finishClose();
  1019. }
  1020. });
  1021. });
  1022. cl2.handshake(function (sid) {
  1023. var ws2 = websocket(cl2, sid);
  1024. ws2.on('message', function (msg) {
  1025. if (!ws2.connected) {
  1026. msg.type.should.eql('connect');
  1027. ws2.connected = true;
  1028. } else {
  1029. msg.should.eql({
  1030. type: 'json'
  1031. , data: [1, 2, 3]
  1032. , endpoint: ''
  1033. });
  1034. messages++;
  1035. ws2.finishClose();
  1036. }
  1037. });
  1038. });
  1039. cl3.handshake(function (sid) {
  1040. var ws3 = websocket(cl3, sid);
  1041. ws3.on('open', function () {
  1042. ws3.packet({
  1043. type: 'event'
  1044. , name: 'trigger broadcast'
  1045. , endpoint: ''
  1046. });
  1047. setTimeout(function () {
  1048. ws3.finishClose();
  1049. }, 50);
  1050. });
  1051. ws3.on('message', function (msg) {
  1052. if (!ws3.connected) {
  1053. msg.type.should.eql('connect');
  1054. ws3.connected = true;
  1055. } else {
  1056. throw new Error('we shouldnt get a message here');
  1057. }
  1058. });
  1059. });
  1060. },
  1061. 'test event with broadcast flag': function (done) {
  1062. var port = ++ports
  1063. , cl1 = client(port)
  1064. , cl2 = client(port)
  1065. , cl3 = client(port)
  1066. , io = create(cl1)
  1067. , messages = 0
  1068. , disconnections = 0;
  1069. io.configure(function () {
  1070. io.set('close timeout', 0);
  1071. });
  1072. io.sockets.on('connection', function (socket) {
  1073. socket.on('trigger broadcast', function () {
  1074. socket.broadcast.emit('hey', 'arnold');
  1075. });
  1076. socket.on('disconnect', function () {
  1077. disconnections++;
  1078. if (disconnections == 3) {
  1079. messages.should.eql(2);
  1080. cl1.end();
  1081. cl2.end();
  1082. cl3.end();
  1083. io.server.close();
  1084. done();
  1085. }
  1086. });
  1087. });
  1088. cl1.handshake(function (sid) {
  1089. var ws1 = websocket(cl1, sid);
  1090. ws1.on('message', function (msg) {
  1091. if (!ws1.connected) {
  1092. msg.type.should.eql('connect');
  1093. ws1.connected = true;
  1094. } else {
  1095. msg.should.eql({
  1096. type: 'event'
  1097. , name: 'hey'
  1098. , args: ['arnold']
  1099. , endpoint: ''
  1100. });
  1101. messages++;
  1102. ws1.finishClose();
  1103. }
  1104. });
  1105. });
  1106. cl2.handshake(function (sid) {
  1107. var ws2 = websocket(cl2, sid);
  1108. ws2.on('message', function (msg) {
  1109. if (!ws2.connected) {
  1110. msg.type.should.eql('connect');
  1111. ws2.connected = true;
  1112. } else {
  1113. msg.should.eql({
  1114. type: 'event'
  1115. , name: 'hey'
  1116. , args: ['arnold']
  1117. , endpoint: ''
  1118. });
  1119. messages++;
  1120. ws2.finishClose();
  1121. }
  1122. });
  1123. });
  1124. cl3.handshake(function (sid) {
  1125. var ws3 = websocket(cl3, sid);
  1126. ws3.on('open', function () {
  1127. ws3.packet({
  1128. type: 'event'
  1129. , name: 'trigger broadcast'
  1130. , endpoint: ''
  1131. });
  1132. setTimeout(function () {
  1133. ws3.finishClose();
  1134. }, 50);
  1135. });
  1136. ws3.on('message', function (msg) {
  1137. if (!ws3.connected) {
  1138. msg.type.should.eql('connect');
  1139. ws3.connected = true;
  1140. } else {
  1141. throw new Error('we shouldnt get a message here');
  1142. }
  1143. });
  1144. });
  1145. },
  1146. 'test message with broadcast flag and to()': function (done) {
  1147. var port = ++ports
  1148. , cl1 = client(port)
  1149. , cl2 = client(port)
  1150. , cl3 = client(port)
  1151. , io = create(cl1)
  1152. , messages = 0
  1153. , connections = 0
  1154. , disconnections = 0;
  1155. io.configure(function () {
  1156. io.set('close timeout', 0);
  1157. });
  1158. io.sockets.on('connection', function (socket) {
  1159. connections++;
  1160. if (connections == 1) {
  1161. socket.join('losers');
  1162. }
  1163. socket.on('trigger broadcast', function () {
  1164. socket.broadcast.to('losers').send('boom');
  1165. });
  1166. socket.on('disconnect', function () {
  1167. disconnections++;
  1168. if (disconnections == 3) {
  1169. messages.should.eql(1);
  1170. cl1.end();
  1171. cl2.end();
  1172. cl3.end();
  1173. io.server.close();
  1174. done();
  1175. }
  1176. });
  1177. });
  1178. cl1.handshake(function (sid) {
  1179. var ws1 = websocket(cl1, sid);
  1180. ws1.on('message', function (msg) {
  1181. if (!ws1.connected) {
  1182. msg.type.should.eql('connect');
  1183. ws1.connected = true;
  1184. } else {
  1185. msg.should.eql({
  1186. type: 'message'
  1187. , data: 'boom'
  1188. , endpoint: ''
  1189. });
  1190. messages++;
  1191. }
  1192. });
  1193. ws1.on('open', function () {
  1194. cl2.handshake(function (sid) {
  1195. var ws2 = websocket(cl2, sid);
  1196. ws2.on('message', function (msg) {
  1197. if (!ws2.connected) {
  1198. msg.type.should.eql('connect');
  1199. ws2.connected = true;
  1200. } else {
  1201. throw new Error('This socket shouldnt get a message');
  1202. }
  1203. });
  1204. ws2.on('open', function () {
  1205. cl3.handshake(function (sid) {
  1206. var ws3 = websocket(cl3, sid);
  1207. ws3.on('open', function () {
  1208. ws3.packet({
  1209. type: 'event'
  1210. , name: 'trigger broadcast'
  1211. , endpoint: ''
  1212. });
  1213. setTimeout(function () {
  1214. ws1.finishClose();
  1215. ws2.finishClose();
  1216. ws3.finishClose();
  1217. }, 50);
  1218. });
  1219. ws3.on('message', function (msg) {
  1220. if (!ws3.connected) {
  1221. msg.type.should.eql('connect');
  1222. ws3.connected = true;
  1223. } else {
  1224. throw new Error('we shouldnt get a message here');
  1225. }
  1226. });
  1227. });
  1228. });
  1229. });
  1230. });
  1231. });
  1232. },
  1233. 'test json with broadcast flag and to()': function (done) {
  1234. var port = ++ports
  1235. , cl1 = client(port)
  1236. , cl2 = client(port)
  1237. , cl3 = client(port)
  1238. , io = create(cl1)
  1239. , messages = 0
  1240. , connections = 0
  1241. , disconnections = 0;
  1242. io.configure(function () {
  1243. io.set('close timeout', 0);
  1244. });
  1245. io.sockets.on('connection', function (socket) {
  1246. connections++;
  1247. if (connections == 1) {
  1248. socket.join('losers');
  1249. }
  1250. socket.on('trigger broadcast', function () {
  1251. socket.broadcast.json.to('losers').send({ hello: 'world' });
  1252. });
  1253. socket.on('disconnect', function () {
  1254. disconnections++;
  1255. if (disconnections == 3) {
  1256. messages.should.eql(1);
  1257. cl1.end();
  1258. cl2.end();
  1259. cl3.end();
  1260. io.server.close();
  1261. done();
  1262. }
  1263. });
  1264. });
  1265. cl1.handshake(function (sid) {
  1266. var ws1 = websocket(cl1, sid);
  1267. ws1.on('message', function (msg) {
  1268. if (!ws1.connected) {
  1269. msg.type.should.eql('connect');
  1270. ws1.connected = true;
  1271. } else {
  1272. msg.should.eql({
  1273. type: 'json'
  1274. , data: { hello: 'world' }
  1275. , endpoint: ''
  1276. });
  1277. messages++;
  1278. }
  1279. });
  1280. ws1.on('open', function () {
  1281. cl2.handshake(function (sid) {
  1282. var ws2 = websocket(cl2, sid);
  1283. ws2.on('message', function (msg) {
  1284. if (!ws2.connected) {
  1285. msg.type.should.eql('connect');
  1286. ws2.connected = true;
  1287. } else {
  1288. throw new Error('This socket shouldnt get a message');
  1289. }
  1290. });
  1291. ws2.on('open', function () {
  1292. cl3.handshake(function (sid) {
  1293. var ws3 = websocket(cl3, sid);
  1294. ws3.on('open', function () {
  1295. ws3.packet({
  1296. type: 'event'
  1297. , name: 'trigger broadcast'
  1298. , endpoint: ''
  1299. });
  1300. setTimeout(function () {
  1301. ws1.finishClose();
  1302. ws2.finishClose();
  1303. ws3.finishClose();
  1304. }, 50);
  1305. });
  1306. ws3.on('message', function (msg) {
  1307. if (!ws3.connected) {
  1308. msg.type.should.eql('connect');
  1309. ws3.connected = true;
  1310. } else {
  1311. throw new Error('we shouldnt get a message here');
  1312. }
  1313. });
  1314. });
  1315. });
  1316. });
  1317. });
  1318. });
  1319. },
  1320. 'test event with broadcast flag and to()': function (done) {
  1321. var port = ++ports
  1322. , cl1 = client(port)
  1323. , cl2 = client(port)
  1324. , cl3 = client(port)
  1325. , io = create(cl1)
  1326. , messages = 0
  1327. , connections = 0
  1328. , disconnections = 0;
  1329. io.configure(function () {
  1330. io.set('close timeout', 0);
  1331. });
  1332. io.sockets.on('connection', function (socket) {
  1333. connections++;
  1334. if (connections == 1) {
  1335. socket.join('losers');
  1336. }
  1337. socket.on('trigger broadcast', function () {
  1338. socket.broadcast.to('losers').emit('victory');
  1339. });
  1340. socket.on('disconnect', function () {
  1341. disconnections++;
  1342. if (disconnections == 3) {
  1343. messages.should.eql(1);
  1344. cl1.end();
  1345. cl2.end();
  1346. cl3.end();
  1347. io.server.close();
  1348. done();
  1349. }
  1350. });
  1351. });
  1352. cl1.handshake(function (sid) {
  1353. var ws1 = websocket(cl1, sid);
  1354. ws1.on('message', function (msg) {
  1355. if (!ws1.connected) {
  1356. msg.type.should.eql('connect');
  1357. ws1.connected = true;
  1358. } else {
  1359. msg.should.eql({
  1360. type: 'event'
  1361. , name: 'victory'
  1362. , args: []
  1363. , endpoint: ''
  1364. });
  1365. messages++;
  1366. }
  1367. });
  1368. ws1.on('open', function () {
  1369. cl2.handshake(function (sid) {
  1370. var ws2 = websocket(cl2, sid);
  1371. ws2.on('message', function (msg) {
  1372. if (!ws2.connected) {
  1373. msg.type.should.eql('connect');
  1374. ws2.connected = true;
  1375. } else {
  1376. throw new Error('This socket shouldnt get a message');
  1377. };
  1378. });
  1379. ws2.on('open', function () {
  1380. cl3.handshake(function (sid) {
  1381. var ws3 = websocket(cl3, sid);
  1382. ws3.on('open', function () {
  1383. ws3.packet({
  1384. type: 'event'
  1385. , name: 'trigger broadcast'
  1386. , endpoint: ''
  1387. });
  1388. setTimeout(function () {
  1389. ws1.finishClose();
  1390. ws2.finishClose();
  1391. ws3.finishClose();
  1392. }, 50);
  1393. });
  1394. ws3.on('message', function (msg) {
  1395. if (!ws3.connected) {
  1396. msg.type.should.eql('connect');
  1397. ws3.connected = true;
  1398. } else {
  1399. throw new Error('we shouldnt get a message here');
  1400. }
  1401. });
  1402. });
  1403. });
  1404. });
  1405. });
  1406. });
  1407. },
  1408. 'test accessing handshake data from sockets': function (done) {
  1409. var cl = client(++ports)
  1410. , io = create(cl)
  1411. , ws;
  1412. io.sockets.on('connection', function (socket) {
  1413. (!!socket.handshake.address.address).should.be.true;
  1414. (!!socket.handshake.address.port).should.be.true;
  1415. socket.handshake.headers.host.should.equal('localhost');
  1416. socket.handshake.headers.connection.should.equal('keep-alive');
  1417. socket.handshake.time.should.match(/GMT/);
  1418. socket.on('disconnect', function () {
  1419. setTimeout(function () {
  1420. ws.finishClose();
  1421. cl.end();
  1422. io.server.close();
  1423. done();
  1424. }, 10);
  1425. });
  1426. socket.disconnect();
  1427. });
  1428. cl.handshake(function (sid) {
  1429. ws = websocket(cl, sid);
  1430. ws.on('message', function (msg) {
  1431. if (!ws.connected) {
  1432. msg.type.should.eql('connect');
  1433. ws.connected = true;
  1434. }
  1435. });
  1436. });
  1437. },
  1438. 'test accessing the array of clients': function (done) {
  1439. var port = ++ports
  1440. , cl1 = client(port)
  1441. , cl2 = client(port)
  1442. , io = create(cl1)
  1443. , total = 2
  1444. , ws1, ws2;
  1445. io.sockets.on('connection', function (socket) {
  1446. socket.on('join ferrets', function () {
  1447. socket.join('ferrets');
  1448. socket.send('done');
  1449. });
  1450. });
  1451. function check() {
  1452. io.sockets.clients('ferrets').should.have.length(1);
  1453. io.sockets.clients('ferrets')[0].should.be.an.instanceof(sio.Socket);
  1454. io.sockets.clients('ferrets')[0].id.should.equal(ws1.sid);
  1455. io.sockets.clients().should.have.length(2);
  1456. io.sockets.clients()[0].should.be.an.instanceof(sio.Socket);
  1457. io.sockets.clients()[0].id.should.equal(ws1.sid);
  1458. io.sockets.clients()[1].should.be.an.instanceof(sio.Socket);
  1459. io.sockets.clients()[1].id.should.equal(ws2.sid);
  1460. ws1.finishClose();
  1461. ws2.finishClose();
  1462. cl1.end();
  1463. cl2.end();
  1464. io.server.close();
  1465. done();
  1466. };
  1467. cl1.handshake(function (sid) {
  1468. ws1 = websocket(cl1, sid);
  1469. ws1.sid = sid;
  1470. ws1.on('message', function (msg) {
  1471. if (!ws1.connected) {
  1472. msg.type.should.eql('connect');
  1473. ws1.connected = true;
  1474. ws1.packet({
  1475. type: 'event'
  1476. , name: 'join ferrets'
  1477. , endpoint: ''
  1478. });
  1479. } else {
  1480. cl2.handshake(function (sid) {
  1481. ws2 = websocket(cl2, sid);
  1482. ws2.sid = sid;
  1483. ws2.on('message', function (msg) {
  1484. if (!ws2.connected) {
  1485. msg.type.should.eql('connect');
  1486. ws2.connected = true;
  1487. check();
  1488. }
  1489. });
  1490. });
  1491. }
  1492. });
  1493. });
  1494. },
  1495. 'test accessing handshake data from sockets on disconnect': function (done) {
  1496. var cl = client(++ports)
  1497. , io = create(cl)
  1498. , ws;
  1499. io.sockets.on('connection', function (socket) {
  1500. socket.on('disconnect', function () {
  1501. (!!socket.handshake.address.address).should.be.true;
  1502. (!!socket.handshake.address.port).should.be.true;
  1503. socket.handshake.headers.host.should.equal('localhost');
  1504. socket.handshake.headers.connection.should.equal('keep-alive');
  1505. socket.handshake.time.should.match(/GMT/);
  1506. setTimeout(function () {
  1507. ws.finishClose();
  1508. cl.end();
  1509. io.server.close();
  1510. done();
  1511. }, 10);
  1512. });
  1513. socket.disconnect();
  1514. });
  1515. cl.handshake(function (sid) {
  1516. ws = websocket(cl, sid);
  1517. ws.on('message', function (msg) {
  1518. if (!ws.connected) {
  1519. msg.type.should.eql('connect');
  1520. ws.connected = true;
  1521. }
  1522. });
  1523. });
  1524. },
  1525. 'test for intentional and unintentional disconnects': function (done) {
  1526. var cl = client(++ports)
  1527. , io = create(cl)
  1528. , calls = 0
  1529. , ws;
  1530. function close () {
  1531. cl.end();
  1532. io.server.close();
  1533. ws.finishClose();
  1534. done();
  1535. }
  1536. io.configure(function () {
  1537. io.set('heartbeat interval', .05);
  1538. io.set('heartbeat timeout', .05);
  1539. io.set('close timeout', 0);
  1540. });
  1541. io.of('/foo').on('connection', function (socket) {
  1542. socket.on('disconnect', function (reason) {
  1543. reason.should.equal('packet');
  1544. if (++calls == 2) close();
  1545. });
  1546. });
  1547. io.of('/bar').on('connection', function (socket) {
  1548. socket.on('disconnect', function (reason) {
  1549. reason.should.equal('socket end');
  1550. if (++calls == 2) close();
  1551. });
  1552. });
  1553. cl.handshake(function (sid) {
  1554. var messages = 0;
  1555. ws = websocket(cl, sid);
  1556. ws.on('open', function () {
  1557. ws.packet({
  1558. type: 'connect'
  1559. , endpoint: '/foo'
  1560. });
  1561. ws.packet({
  1562. type: 'connect'
  1563. , endpoint: '/bar'
  1564. });
  1565. });
  1566. ws.on('message', function (packet) {
  1567. if (packet.type == 'connect') {
  1568. if (++messages === 3) {
  1569. ws.packet({ type: 'disconnect', endpoint:'/foo' });
  1570. ws.finishClose();
  1571. }
  1572. }
  1573. });
  1574. });
  1575. },
  1576. 'test socket clean up': function (done) {
  1577. var cl = client(++ports)
  1578. , io = create(cl)
  1579. , ws;
  1580. io.sockets.on('connection', function (socket) {
  1581. var self = this
  1582. , id = socket.id;
  1583. socket.on('disconnect', function () {
  1584. setTimeout(function () {
  1585. var available = !!self.sockets[id];
  1586. available.should.be.false;
  1587. ws.finishClose();
  1588. cl.end();
  1589. io.server.close();
  1590. done();
  1591. }, 10);
  1592. });
  1593. socket.disconnect();
  1594. });
  1595. cl.handshake(function (sid) {
  1596. ws = websocket(cl, sid);
  1597. ws.on('message', function (msg) {
  1598. if (!ws.connected) {
  1599. msg.type.should.eql('connect');
  1600. ws.connected = true;
  1601. }
  1602. });
  1603. });
  1604. },
  1605. 'accessing the transport type': function (done) {
  1606. var cl = client(++ports)
  1607. , io = create(cl)
  1608. , ws;
  1609. io.sockets.on('connection', function (socket) {
  1610. socket.transport.should.equal('websocket');
  1611. socket.on('disconnect', function () {
  1612. setTimeout(function () {
  1613. ws.finishClose();
  1614. cl.end();
  1615. io.server.close();
  1616. done();
  1617. }, 10);
  1618. });
  1619. socket.disconnect();
  1620. });
  1621. cl.handshake(function (sid) {
  1622. ws = websocket(cl, sid);
  1623. ws.on('message', function (msg) {
  1624. if (!ws.connected) {
  1625. msg.type.should.eql('connect');
  1626. ws.connected = true;
  1627. }
  1628. });
  1629. });
  1630. }
  1631. };