PageRenderTime 40ms CodeModel.GetById 12ms RepoModel.GetById 0ms app.codeStats 0ms

/public/vendor/bower_components/socket.io-client/test/connection.js

https://github.com/kirillKey/questSearchTrain
JavaScript | 347 lines | 298 code | 45 blank | 4 comment | 10 complexity | 6a3cc54b6f5963f21812c3314510cea3 MD5 | raw file
Possible License(s): Apache-2.0, MIT, 0BSD, GPL-2.0, BSD-3-Clause
  1. var expect = require('expect.js');
  2. var io = require('../');
  3. var hasCORS = require('has-cors');
  4. var textBlobBuilder = require('text-blob-builder');
  5. describe('connection', function() {
  6. this.timeout(20000);
  7. it('should connect to localhost', function(done) {
  8. var socket = io({ forceNew: true });
  9. socket.emit('hi');
  10. socket.on('hi', function(data){
  11. socket.disconnect();
  12. done();
  13. });
  14. });
  15. it('should work with acks', function(done){
  16. var socket = io({ forceNew: true });
  17. socket.emit('ack');
  18. socket.on('ack', function(fn){
  19. fn(5, { test: true });
  20. });
  21. socket.on('got it', function(){
  22. socket.disconnect();
  23. done();
  24. });
  25. });
  26. it('should receive date with ack', function(done){
  27. var socket = io({ forceNew: true });
  28. socket.emit('getAckDate', { test: true }, function(data){
  29. expect(data).to.be.a('string');
  30. socket.disconnect();
  31. done();
  32. });
  33. });
  34. it('should work with false', function(done){
  35. var socket = io({ forceNew: true });
  36. socket.emit('false');
  37. socket.on('false', function(f){
  38. expect(f).to.be(false);
  39. socket.disconnect();
  40. done();
  41. });
  42. });
  43. it('should receive utf8 multibyte characters', function(done) {
  44. var correct = [
  45. 'てすと',
  46. 'Я Б Г Д Ж Й',
  47. 'Ä ä Ü ü ß',
  48. 'utf8 — string',
  49. 'utf8 — string'
  50. ];
  51. var socket = io({ forceNew: true });
  52. var i = 0;
  53. socket.on('takeUtf8', function(data) {
  54. expect(data).to.be(correct[i]);
  55. i++;
  56. if (i == correct.length) {
  57. socket.disconnect();
  58. done();
  59. }
  60. });
  61. socket.emit('getUtf8');
  62. });
  63. it('should connect to a namespace after connection established', function(done) {
  64. var manager = io.Manager();
  65. var socket = manager.socket('/');
  66. socket.on('connect', function(){
  67. var foo = manager.socket('/foo');
  68. foo.on('connect', function(){
  69. foo.close();
  70. socket.close();
  71. done();
  72. });
  73. });
  74. });
  75. it('should reconnect by default', function(done){
  76. var socket = io({ forceNew: true });
  77. socket.io.on('reconnect', function() {
  78. socket.disconnect();
  79. done();
  80. });
  81. setTimeout(function() {
  82. socket.io.engine.close();
  83. }, 500);
  84. });
  85. it('reconnect event should fire in socket', function(done){
  86. var socket = io({ forceNew: true });
  87. socket.on('reconnect', function() {
  88. socket.disconnect();
  89. done();
  90. });
  91. setTimeout(function() {
  92. socket.io.engine.close();
  93. }, 500);
  94. });
  95. it('should try to reconnect twice and fail when requested two attempts with immediate timeout and reconnect enabled', function(done) {
  96. var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
  97. var socket;
  98. var reconnects = 0;
  99. var reconnectCb = function() {
  100. reconnects++;
  101. };
  102. manager.on('reconnect_attempt', reconnectCb);
  103. manager.on('reconnect_failed', function failed() {
  104. expect(reconnects).to.be(2);
  105. socket.close();
  106. done();
  107. });
  108. socket = manager.socket('/timeout');
  109. });
  110. it('should fire reconnect_* events on socket', function(done) {
  111. var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
  112. var socket = manager.socket('/timeout_socket');
  113. var reconnects = 0;
  114. var reconnectCb = function(attempts) {
  115. reconnects++;
  116. expect(attempts).to.be(reconnects);
  117. };
  118. socket.on('reconnect_attempt', reconnectCb);
  119. socket.on('reconnect_failed', function failed() {
  120. expect(reconnects).to.be(2);
  121. socket.close();
  122. done();
  123. });
  124. });
  125. it('should fire error on socket', function(done) {
  126. var manager = io.Manager({ reconnection: true });
  127. var socket = manager.socket('/timeout_socket');
  128. socket.on('error', function(data) {
  129. expect(data.code).to.be('test');
  130. socket.close();
  131. done();
  132. });
  133. socket.on('connect', function() {
  134. manager.engine.onPacket({ type: 'error', data: 'test' });
  135. });
  136. });
  137. it('should fire reconnecting (on socket) with attempts number when reconnecting twice', function(done) {
  138. var manager = io.Manager({ reconnection: true, timeout: 0, reconnectionAttempts: 2, reconnectionDelay: 10 });
  139. var socket = manager.socket('/timeout_socket');
  140. var reconnects = 0;
  141. var reconnectCb = function(attempts) {
  142. reconnects++;
  143. expect(attempts).to.be(reconnects);
  144. };
  145. socket.on('reconnecting', reconnectCb);
  146. socket.on('reconnect_failed', function failed() {
  147. expect(reconnects).to.be(2);
  148. socket.close();
  149. done();
  150. });
  151. });
  152. it('should not try to reconnect and should form a connection when connecting to correct port with default timeout', function(done) {
  153. var manager = io.Manager({ reconnection: true, reconnectionDelay: 10 });
  154. var cb = function() {
  155. socket.close();
  156. expect().fail();
  157. };
  158. manager.on('reconnect_attempt', cb);
  159. var socket = manager.socket('/valid');
  160. socket.on('connect', function(){
  161. // set a timeout to let reconnection possibly fire
  162. setTimeout(function() {
  163. socket.close();
  164. done();
  165. }, 1000);
  166. });
  167. });
  168. // Ignore incorrect connection test for old IE due to no support for
  169. // `script.onerror` (see: http://requirejs.org/docs/api.html#ieloadfail)
  170. if (!global.document || hasCORS) {
  171. it('should try to reconnect twice and fail when requested two attempts with incorrect address and reconnect enabled', function(done) {
  172. var manager = io.Manager('http://localhost:3940', { reconnection: true, reconnectionAttempts: 2, reconnectionDelay: 10 });
  173. var socket = manager.socket('/asd');
  174. var reconnects = 0;
  175. var cb = function() {
  176. reconnects++;
  177. };
  178. manager.on('reconnect_attempt', cb);
  179. manager.on('reconnect_failed', function() {
  180. expect(reconnects).to.be(2);
  181. socket.disconnect();
  182. done();
  183. });
  184. });
  185. it('should not try to reconnect with incorrect port when reconnection disabled', function(done) {
  186. var manager = io.Manager('http://localhost:9823', { reconnection: false });
  187. var cb = function() {
  188. socket.close();
  189. expect().fail();
  190. };
  191. manager.on('reconnect_attempt', cb);
  192. manager.on('connect_error', function(){
  193. // set a timeout to let reconnection possibly fire
  194. setTimeout(function() {
  195. socket.disconnect();
  196. done();
  197. }, 1000);
  198. });
  199. var socket = manager.socket('/invalid');
  200. });
  201. }
  202. it('should emit date as string', function(done){
  203. var socket = io({ forceNew: true });
  204. socket.on('takeDate', function(data) {
  205. expect(data).to.be.a('string');
  206. done();
  207. });
  208. socket.emit('getDate');
  209. });
  210. it('should emit date in object', function(done){
  211. var socket = io({ forceNew: true });
  212. socket.on('takeDateObj', function(data) {
  213. expect(data).to.be.an('object');
  214. expect(data.date).to.be.a('string');
  215. done();
  216. });
  217. socket.emit('getDateObj');
  218. });
  219. if (!global.Blob && !global.ArrayBuffer) {
  220. it('should get base64 data as a last resort', function(done) {
  221. var socket = io({ forceNew: true });
  222. socket.on('takebin', function(a) {
  223. socket.disconnect();
  224. expect(a.base64).to.be(true);
  225. expect(a.data).to.eql('YXNkZmFzZGY=');
  226. done();
  227. });
  228. socket.emit('getbin');
  229. });
  230. }
  231. if (global.ArrayBuffer) {
  232. var base64 = require('base64-arraybuffer');
  233. it('should get binary data (as an ArrayBuffer)', function(done){
  234. var socket = io({ forceNew: true });
  235. socket.emit('doge');
  236. socket.on('doge', function(buffer){
  237. expect(buffer instanceof ArrayBuffer).to.be(true);
  238. socket.disconnect();
  239. done();
  240. });
  241. });
  242. it('should send binary data (as an ArrayBuffer)', function(done){
  243. var socket = io({ forceNew: true });
  244. socket.on('buffack', function(){
  245. socket.disconnect();
  246. done();
  247. });
  248. var buf = base64.decode("asdfasdf");
  249. socket.emit('buffa', buf);
  250. });
  251. it('should send binary data (as an ArrayBuffer) mixed with json', function(done) {
  252. var socket = io({ forceNew: true });
  253. socket.on('jsonbuff-ack', function() {
  254. socket.disconnect();
  255. done();
  256. });
  257. var buf = base64.decode("howdy");
  258. socket.emit('jsonbuff', {hello: 'lol', message: buf, goodbye: 'gotcha'});
  259. });
  260. it('should send events with ArrayBuffers in the correct order', function(done) {
  261. var socket = io({ forceNew: true });
  262. socket.on('abuff2-ack', function() {
  263. socket.disconnect();
  264. done();
  265. });
  266. var buf = base64.decode("abuff1");
  267. socket.emit('abuff1', buf);
  268. socket.emit('abuff2', 'please arrive second');
  269. });
  270. }
  271. if (global.Blob && null != textBlobBuilder('xxx')) {
  272. it('should send binary data (as a Blob)', function(done){
  273. var socket = io({ forceNew: true });
  274. socket.on('back', function(){
  275. socket.disconnect();
  276. done();
  277. });
  278. var blob = textBlobBuilder('hello world');
  279. socket.emit('blob', blob);
  280. });
  281. it('should send binary data (as a Blob) mixed with json', function(done) {
  282. var socket = io({ forceNew: true });
  283. socket.on('jsonblob-ack', function() {
  284. socket.disconnect();
  285. done();
  286. });
  287. var blob = textBlobBuilder('EEEEEEEEE');
  288. socket.emit('jsonblob', {hello: 'lol', message: blob, goodbye: 'gotcha'});
  289. });
  290. it('should send events with Blobs in the correct order', function(done) {
  291. var socket = io({ forceNew: true });
  292. socket.on('blob3-ack', function() {
  293. socket.disconnect();
  294. done();
  295. });
  296. var blob = textBlobBuilder('BLOBBLOB');
  297. socket.emit('blob1', blob);
  298. socket.emit('blob2', 'second');
  299. socket.emit('blob3', blob);
  300. });
  301. }
  302. });