PageRenderTime 48ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 1ms

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

https://bitbucket.org/atlassianlabs/work-logger
JavaScript | 422 lines | 328 code | 89 blank | 5 comment | 37 complexity | 78050bfd719e2985278297815056dc94 MD5 | raw file
Possible License(s): Apache-2.0, MIT, BSD-3-Clause
  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 manual buffer flushing': function (next) {
  56. var socket = create();
  57. socket.socket.options['manualFlush'] = true;
  58. socket.on('error', function (msg) {
  59. throw new Error(msg || 'Received an error');
  60. });
  61. socket.on('connect', function () {
  62. socket.socket.connected = false;
  63. socket.send('buffered');
  64. socket.socket.onConnect();
  65. socket.socket.flushBuffer();
  66. socket.on('message', function (msg) {
  67. msg.should().equal('buffered');
  68. socket.disconnect();
  69. next();
  70. });
  71. });
  72. },
  73. 'test automatic buffer flushing': function (next) {
  74. var socket = create();
  75. socket.on('error', function (msg) {
  76. throw new Error(msg || 'Received an error');
  77. });
  78. socket.on('connect', function () {
  79. socket.socket.connected = false;
  80. socket.send('buffered');
  81. socket.socket.onConnect();
  82. socket.on('message', function (msg) {
  83. msg.should().equal('buffered');
  84. socket.disconnect();
  85. next();
  86. });
  87. });
  88. },
  89. 'test acks sent from client': function (next) {
  90. var socket = create();
  91. socket.on('error', function (msg) {
  92. throw new Error(msg || 'Received an error');
  93. });
  94. socket.on('connect', function () {
  95. socket.on('message', function (msg) {
  96. if ('tobi 2' == msg) {
  97. socket.disconnect();
  98. next();
  99. }
  100. });
  101. });
  102. },
  103. 'test acks sent from server': function (next) {
  104. var socket = create();
  105. socket.on('error', function (msg) {
  106. throw new Error(msg || 'Received an error');
  107. });
  108. socket.on('connect', function () {
  109. socket.send('ooo', function () {
  110. socket.disconnect();
  111. next();
  112. });
  113. });
  114. },
  115. 'test connecting to namespaces': function (next) {
  116. var io = create()
  117. , socket = io.socket
  118. , namespaces = 2
  119. , connect = 0;
  120. function finish () {
  121. socket.of('').disconnect();
  122. connect.should().equal(3);
  123. next();
  124. }
  125. socket.on('connect', function(){
  126. connect++;
  127. });
  128. socket.of('/woot').on('connect', function () {
  129. connect++;
  130. }).on('message', function (msg) {
  131. msg.should().equal('connected to woot');
  132. --namespaces || finish();
  133. }).on('error', function (msg) {
  134. throw new Error(msg || 'Received an error');
  135. });
  136. socket.of('/chat').on('connect', function () {
  137. connect++;
  138. }).on('message', function (msg) {
  139. msg.should().equal('connected to chat');
  140. --namespaces || finish();
  141. }).on('error', function (msg) {
  142. throw new Error(msg || 'Received an error');
  143. });
  144. },
  145. 'test disconnecting from namespaces': function (next) {
  146. var socket = create().socket
  147. , namespaces = 2
  148. , disconnections = 0;
  149. function finish () {
  150. socket.of('').disconnect();
  151. next();
  152. };
  153. socket.of('/a').on('error', function (msg) {
  154. throw new Error(msg || 'Received an error');
  155. });
  156. socket.of('/a').on('connect', function () {
  157. socket.of('/a').disconnect();
  158. });
  159. socket.of('/a').on('disconnect', function () {
  160. --namespaces || finish();
  161. });
  162. socket.of('/b').on('error', function (msg) {
  163. throw new Error(msg || 'Received an error');
  164. });
  165. socket.of('/b').on('connect', function () {
  166. socket.of('/b').disconnect();
  167. });
  168. socket.of('/b').on('disconnect', function () {
  169. --namespaces || finish();
  170. });
  171. },
  172. 'test authorizing for namespaces': function (next) {
  173. var socket = create().socket
  174. function finish () {
  175. socket.of('').disconnect();
  176. next();
  177. };
  178. socket.of('/a')
  179. .on('connect_failed', function (msg) {
  180. next();
  181. })
  182. .on('error', function (msg) {
  183. throw new Error(msg || 'Received an error');
  184. });
  185. },
  186. 'test sending json from server': function (next) {
  187. var socket = create();
  188. socket.on('error', function (msg) {
  189. throw new Error(msg || 'Received an error');
  190. });
  191. socket.on('message', function (msg) {
  192. msg.should().eql(3141592);
  193. socket.disconnect();
  194. next();
  195. });
  196. },
  197. 'test sending json from client': function (next) {
  198. var socket = create();
  199. socket.on('error', function (msg) {
  200. throw new Error(msg || 'Received an error');
  201. });
  202. socket.json.send([1, 2, 3]);
  203. socket.on('message', function (msg) {
  204. msg.should().equal('echo');
  205. socket.disconnect();
  206. next();
  207. });
  208. },
  209. 'test emitting an event from server': function (next) {
  210. var socket = create();
  211. socket.on('error', function (msg) {
  212. throw new Error(msg || 'Received an error');
  213. });
  214. socket.on('woot', function () {
  215. socket.disconnect();
  216. next();
  217. });
  218. },
  219. 'test emitting an event to server': function (next) {
  220. var socket = create();
  221. socket.on('error', function (msg) {
  222. throw new Error(msg || 'Received an error');
  223. });
  224. socket.emit('woot');
  225. socket.on('echo', function () {
  226. socket.disconnect();
  227. next();
  228. })
  229. },
  230. 'test emitting multiple events at once to the server': function (next) {
  231. var socket = create();
  232. socket.on('connect', function () {
  233. socket.emit('print', 'foo');
  234. socket.emit('print', 'bar');
  235. });
  236. socket.on('done', function () {
  237. socket.disconnect();
  238. next();
  239. });
  240. },
  241. 'test emitting an event from server and sending back data': function (next) {
  242. var socket = create();
  243. socket.on('error', function (msg) {
  244. throw new Error(msg || 'Received an error');
  245. });
  246. socket.on('woot', function (a, fn) {
  247. a.should().eql(1);
  248. fn('test');
  249. socket.on('done', function () {
  250. socket.disconnect();
  251. next();
  252. });
  253. });
  254. },
  255. 'test emitting an event to server and sending back data': function (next) {
  256. var socket = create();
  257. socket.on('error', function (msg) {
  258. throw new Error(msg || 'Received an error');
  259. });
  260. socket.emit('tobi', 1, 2, function (a) {
  261. a.should().eql({ hello: 'world' });
  262. socket.disconnect();
  263. next();
  264. });
  265. },
  266. 'test encoding a payload': function (next) {
  267. var socket = create('/woot');
  268. socket.on('error', function (msg) {
  269. throw new Error(msg || 'Received an error');
  270. });
  271. socket.on('connect', function () {
  272. socket.socket.setBuffer(true);
  273. socket.send('ñ');
  274. socket.send('ñ');
  275. socket.send('ñ');
  276. socket.send('ñ');
  277. socket.socket.setBuffer(false);
  278. });
  279. socket.on('done', function () {
  280. socket.disconnect();
  281. next();
  282. });
  283. },
  284. 'test sending query strings to the server': function (next) {
  285. var socket = create('?foo=bar');
  286. socket.on('error', function (msg) {
  287. throw new Error(msg || 'Received an error');
  288. });
  289. socket.on('message', function (data) {
  290. data.query.foo.should().eql('bar');
  291. socket.disconnect();
  292. next();
  293. });
  294. },
  295. 'test sending newline': function (next) {
  296. var socket = create();
  297. socket.on('error', function (msg) {
  298. throw new Error(msg || 'Received an error');
  299. });
  300. socket.send('\n');
  301. socket.on('done', function () {
  302. socket.disconnect();
  303. next();
  304. });
  305. },
  306. 'test sending unicode': function (next) {
  307. var socket = create();
  308. socket.on('error', function (msg) {
  309. throw new Error(msg || 'Received an error');
  310. });
  311. socket.json.send({ test: "☃" });
  312. socket.on('done', function () {
  313. socket.disconnect();
  314. next();
  315. });
  316. },
  317. 'test webworker connection': function (next) {
  318. if (!window.Worker) {
  319. return next();
  320. }
  321. var worker = new Worker('/test/worker.js');
  322. worker.postMessage(uri());
  323. worker.onmessage = function (ev) {
  324. if ('done!' == ev.data) return next();
  325. throw new Error('Unexpected message: ' + ev.data);
  326. }
  327. }
  328. };
  329. })(
  330. 'undefined' == typeof module ? module = {} : module
  331. , 'undefined' == typeof io ? require('socket.io-client') : io
  332. , 'undefined' == typeof should ? require('should-browser') : should
  333. );