PageRenderTime 51ms CodeModel.GetById 1ms app.highlight 46ms RepoModel.GetById 1ms 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
  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 acks sent from client': function (next) {
 73      var socket = create();
 74
 75      socket.on('error', function (msg) {
 76        throw new Error(msg || 'Received an error');
 77      });
 78
 79      socket.on('connect', function () {
 80        socket.on('message', function (msg) {
 81          if ('tobi 2' == msg) {
 82            socket.disconnect();
 83            next();
 84          }
 85        });
 86      });
 87    },
 88
 89    'test acks sent from server': function (next) {
 90      var socket = create();
 91
 92      socket.on('error', function (msg) {
 93        throw new Error(msg || 'Received an error');
 94      });
 95
 96      socket.on('connect', function () {
 97        socket.send('ooo', function () {
 98          socket.disconnect();
 99          next();
100        });
101      });
102    },
103
104    'test connecting to namespaces': function (next) {
105      var io = create()
106        , socket = io.socket
107        , namespaces = 2
108        , connect = 0;
109
110      function finish () {
111        socket.of('').disconnect();
112        connect.should().equal(3);
113        next();
114      }
115
116      socket.on('connect', function(){
117        connect++;
118      });
119
120      socket.of('/woot').on('connect', function () {
121        connect++;
122      }).on('message', function (msg) {
123        msg.should().equal('connected to woot');
124        --namespaces || finish();
125      }).on('error', function (msg) {
126        throw new Error(msg || 'Received an error');
127      });
128
129      socket.of('/chat').on('connect', function () {
130        connect++;
131      }).on('message', function (msg) {
132        msg.should().equal('connected to chat');
133        --namespaces || finish();
134      }).on('error', function (msg) {
135        throw new Error(msg || 'Received an error');
136      });
137    },
138
139    'test disconnecting from namespaces': function (next) {
140      var socket = create().socket
141        , namespaces = 2
142        , disconnections = 0;
143
144      function finish () {
145        socket.of('').disconnect();
146        next();
147      };
148
149      socket.of('/a').on('error', function (msg) {
150        throw new Error(msg || 'Received an error');
151      });
152
153      socket.of('/a').on('connect', function () {
154        socket.of('/a').disconnect();
155      });
156
157      socket.of('/a').on('disconnect', function () {
158        --namespaces || finish();
159      });
160
161      socket.of('/b').on('error', function (msg) {
162        throw new Error(msg || 'Received an error');
163      });
164
165      socket.of('/b').on('connect', function () {
166        socket.of('/b').disconnect();
167      });
168
169      socket.of('/b').on('disconnect', function () {
170        --namespaces || finish();
171      });
172    },
173
174    'test authorizing for namespaces': function (next) {
175      var socket = create().socket
176
177      function finish () {
178        socket.of('').disconnect();
179        next();
180      };
181
182      socket.of('/a')
183        .on('connect_failed', function (msg) {
184          next();
185        })
186        .on('error', function (msg) {
187          throw new Error(msg || 'Received an error');
188        });
189    },
190
191    'test sending json from server': function (next) {
192      var socket = create();
193
194      socket.on('error', function (msg) {
195        throw new Error(msg || 'Received an error');
196      });
197
198      socket.on('message', function (msg) {
199        msg.should().eql(3141592);
200        socket.disconnect();
201        next();
202      });
203    },
204
205    'test sending json from client': function (next) {
206      var socket = create();
207
208      socket.on('error', function (msg) {
209        throw new Error(msg || 'Received an error');
210      });
211
212      socket.json.send([1, 2, 3]);
213      socket.on('message', function (msg) {
214        msg.should().equal('echo');
215        socket.disconnect();
216        next();
217      });
218    },
219
220    'test emitting an event from server': function (next) {
221      var socket = create();
222
223      socket.on('error', function (msg) {
224        throw new Error(msg || 'Received an error');
225      });
226
227      socket.on('woot', function () {
228        socket.disconnect();
229        next();
230      });
231    },
232
233    'test emitting an event to server': function (next) {
234      var socket = create();
235
236      socket.on('error', function (msg) {
237        throw new Error(msg || 'Received an error');
238      });
239
240      socket.emit('woot');
241      socket.on('echo', function () {
242        socket.disconnect();
243        next();
244      })
245    },
246
247    'test emitting multiple events at once to the server': function (next) {
248      var socket = create();
249
250      socket.on('connect', function () {
251        socket.emit('print', 'foo');
252        socket.emit('print', 'bar');
253      });
254
255      socket.on('done', function () {
256        socket.disconnect();
257        next();
258      });
259    },
260
261    'test emitting an event from server and sending back data': function (next) {
262      var socket = create();
263
264      socket.on('error', function (msg) {
265        throw new Error(msg || 'Received an error');
266      });
267
268      socket.on('woot', function (a, fn) {
269        a.should().eql(1);
270        fn('test');
271
272        socket.on('done', function () {
273          socket.disconnect();
274          next();
275        });
276      });
277    },
278
279    'test emitting an event to server and sending back data': function (next) {
280      var socket = create();
281
282      socket.on('error', function (msg) {
283        throw new Error(msg || 'Received an error');
284      });
285
286      socket.emit('tobi', 1, 2, function (a) {
287        a.should().eql({ hello: 'world' });
288        socket.disconnect();
289        next();
290      });
291    },
292
293    'test encoding a payload': function (next) {
294      var socket = create('/woot');
295
296      socket.on('error', function (msg) {
297        throw new Error(msg || 'Received an error');
298      });
299
300      socket.on('connect', function () {
301        socket.socket.setBuffer(true);
302        socket.send('単');
303        socket.send('単');
304        socket.send('単');
305        socket.send('単');
306        socket.socket.setBuffer(false);
307      });
308
309      socket.on('done', function () {
310        socket.disconnect();
311        next();
312      });
313    },
314
315    'test sending query strings to the server': function (next) {
316      var socket = create('?foo=bar');
317
318      socket.on('error', function (msg) {
319        throw new Error(msg || 'Received an error');
320      });
321
322      socket.on('message', function (data) {
323        data.query.foo.should().eql('bar');
324
325        socket.disconnect();
326        next();
327      });
328    },
329
330    'test sending newline': function (next) {
331      var socket = create();
332
333      socket.on('error', function (msg) {
334        throw new Error(msg || 'Received an error');
335      });
336
337      socket.send('\n');
338
339      socket.on('done', function () {
340        socket.disconnect();
341        next();
342      });
343    },
344
345    'test sending unicode': function (next) {
346      var socket = create();
347
348      socket.on('error', function (msg) {
349        throw new Error(msg || 'Received an error');
350      });
351
352      socket.json.send({ test: "\u2028" });
353
354      socket.on('done', function () {
355        socket.disconnect();
356        next();
357      });
358    }
359
360  };
361
362})(
363    'undefined' == typeof module ? module = {} : module
364  , 'undefined' == typeof io ? require('socket.io-client') : io
365  , 'undefined' == typeof should ? require('should-browser') : should
366);