4 * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
8 (function (module, io, should) {
10 if ('object' == typeof global) {
11 return module.exports = { '': function () {} };
16 'test connecting the socket and disconnecting': function (next) {
17 var socket = create();
19 socket.on('error', function (msg) {
20 throw new Error(msg || 'Received an error');
23 socket.on('connect', function () {
29 'test receiving messages': function (next) {
34 socket.on('error', function (msg) {
35 throw new Error(msg || 'Received an error');
38 socket.on('connect', function () {
42 socket.on('message', function (i) {
43 String(++messages).should().equal(i);
46 socket.on('disconnect', function (reason) {
47 connected.should().be_true;
48 messages.should().equal(3);
49 reason.should().eql('booted');
54 'test sending messages': function (next) {
55 var socket = create();
57 socket.on('error', function (msg) {
58 throw new Error(msg || 'Received an error');
61 socket.on('connect', function () {
64 socket.on('message', function (msg) {
65 msg.should().equal('echo');
72 'test manual buffer flushing': function (next) {
73 var socket = create();
75 socket.socket.options['manualFlush'] = true;
77 socket.on('error', function (msg) {
78 throw new Error(msg || 'Received an error');
81 socket.on('connect', function () {
82 socket.socket.connected = false;
83 socket.send('buffered');
84 socket.socket.onConnect();
85 socket.socket.flushBuffer();
87 socket.on('message', function (msg) {
88 msg.should().equal('buffered');
95 'test automatic buffer flushing': function (next) {
96 var socket = create();
98 socket.on('error', function (msg) {
99 throw new Error(msg || 'Received an error');
102 socket.on('connect', function () {
103 socket.socket.connected = false;
104 socket.send('buffered');
105 socket.socket.onConnect();
107 socket.on('message', function (msg) {
108 msg.should().equal('buffered');
115 'test acks sent from client': function (next) {
116 var socket = create();
118 socket.on('error', function (msg) {
119 throw new Error(msg || 'Received an error');
122 socket.on('connect', function () {
123 socket.on('message', function (msg) {
124 if ('tobi 2' == msg) {
132 'test acks sent from server': function (next) {
133 var socket = create();
135 socket.on('error', function (msg) {
136 throw new Error(msg || 'Received an error');
139 socket.on('connect', function () {
140 socket.send('ooo', function () {
147 'test connecting to namespaces': function (next) {
154 socket.of('').disconnect();
155 connect.should().equal(3);
159 socket.on('connect', function(){
163 socket.of('/woot').on('connect', function () {
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');
172 socket.of('/chat').on('connect', function () {
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');
182 'test disconnecting from namespaces': function (next) {
183 var socket = create().socket
185 , disconnections = 0;
188 socket.of('').disconnect();
192 socket.of('/a').on('error', function (msg) {
193 throw new Error(msg || 'Received an error');
196 socket.of('/a').on('connect', function () {
197 socket.of('/a').disconnect();
200 socket.of('/a').on('disconnect', function () {
201 --namespaces || finish();
204 socket.of('/b').on('error', function (msg) {
205 throw new Error(msg || 'Received an error');
208 socket.of('/b').on('connect', function () {
209 socket.of('/b').disconnect();
212 socket.of('/b').on('disconnect', function () {
213 --namespaces || finish();
217 'test authorizing for namespaces': function (next) {
218 var socket = create().socket
221 socket.of('').disconnect();
226 .on('connect_failed', function (msg) {
229 .on('error', function (msg) {
230 throw new Error(msg || 'Received an error');
234 'test sending json from server': function (next) {
235 var socket = create();
237 socket.on('error', function (msg) {
238 throw new Error(msg || 'Received an error');
241 socket.on('message', function (msg) {
242 msg.should().eql(3141592);
248 'test sending json from client': function (next) {
249 var socket = create();
251 socket.on('error', function (msg) {
252 throw new Error(msg || 'Received an error');
255 socket.json.send([1, 2, 3]);
256 socket.on('message', function (msg) {
257 msg.should().equal('echo');
263 'test emitting an event from server': function (next) {
264 var socket = create();
266 socket.on('error', function (msg) {
267 throw new Error(msg || 'Received an error');
270 socket.on('woot', function () {
276 'test emitting an event to server': function (next) {
277 var socket = create();
279 socket.on('error', function (msg) {
280 throw new Error(msg || 'Received an error');
284 socket.on('echo', function () {
290 'test emitting multiple events at once to the server': function (next) {
291 var socket = create();
293 socket.on('connect', function () {
294 socket.emit('print', 'foo');
295 socket.emit('print', 'bar');
298 socket.on('done', function () {
304 'test emitting an event from server and sending back data': function (next) {
305 var socket = create();
307 socket.on('error', function (msg) {
308 throw new Error(msg || 'Received an error');
311 socket.on('woot', function (a, fn) {
315 socket.on('done', function () {
322 'test emitting an event to server and sending back data': function (next) {
323 var socket = create();
325 socket.on('error', function (msg) {
326 throw new Error(msg || 'Received an error');
329 socket.emit('tobi', 1, 2, function (a) {
330 a.should().eql({ hello: 'world' });
336 'test encoding a payload': function (next) {
337 var socket = create('/woot');
339 socket.on('error', function (msg) {
340 throw new Error(msg || 'Received an error');
343 socket.on('connect', function () {
344 socket.socket.setBuffer(true);
349 socket.socket.setBuffer(false);
352 socket.on('done', function () {
358 'test sending query strings to the server': function (next) {
359 var socket = create('?foo=bar');
361 socket.on('error', function (msg) {
362 throw new Error(msg || 'Received an error');
365 socket.on('message', function (data) {
366 data.query.foo.should().eql('bar');
373 'test sending newline': function (next) {
374 var socket = create();
376 socket.on('error', function (msg) {
377 throw new Error(msg || 'Received an error');
382 socket.on('done', function () {
388 'test sending unicode': function (next) {
389 var socket = create();
391 socket.on('error', function (msg) {
392 throw new Error(msg || 'Received an error');
395 socket.json.send({ test: "☃" });
397 socket.on('done', function () {
403 'test webworker connection': function (next) {
404 if (!window.Worker) {
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);
419 'undefined' == typeof module ? module = {} : module
420 , 'undefined' == typeof io ? require('socket.io-client') : io
421 , 'undefined' == typeof should ? require('should-browser') : should