PageRenderTime 41ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

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

https://github.com/Unitech/Quizzy
JavaScript | 366 lines | 283 code | 78 blank | 5 comment | 32 complexity | 450d80525f3aaa5c1cc9554cb56e56a2 MD5 | raw file
Possible License(s): MIT, Apache-2.0
  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. socket.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 disconnecting from namespaces': function (next) {
  112. var socket = create().socket
  113. , namespaces = 2
  114. , disconnections = 0;
  115. function finish () {
  116. socket.of('').disconnect();
  117. next();
  118. };
  119. socket.of('/a').on('error', function (msg) {
  120. throw new Error(msg || 'Received an error');
  121. });
  122. socket.of('/a').on('connect', function () {
  123. socket.of('/a').disconnect();
  124. });
  125. socket.of('/a').on('disconnect', function () {
  126. --namespaces || finish();
  127. });
  128. socket.of('/b').on('error', function (msg) {
  129. throw new Error(msg || 'Received an error');
  130. });
  131. socket.of('/b').on('connect', function () {
  132. socket.of('/b').disconnect();
  133. });
  134. socket.of('/b').on('disconnect', function () {
  135. --namespaces || finish();
  136. });
  137. },
  138. 'test authorizing for namespaces': function (next) {
  139. var socket = create().socket
  140. function finish () {
  141. socket.of('').disconnect();
  142. next();
  143. };
  144. socket.of('/a')
  145. .on('connect_failed', function (msg) {
  146. next();
  147. })
  148. .on('error', function (msg) {
  149. throw new Error(msg || 'Received an error');
  150. });
  151. },
  152. 'test sending json from server': function (next) {
  153. var socket = create();
  154. socket.on('error', function (msg) {
  155. throw new Error(msg || 'Received an error');
  156. });
  157. socket.on('message', function (msg) {
  158. msg.should().eql(3141592);
  159. socket.disconnect();
  160. next();
  161. });
  162. },
  163. 'test sending json from client': function (next) {
  164. var socket = create();
  165. socket.on('error', function (msg) {
  166. throw new Error(msg || 'Received an error');
  167. });
  168. socket.json.send([1, 2, 3]);
  169. socket.on('message', function (msg) {
  170. msg.should().equal('echo');
  171. socket.disconnect();
  172. next();
  173. });
  174. },
  175. 'test emitting an event from server': function (next) {
  176. var socket = create();
  177. socket.on('error', function (msg) {
  178. throw new Error(msg || 'Received an error');
  179. });
  180. socket.on('woot', function () {
  181. socket.disconnect();
  182. next();
  183. });
  184. },
  185. 'test emitting an event to server': function (next) {
  186. var socket = create();
  187. socket.on('error', function (msg) {
  188. throw new Error(msg || 'Received an error');
  189. });
  190. socket.emit('woot');
  191. socket.on('echo', function () {
  192. socket.disconnect();
  193. next();
  194. })
  195. },
  196. 'test emitting multiple events at once to the server': function (next) {
  197. var socket = create();
  198. socket.on('connect', function () {
  199. socket.emit('print', 'foo');
  200. socket.emit('print', 'bar');
  201. });
  202. socket.on('done', function () {
  203. socket.disconnect();
  204. next();
  205. });
  206. },
  207. 'test emitting an event from server and sending back data': function (next) {
  208. var socket = create();
  209. socket.on('error', function (msg) {
  210. throw new Error(msg || 'Received an error');
  211. });
  212. socket.on('woot', function (a, fn) {
  213. a.should().eql(1);
  214. fn('test');
  215. socket.on('done', function () {
  216. socket.disconnect();
  217. next();
  218. });
  219. });
  220. },
  221. 'test emitting an event to server and sending back data': function (next) {
  222. var socket = create();
  223. socket.on('error', function (msg) {
  224. throw new Error(msg || 'Received an error');
  225. });
  226. socket.emit('tobi', 1, 2, function (a) {
  227. a.should().eql({ hello: 'world' });
  228. socket.disconnect();
  229. next();
  230. });
  231. },
  232. 'test encoding a payload': function (next) {
  233. var socket = create('/woot');
  234. socket.on('error', function (msg) {
  235. throw new Error(msg || 'Received an error');
  236. });
  237. socket.on('connect', function () {
  238. socket.socket.setBuffer(true);
  239. socket.send('単');
  240. socket.send('単');
  241. socket.send('単');
  242. socket.send('単');
  243. socket.socket.setBuffer(false);
  244. });
  245. socket.on('done', function () {
  246. socket.disconnect();
  247. next();
  248. });
  249. },
  250. 'test sending query strings to the server': function (next) {
  251. var socket = create('?foo=bar');
  252. socket.on('error', function (msg) {
  253. throw new Error(msg || 'Received an error');
  254. });
  255. socket.on('message', function (data) {
  256. data.query.foo.should().eql('bar');
  257. socket.disconnect();
  258. next();
  259. });
  260. },
  261. 'test sending newline': function (next) {
  262. var socket = create();
  263. socket.on('error', function (msg) {
  264. throw new Error(msg || 'Received an error');
  265. });
  266. socket.send('\n');
  267. socket.on('done', function () {
  268. socket.disconnect();
  269. next();
  270. });
  271. },
  272. 'test sending unicode': function (next) {
  273. var socket = create();
  274. socket.on('error', function (msg) {
  275. throw new Error(msg || 'Received an error');
  276. });
  277. socket.json.send({ test: "\u2028" });
  278. socket.on('done', function () {
  279. socket.disconnect();
  280. next();
  281. });
  282. }
  283. };
  284. })(
  285. 'undefined' == typeof module ? module = {} : module
  286. , 'undefined' == typeof io ? require('socket.io-client') : io
  287. , 'undefined' == typeof should ? require('should-browser') : should
  288. );