Bug:Fix file validation issue
[vnfsdk/refrepo.git] / vnfmarket / src / main / webapp / vnfmarket / node_modules / socket.io-client / test / socket.test.js
1
2 /*!
3  * socket.io-node
4  * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
5  * MIT Licensed
6  */
7
8 (function (module, io, should) {
9
10   if ('object' == typeof global) {
11     return module.exports = { '': function () {} };
12   }
13
14   module.exports = {
15
16     'test connecting the socket and disconnecting': function (next) {
17       var socket = create();
18
19       socket.on('error', function (msg) {
20         throw new Error(msg || 'Received an error');
21       });
22
23       socket.on('connect', function () {
24         socket.disconnect();
25         next();
26       });
27     },
28
29     'test receiving messages': function (next) {
30       var socket = create()
31         , connected = false
32         , messages = 0;
33
34       socket.on('error', function (msg) {
35         throw new Error(msg || 'Received an error');
36       });
37
38       socket.on('connect', function () {
39         connected = true;
40       });
41
42       socket.on('message', function (i) {
43         String(++messages).should().equal(i);
44       });
45
46       socket.on('disconnect', function (reason) {
47         connected.should().be_true;
48         messages.should().equal(3);
49         reason.should().eql('booted');
50         next();
51       });
52     },
53
54     'test sending messages': function (next) {
55       var socket = create();
56
57       socket.on('error', function (msg) {
58         throw new Error(msg || 'Received an error');
59       });
60
61       socket.on('connect', function () {
62         socket.send('echo');
63
64         socket.on('message', function (msg) {
65           msg.should().equal('echo');
66           socket.disconnect();
67           next();
68         });
69       });
70     },
71
72     'test manual buffer flushing': function (next) {
73       var socket = create();
74
75       socket.socket.options['manualFlush'] = true;
76
77       socket.on('error', function (msg) {
78         throw new Error(msg || 'Received an error');
79       });
80
81       socket.on('connect', function () {
82         socket.socket.connected = false;
83         socket.send('buffered');
84         socket.socket.onConnect();
85         socket.socket.flushBuffer();
86
87         socket.on('message', function (msg) {
88           msg.should().equal('buffered');
89           socket.disconnect();
90           next();
91         });
92       });
93     },
94
95     'test automatic buffer flushing': function (next) {
96       var socket = create();
97
98       socket.on('error', function (msg) {
99         throw new Error(msg || 'Received an error');
100       });
101
102       socket.on('connect', function () {
103         socket.socket.connected = false;
104         socket.send('buffered');
105         socket.socket.onConnect();
106
107         socket.on('message', function (msg) {
108           msg.should().equal('buffered');
109           socket.disconnect();
110           next();
111         });
112       });
113     },
114
115     'test acks sent from client': function (next) {
116       var socket = create();
117
118       socket.on('error', function (msg) {
119         throw new Error(msg || 'Received an error');
120       });
121
122       socket.on('connect', function () {
123         socket.on('message', function (msg) {
124           if ('tobi 2' == msg) {
125             socket.disconnect();
126             next();
127           }
128         });
129       });
130     },
131
132     'test acks sent from server': function (next) {
133       var socket = create();
134
135       socket.on('error', function (msg) {
136         throw new Error(msg || 'Received an error');
137       });
138
139       socket.on('connect', function () {
140         socket.send('ooo', function () {
141           socket.disconnect();
142           next();
143         });
144       });
145     },
146
147     'test connecting to namespaces': function (next) {
148       var io = create()
149         , socket = io.socket
150         , namespaces = 2
151         , connect = 0;
152
153       function finish () {
154         socket.of('').disconnect();
155         connect.should().equal(3);
156         next();
157       }
158
159       socket.on('connect', function(){
160         connect++;
161       });
162
163       socket.of('/woot').on('connect', function () {
164         connect++;
165       }).on('message', function (msg) {
166         msg.should().equal('connected to woot');
167         --namespaces || finish();
168       }).on('error', function (msg) {
169         throw new Error(msg || 'Received an error');
170       });
171
172       socket.of('/chat').on('connect', function () {
173         connect++;
174       }).on('message', function (msg) {
175         msg.should().equal('connected to chat');
176         --namespaces || finish();
177       }).on('error', function (msg) {
178         throw new Error(msg || 'Received an error');
179       });
180     },
181
182     'test disconnecting from namespaces': function (next) {
183       var socket = create().socket
184         , namespaces = 2
185         , disconnections = 0;
186
187       function finish () {
188         socket.of('').disconnect();
189         next();
190       };
191
192       socket.of('/a').on('error', function (msg) {
193         throw new Error(msg || 'Received an error');
194       });
195
196       socket.of('/a').on('connect', function () {
197         socket.of('/a').disconnect();
198       });
199
200       socket.of('/a').on('disconnect', function () {
201         --namespaces || finish();
202       });
203
204       socket.of('/b').on('error', function (msg) {
205         throw new Error(msg || 'Received an error');
206       });
207
208       socket.of('/b').on('connect', function () {
209         socket.of('/b').disconnect();
210       });
211
212       socket.of('/b').on('disconnect', function () {
213         --namespaces || finish();
214       });
215     },
216
217     'test authorizing for namespaces': function (next) {
218       var socket = create().socket
219
220       function finish () {
221         socket.of('').disconnect();
222         next();
223       };
224
225       socket.of('/a')
226         .on('connect_failed', function (msg) {
227           next();
228         })
229         .on('error', function (msg) {
230           throw new Error(msg || 'Received an error');
231         });
232     },
233
234     'test sending json from server': function (next) {
235       var socket = create();
236
237       socket.on('error', function (msg) {
238         throw new Error(msg || 'Received an error');
239       });
240
241       socket.on('message', function (msg) {
242         msg.should().eql(3141592);
243         socket.disconnect();
244         next();
245       });
246     },
247
248     'test sending json from client': function (next) {
249       var socket = create();
250
251       socket.on('error', function (msg) {
252         throw new Error(msg || 'Received an error');
253       });
254
255       socket.json.send([1, 2, 3]);
256       socket.on('message', function (msg) {
257         msg.should().equal('echo');
258         socket.disconnect();
259         next();
260       });
261     },
262
263     'test emitting an event from server': function (next) {
264       var socket = create();
265
266       socket.on('error', function (msg) {
267         throw new Error(msg || 'Received an error');
268       });
269
270       socket.on('woot', function () {
271         socket.disconnect();
272         next();
273       });
274     },
275
276     'test emitting an event to server': function (next) {
277       var socket = create();
278
279       socket.on('error', function (msg) {
280         throw new Error(msg || 'Received an error');
281       });
282
283       socket.emit('woot');
284       socket.on('echo', function () {
285         socket.disconnect();
286         next();
287       })
288     },
289
290     'test emitting multiple events at once to the server': function (next) {
291       var socket = create();
292
293       socket.on('connect', function () {
294         socket.emit('print', 'foo');
295         socket.emit('print', 'bar');
296       });
297
298       socket.on('done', function () {
299         socket.disconnect();
300         next();
301       });
302     },
303
304     'test emitting an event from server and sending back data': function (next) {
305       var socket = create();
306
307       socket.on('error', function (msg) {
308         throw new Error(msg || 'Received an error');
309       });
310
311       socket.on('woot', function (a, fn) {
312         a.should().eql(1);
313         fn('test');
314
315         socket.on('done', function () {
316           socket.disconnect();
317           next();
318         });
319       });
320     },
321
322     'test emitting an event to server and sending back data': function (next) {
323       var socket = create();
324
325       socket.on('error', function (msg) {
326         throw new Error(msg || 'Received an error');
327       });
328
329       socket.emit('tobi', 1, 2, function (a) {
330         a.should().eql({ hello: 'world' });
331         socket.disconnect();
332         next();
333       });
334     },
335
336     'test encoding a payload': function (next) {
337       var socket = create('/woot');
338
339       socket.on('error', function (msg) {
340         throw new Error(msg || 'Received an error');
341       });
342
343       socket.on('connect', function () {
344         socket.socket.setBuffer(true);
345         socket.send('ñ');
346         socket.send('ñ');
347         socket.send('ñ');
348         socket.send('ñ');
349         socket.socket.setBuffer(false);
350       });
351
352       socket.on('done', function () {
353         socket.disconnect();
354         next();
355       });
356     },
357
358     'test sending query strings to the server': function (next) {
359       var socket = create('?foo=bar');
360
361       socket.on('error', function (msg) {
362         throw new Error(msg || 'Received an error');
363       });
364
365       socket.on('message', function (data) {
366         data.query.foo.should().eql('bar');
367
368         socket.disconnect();
369         next();
370       });
371     },
372
373     'test sending newline': function (next) {
374       var socket = create();
375
376       socket.on('error', function (msg) {
377         throw new Error(msg || 'Received an error');
378       });
379
380       socket.send('\n');
381
382       socket.on('done', function () {
383         socket.disconnect();
384         next();
385       });
386     },
387
388     'test sending unicode': function (next) {
389       var socket = create();
390
391       socket.on('error', function (msg) {
392         throw new Error(msg || 'Received an error');
393       });
394
395       socket.json.send({ test: "☃" });
396
397       socket.on('done', function () {
398         socket.disconnect();
399         next();
400       });
401     },
402
403     'test webworker connection': function (next) {
404       if (!window.Worker) {
405         return next();
406       }
407
408       var worker = new Worker('/test/worker.js');
409       worker.postMessage(uri());
410       worker.onmessage = function (ev) {
411         if ('done!' == ev.data) return next();
412         throw new Error('Unexpected message: ' + ev.data);
413       }
414     }
415
416   };
417
418 })(
419     'undefined' == typeof module ? module = {} : module
420   , 'undefined' == typeof io ? require('socket.io-client') : io
421   , 'undefined' == typeof should ? require('should-browser') : should
422 );