/node_modules/socket.io/node_modules/socket.io-client/test/socket.test.js

https://github.com/ryooo321/node_websocket_chat_box · JavaScript · 331 lines · 257 code · 69 blank · 5 comment · 34 complexity · b77bf13ee4a45ae88117fcc2ff9d3851 MD5 · raw file

  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. (function (module, io, should) {
  7. if ('object' == typeof global) {
  8. return module.exports = { '': function () {} };
  9. }
  10. module.exports = {
  11. 'test connecting the socket and disconnecting': function (next) {
  12. var socket = create();
  13. socket.on('error', function (msg) {
  14. throw new Error(msg || 'Received an error');
  15. });
  16. socket.on('connect', function () {
  17. socket.disconnect();
  18. next();
  19. });
  20. },
  21. 'test receiving messages': function (next) {
  22. var socket = create()
  23. , connected = false
  24. , messages = 0;
  25. socket.on('error', function (msg) {
  26. throw new Error(msg || 'Received an error');
  27. });
  28. socket.on('connect', function () {
  29. connected = true;
  30. });
  31. socket.on('message', function (i) {
  32. String(++messages).should().equal(i);
  33. });
  34. socket.on('disconnect', function (reason) {
  35. connected.should().be_true;
  36. messages.should().equal(3);
  37. reason.should().eql('booted');
  38. next();
  39. });
  40. },
  41. 'test sending messages': function (next) {
  42. var socket = create();
  43. socket.on('error', function (msg) {
  44. throw new Error(msg || 'Received an error');
  45. });
  46. socket.on('connect', function () {
  47. socket.send('echo');
  48. socket.on('message', function (msg) {
  49. msg.should().equal('echo');
  50. socket.disconnect();
  51. next();
  52. });
  53. });
  54. },
  55. 'test acks sent from client': function (next) {
  56. var socket = create();
  57. socket.on('error', function (msg) {
  58. throw new Error(msg || 'Received an error');
  59. });
  60. socket.on('connect', function () {
  61. socket.on('message', function (msg) {
  62. if ('tobi 2' == msg) {
  63. socket.disconnect();
  64. next();
  65. }
  66. });
  67. });
  68. },
  69. 'test acks sent from server': function (next) {
  70. var socket = create();
  71. socket.on('error', function (msg) {
  72. throw new Error(msg || 'Received an error');
  73. });
  74. socket.on('connect', function () {
  75. socket.send('ooo', function () {
  76. socket.disconnect();
  77. next();
  78. });
  79. });
  80. },
  81. 'test connecting to namespaces': function (next) {
  82. var io = create()
  83. , socket = io.socket
  84. , namespaces = 2
  85. , connect = 0;
  86. function finish () {
  87. socket.of('').disconnect();
  88. connect.should().equal(3);
  89. next();
  90. }
  91. io.on('connect', function(){
  92. connect++;
  93. });
  94. socket.of('/woot').on('connect', function () {
  95. connect++;
  96. }).on('message', function (msg) {
  97. msg.should().equal('connected to woot');
  98. --namespaces || finish();
  99. }).on('error', function (msg) {
  100. throw new Error(msg || 'Received an error');
  101. });
  102. socket.of('/chat').on('connect', function () {
  103. connect++;
  104. }).on('message', function (msg) {
  105. msg.should().equal('connected to chat');
  106. --namespaces || finish();
  107. }).on('error', function (msg) {
  108. throw new Error(msg || 'Received an error');
  109. });
  110. },
  111. 'test different namespace connection methods': function (next) {
  112. var io = create('/a')
  113. , connect = 0
  114. , message = 0
  115. , socket = io.socket;
  116. function finish () {
  117. socket.of('').disconnect();
  118. connect.should().equal(3);
  119. message.should().equal(3);
  120. next();
  121. }
  122. io.on('connect', function () {
  123. ++connect;
  124. }).on('message', function (data) {
  125. data.should().eql('a');
  126. if (++message === 3) finish();
  127. }).on('error', function (msg) {
  128. throw new Error(msg || 'Received an error');
  129. });
  130. socket.of('/b').on('connect', function () {
  131. ++connect;
  132. }).on('message', function (data) {
  133. data.should().eql('b');
  134. if (++message === 3) finish();
  135. }).on('error', function (msg) {
  136. throw new Error(msg || 'Received an error');
  137. });
  138. io.of('/c').on('connect', function () {
  139. ++connect;
  140. }).on('message', function (data) {
  141. data.should().eql('c');
  142. if (++message === 3) finish();
  143. }).on('error', function (msg) {
  144. throw new Error(msg || 'Received an error');
  145. });
  146. },
  147. 'test disconnecting from namespaces': function (next) {
  148. var socket = create().socket
  149. , namespaces = 2
  150. , disconnections = 0;
  151. function finish () {
  152. socket.of('').disconnect();
  153. next();
  154. };
  155. socket.of('/a').on('error', function (msg) {
  156. throw new Error(msg || 'Received an error');
  157. });
  158. socket.of('/a').on('connect', function () {
  159. socket.of('/a').disconnect();
  160. });
  161. socket.of('/a').on('disconnect', function () {
  162. --namespaces || finish();
  163. });
  164. socket.of('/b').on('error', function (msg) {
  165. throw new Error(msg || 'Received an error');
  166. });
  167. socket.of('/b').on('connect', function () {
  168. socket.of('/b').disconnect();
  169. });
  170. socket.of('/b').on('disconnect', function () {
  171. --namespaces || finish();
  172. });
  173. },
  174. 'test authorizing for namespaces': function (next) {
  175. var socket = create().socket
  176. function finish () {
  177. socket.of('').disconnect();
  178. next();
  179. };
  180. socket.of('/a')
  181. .on('connect_failed', function (msg) {
  182. next();
  183. })
  184. .on('error', function (msg) {
  185. throw new Error(msg || 'Received an error');
  186. });
  187. },
  188. 'test sending json from server': function (next) {
  189. var socket = create();
  190. socket.on('error', function (msg) {
  191. throw new Error(msg || 'Received an error');
  192. });
  193. socket.on('message', function (msg) {
  194. msg.should().eql(3141592);
  195. socket.disconnect();
  196. next();
  197. });
  198. },
  199. 'test sending json from client': function (next) {
  200. var socket = create();
  201. socket.on('error', function (msg) {
  202. throw new Error(msg || 'Received an error');
  203. });
  204. socket.json.send([1, 2, 3]);
  205. socket.on('message', function (msg) {
  206. msg.should().equal('echo');
  207. socket.disconnect();
  208. next();
  209. });
  210. },
  211. 'test emitting an event from server': function (next) {
  212. var socket = create();
  213. socket.on('error', function (msg) {
  214. throw new Error(msg || 'Received an error');
  215. });
  216. socket.on('woot', function () {
  217. socket.disconnect();
  218. next();
  219. });
  220. },
  221. 'test emitting an event to server': function (next) {
  222. var socket = create();
  223. socket.on('error', function (msg) {
  224. throw new Error(msg || 'Received an error');
  225. });
  226. socket.emit('woot');
  227. socket.on('echo', function () {
  228. socket.disconnect();
  229. next();
  230. })
  231. },
  232. 'test emitting an event from server and sending back data': function (next) {
  233. var socket = create();
  234. socket.on('error', function (msg) {
  235. throw new Error(msg || 'Received an error');
  236. });
  237. socket.on('woot', function (a, fn) {
  238. a.should().eql(1);
  239. fn('test');
  240. socket.on('done', function () {
  241. socket.disconnect();
  242. next();
  243. });
  244. });
  245. },
  246. 'test emitting an event to server and sending back data': function (next) {
  247. var socket = create();
  248. socket.on('error', function (msg) {
  249. throw new Error(msg || 'Received an error');
  250. });
  251. socket.emit('tobi', 1, 2, function (a) {
  252. a.should().eql({ hello: 'world' });
  253. socket.disconnect();
  254. next();
  255. });
  256. }
  257. };
  258. })(
  259. 'undefined' == typeof module ? module = {} : module
  260. , 'undefined' == typeof io ? require('socket.io-client') : io
  261. , 'undefined' == typeof should ? require('should-browser') : should
  262. );