parser.test.js 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360
  1. /*!
  2. * socket.io-node
  3. * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
  4. * MIT Licensed
  5. */
  6. (function (module, io, should) {
  7. var parser = io.parser;
  8. module.exports = {
  9. 'decoding error packet': function () {
  10. parser.decodePacket('7:::').should().eql({
  11. type: 'error'
  12. , reason: ''
  13. , advice: ''
  14. , endpoint: ''
  15. });
  16. },
  17. 'decoding error packet with reason': function () {
  18. parser.decodePacket('7:::0').should().eql({
  19. type: 'error'
  20. , reason: 'transport not supported'
  21. , advice: ''
  22. , endpoint: ''
  23. });
  24. },
  25. 'decoding error packet with reason and advice': function () {
  26. parser.decodePacket('7:::2+0').should().eql({
  27. type: 'error'
  28. , reason: 'unauthorized'
  29. , advice: 'reconnect'
  30. , endpoint: ''
  31. });
  32. },
  33. 'decoding error packet with endpoint': function () {
  34. parser.decodePacket('7::/woot').should().eql({
  35. type: 'error'
  36. , reason: ''
  37. , advice: ''
  38. , endpoint: '/woot'
  39. });
  40. },
  41. 'decoding ack packet': function () {
  42. parser.decodePacket('6:::140').should().eql({
  43. type: 'ack'
  44. , ackId: '140'
  45. , endpoint: ''
  46. , args: []
  47. });
  48. },
  49. 'decoding ack packet with args': function () {
  50. parser.decodePacket('6:::12+["woot","wa"]').should().eql({
  51. type: 'ack'
  52. , ackId: '12'
  53. , endpoint: ''
  54. , args: ['woot', 'wa']
  55. });
  56. },
  57. 'decoding ack packet with bad json': function () {
  58. var thrown = false;
  59. try {
  60. parser.decodePacket('6:::1+{"++]').should().eql({
  61. type: 'ack'
  62. , ackId: '1'
  63. , endpoint: ''
  64. , args: []
  65. });
  66. } catch (e) {
  67. thrown = true;
  68. }
  69. thrown.should().be_false;
  70. },
  71. 'decoding json packet': function () {
  72. parser.decodePacket('4:::"2"').should().eql({
  73. type: 'json'
  74. , endpoint: ''
  75. , data: '2'
  76. });
  77. },
  78. 'decoding json packet with message id and ack data': function () {
  79. parser.decodePacket('4:1+::{"a":"b"}').should().eql({
  80. type: 'json'
  81. , id: 1
  82. , ack: 'data'
  83. , endpoint: ''
  84. , data: { a: 'b' }
  85. });
  86. },
  87. 'decoding an event packet': function () {
  88. parser.decodePacket('5:::{"name":"woot"}').should().eql({
  89. type: 'event'
  90. , name: 'woot'
  91. , endpoint: ''
  92. , args: []
  93. });
  94. },
  95. 'decoding an event packet with message id and ack': function () {
  96. parser.decodePacket('5:1+::{"name":"tobi"}').should().eql({
  97. type: 'event'
  98. , id: 1
  99. , ack: 'data'
  100. , endpoint: ''
  101. , name: 'tobi'
  102. , args: []
  103. });
  104. },
  105. 'decoding an event packet with data': function () {
  106. parser.decodePacket('5:::{"name":"edwald","args":[{"a": "b"},2,"3"]}')
  107. .should().eql({
  108. type: 'event'
  109. , name: 'edwald'
  110. , endpoint: ''
  111. , args: [{a: 'b'}, 2, '3']
  112. });
  113. },
  114. 'decoding a message packet': function () {
  115. parser.decodePacket('3:::woot').should().eql({
  116. type: 'message'
  117. , endpoint: ''
  118. , data: 'woot'
  119. });
  120. },
  121. 'decoding a message packet with id and endpoint': function () {
  122. parser.decodePacket('3:5:/tobi').should().eql({
  123. type: 'message'
  124. , id: 5
  125. , ack: true
  126. , endpoint: '/tobi'
  127. , data: ''
  128. });
  129. },
  130. 'decoding a heartbeat packet': function () {
  131. parser.decodePacket('2:::').should().eql({
  132. type: 'heartbeat'
  133. , endpoint: ''
  134. });
  135. },
  136. 'decoding a connection packet': function () {
  137. parser.decodePacket('1::/tobi').should().eql({
  138. type: 'connect'
  139. , endpoint: '/tobi'
  140. , qs: ''
  141. });
  142. },
  143. 'decoding a connection packet with query string': function () {
  144. parser.decodePacket('1::/test:?test=1').should().eql({
  145. type: 'connect'
  146. , endpoint: '/test'
  147. , qs: '?test=1'
  148. });
  149. },
  150. 'decoding a disconnection packet': function () {
  151. parser.decodePacket('0::/woot').should().eql({
  152. type: 'disconnect'
  153. , endpoint: '/woot'
  154. });
  155. },
  156. 'encoding error packet': function () {
  157. parser.encodePacket({
  158. type: 'error'
  159. , reason: ''
  160. , advice: ''
  161. , endpoint: ''
  162. }).should().eql('7::');
  163. },
  164. 'encoding error packet with reason': function () {
  165. parser.encodePacket({
  166. type: 'error'
  167. , reason: 'transport not supported'
  168. , advice: ''
  169. , endpoint: ''
  170. }).should().eql('7:::0');
  171. },
  172. 'encoding error packet with reason and advice': function () {
  173. parser.encodePacket({
  174. type: 'error'
  175. , reason: 'unauthorized'
  176. , advice: 'reconnect'
  177. , endpoint: ''
  178. }).should().eql('7:::2+0');
  179. },
  180. 'encoding error packet with endpoint': function () {
  181. parser.encodePacket({
  182. type: 'error'
  183. , reason: ''
  184. , advice: ''
  185. , endpoint: '/woot'
  186. }).should().eql('7::/woot');
  187. },
  188. 'encoding ack packet': function () {
  189. parser.encodePacket({
  190. type: 'ack'
  191. , ackId: '140'
  192. , endpoint: ''
  193. , args: []
  194. }).should().eql('6:::140');
  195. },
  196. 'encoding ack packet with args': function () {
  197. parser.encodePacket({
  198. type: 'ack'
  199. , ackId: '12'
  200. , endpoint: ''
  201. , args: ['woot', 'wa']
  202. }).should().eql('6:::12+["woot","wa"]');
  203. },
  204. 'encoding json packet': function () {
  205. parser.encodePacket({
  206. type: 'json'
  207. , endpoint: ''
  208. , data: '2'
  209. }).should().eql('4:::"2"');
  210. },
  211. 'encoding json packet with message id and ack data': function () {
  212. parser.encodePacket({
  213. type: 'json'
  214. , id: 1
  215. , ack: 'data'
  216. , endpoint: ''
  217. , data: { a: 'b' }
  218. }).should().eql('4:1+::{"a":"b"}');
  219. },
  220. 'encoding an event packet': function () {
  221. parser.encodePacket({
  222. type: 'event'
  223. , name: 'woot'
  224. , endpoint: ''
  225. , args: []
  226. }).should().eql('5:::{"name":"woot"}');
  227. },
  228. 'encoding an event packet with message id and ack': function () {
  229. parser.encodePacket({
  230. type: 'event'
  231. , id: 1
  232. , ack: 'data'
  233. , endpoint: ''
  234. , name: 'tobi'
  235. , args: []
  236. }).should().eql('5:1+::{"name":"tobi"}');
  237. },
  238. 'encoding an event packet with data': function () {
  239. parser.encodePacket({
  240. type: 'event'
  241. , name: 'edwald'
  242. , endpoint: ''
  243. , args: [{a: 'b'}, 2, '3']
  244. }).should().eql('5:::{"name":"edwald","args":[{"a":"b"},2,"3"]}');
  245. },
  246. 'encoding a message packet': function () {
  247. parser.encodePacket({
  248. type: 'message'
  249. , endpoint: ''
  250. , data: 'woot'
  251. }).should().eql('3:::woot');
  252. },
  253. 'encoding a message packet with id and endpoint': function () {
  254. parser.encodePacket({
  255. type: 'message'
  256. , id: 5
  257. , ack: true
  258. , endpoint: '/tobi'
  259. , data: ''
  260. }).should().eql('3:5:/tobi');
  261. },
  262. 'encoding a heartbeat packet': function () {
  263. parser.encodePacket({
  264. type: 'heartbeat'
  265. , endpoint: ''
  266. }).should().eql('2::');
  267. },
  268. 'encoding a connection packet': function () {
  269. parser.encodePacket({
  270. type: 'connect'
  271. , endpoint: '/tobi'
  272. , qs: ''
  273. }).should().eql('1::/tobi');
  274. },
  275. 'encoding a connection packet with query string': function () {
  276. parser.encodePacket({
  277. type: 'connect'
  278. , endpoint: '/test'
  279. , qs: '?test=1'
  280. }).should().eql('1::/test:?test=1');
  281. },
  282. 'encoding a disconnection packet': function () {
  283. parser.encodePacket({
  284. type: 'disconnect'
  285. , endpoint: '/woot'
  286. }).should().eql('0::/woot');
  287. },
  288. 'test decoding a payload': function () {
  289. parser.decodePayload('\ufffd5\ufffd3:::5\ufffd7\ufffd3:::53d'
  290. + '\ufffd3\ufffd0::').should().eql([
  291. { type: 'message', data: '5', endpoint: '' }
  292. , { type: 'message', data: '53d', endpoint: '' }
  293. , { type: 'disconnect', endpoint: '' }
  294. ]);
  295. },
  296. 'test encoding a payload': function () {
  297. parser.encodePayload([
  298. parser.encodePacket({ type: 'message', data: '5', endpoint: '' })
  299. , parser.encodePacket({ type: 'message', data: '53d', endpoint: '' })
  300. ]).should().eql('\ufffd5\ufffd3:::5\ufffd7\ufffd3:::53d')
  301. },
  302. 'test decoding newline': function () {
  303. parser.decodePacket('3:::\n').should().eql({
  304. type: 'message'
  305. , endpoint: ''
  306. , data: '\n'
  307. });
  308. }
  309. };
  310. })(
  311. 'undefined' == typeof module ? module = {} : module
  312. , 'undefined' == typeof io ? require('socket.io-client') : io
  313. , 'undefined' == typeof should ? require('should') : should
  314. );