PageRenderTime 65ms CodeModel.GetById 30ms RepoModel.GetById 1ms app.codeStats 0ms

/demo/server/node_modules/socket.io/test/namespace.test.js

https://bitbucket.org/jesperrasmussen/slides
JavaScript | 286 lines | 237 code | 38 blank | 11 comment | 15 complexity | defbc131dd9f7a8559833fb011f64dc5 MD5 | raw file
Possible License(s): BSD-3-Clause, MIT
  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('socket.io')
  10. , should = require('./common')
  11. , ports = 15700;
  12. /**
  13. * Test.
  14. */
  15. module.exports = {
  16. 'namespace pass no authentication': function (done) {
  17. var cl = client(++ports)
  18. , io = create(cl)
  19. , ws;
  20. io.of('/a')
  21. .on('connection', function (socket) {
  22. cl.end();
  23. ws.finishClose();
  24. io.server.close()
  25. done();
  26. });
  27. cl.handshake(function (sid) {
  28. ws = websocket(cl, sid);
  29. ws.on('open', function () {
  30. ws.packet({
  31. type: 'connect'
  32. , endpoint: '/a'
  33. });
  34. })
  35. });
  36. },
  37. 'namespace pass authentication': function (done) {
  38. var cl = client(++ports)
  39. , io = create(cl)
  40. , ws;
  41. io.of('/a')
  42. .authorization(function (data, fn) {
  43. fn(null, true);
  44. })
  45. .on('connection', function (socket) {
  46. cl.end();
  47. ws.finishClose();
  48. io.server.close()
  49. done();
  50. });
  51. cl.handshake(function (sid) {
  52. ws = websocket(cl, sid);
  53. ws.on('open', function () {
  54. ws.packet({
  55. type: 'connect'
  56. , endpoint: '/a'
  57. });
  58. })
  59. });
  60. },
  61. 'namespace authentication handshake data': function (done) {
  62. var cl = client(++ports)
  63. , io = create(cl)
  64. , ws;
  65. io.of('/a')
  66. .authorization(function (data, fn) {
  67. data.foo = 'bar';
  68. fn(null, true);
  69. })
  70. .on('connection', function (socket) {
  71. (!!socket.handshake.address.address).should.be.true;
  72. (!!socket.handshake.address.port).should.be.true;
  73. socket.handshake.headers.host.should.equal('localhost');
  74. socket.handshake.headers.connection.should.equal('keep-alive');
  75. socket.handshake.time.should.match(/GMT/);
  76. socket.handshake.foo.should.equal('bar');
  77. cl.end();
  78. ws.finishClose();
  79. io.server.close()
  80. done();
  81. });
  82. cl.handshake(function (sid) {
  83. ws = websocket(cl, sid);
  84. ws.on('open', function () {
  85. ws.packet({
  86. type: 'connect'
  87. , endpoint: '/a'
  88. });
  89. })
  90. });
  91. },
  92. 'namespace fail authentication': function (done) {
  93. var cl = client(++ports)
  94. , io = create(cl)
  95. , calls = 0
  96. , ws;
  97. io.of('/a')
  98. .authorization(function (data, fn) {
  99. fn(null, false);
  100. })
  101. .on('connection', function (socket) {
  102. throw new Error('Should not be called');
  103. });
  104. cl.handshake(function (sid) {
  105. ws = websocket(cl, sid);
  106. ws.on('open', function () {
  107. ws.packet({
  108. type: 'connect'
  109. , endpoint: '/a'
  110. });
  111. });
  112. ws.on('message', function (data) {
  113. if (data.endpoint == '/a') {
  114. data.type.should.eql('error');
  115. data.reason.should.eql('unauthorized')
  116. cl.end();
  117. ws.finishClose();
  118. io.server.close()
  119. done();
  120. }
  121. })
  122. });
  123. },
  124. 'broadcasting sends and emits on a namespace': function (done) {
  125. var cl = client(++ports)
  126. , io = create(cl)
  127. , calls = 0
  128. , connect = 0
  129. , message = 0
  130. , events = 0
  131. , expected = 5
  132. , ws1
  133. , ws2;
  134. io.of('a')
  135. .on('connection', function (socket){
  136. if (connect < 2) {
  137. return;
  138. }
  139. socket.broadcast.emit('b', 'test');
  140. socket.broadcast.json.emit('json', {foo:'bar'});
  141. socket.broadcast.send('foo');
  142. });
  143. function finish () {
  144. connect.should.equal(2);
  145. message.should.equal(1);
  146. events.should.equal(2);
  147. cl.end();
  148. ws1.finishClose();
  149. ws2.finishClose();
  150. io.server.close();
  151. done();
  152. }
  153. cl.handshake(function (sid) {
  154. ws1 = websocket(cl, sid);
  155. ws1.on('message', function (data) {
  156. if (data.type === 'connect') {
  157. if (connect == 0) {
  158. cl.handshake(function (sid) {
  159. ws2 = websocket(cl, sid);
  160. ws2.on('open', function () {
  161. ws2.packet({
  162. type: 'connect'
  163. , endpoint: 'a'
  164. });
  165. });
  166. });
  167. }
  168. ++connect;
  169. if (++calls === expected) finish();
  170. }
  171. if (data.type === 'message') {
  172. ++message;
  173. if (++calls === expected) finish();
  174. }
  175. if (data.type === 'event') {
  176. if (data.name === 'b' || data.name === 'json') ++events;
  177. if (++calls === expected) finish();
  178. }
  179. });
  180. ws1.on('open', function() {
  181. ws1.packet({
  182. type: 'connect'
  183. , endpoint: 'a'
  184. });
  185. });
  186. })
  187. },
  188. 'joining rooms inside a namespace': function (done) {
  189. var cl = client(++ports)
  190. , io = create(cl)
  191. , calls = 0
  192. , ws;
  193. io.of('/foo').on('connection', function (socket) {
  194. socket.join('foo.bar');
  195. this.in('foo.bar').emit('baz', 'pewpew');
  196. });
  197. cl.handshake(function (sid) {
  198. ws = websocket(cl, sid);
  199. ws.on('open', function (){
  200. ws.packet({
  201. type: 'connect'
  202. , endpoint: '/foo'
  203. });
  204. });
  205. ws.on('message', function (data) {
  206. if (data.type === 'event') {
  207. data.name.should.equal('baz');
  208. cl.end();
  209. ws.finishClose();
  210. io.server.close();
  211. done();
  212. }
  213. });
  214. })
  215. },
  216. 'ignoring blacklisted events': function (done) {
  217. var cl = client(++ports)
  218. , io = create(cl)
  219. , calls = 0
  220. , ws;
  221. io.set('heartbeat interval', 1);
  222. io.set('blacklist', ['foobar']);
  223. io.sockets.on('connection', function (socket) {
  224. socket.on('foobar', function () {
  225. calls++;
  226. });
  227. });
  228. cl.handshake(function (sid) {
  229. ws = websocket(cl, sid);
  230. ws.on('open', function (){
  231. ws.packet({
  232. type: 'event'
  233. , name: 'foobar'
  234. , endpoint: ''
  235. });
  236. });
  237. ws.on('message', function (data) {
  238. if (data.type === 'heartbeat') {
  239. cl.end();
  240. ws.finishClose();
  241. io.server.close();
  242. calls.should.equal(0);
  243. done();
  244. }
  245. });
  246. });
  247. }
  248. };