| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422 |
- /*!
- * socket.io-node
- * Copyright(c) 2011 LearnBoost <dev@learnboost.com>
- * MIT Licensed
- */
- (function (module, io, should) {
- if ('object' == typeof global) {
- return module.exports = { '': function () {} };
- }
- module.exports = {
- 'test connecting the socket and disconnecting': function (next) {
- var socket = create();
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.on('connect', function () {
- socket.disconnect();
- next();
- });
- },
- 'test receiving messages': function (next) {
- var socket = create()
- , connected = false
- , messages = 0;
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.on('connect', function () {
- connected = true;
- });
- socket.on('message', function (i) {
- String(++messages).should().equal(i);
- });
- socket.on('disconnect', function (reason) {
- connected.should().be_true;
- messages.should().equal(3);
- reason.should().eql('booted');
- next();
- });
- },
- 'test sending messages': function (next) {
- var socket = create();
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.on('connect', function () {
- socket.send('echo');
- socket.on('message', function (msg) {
- msg.should().equal('echo');
- socket.disconnect();
- next();
- });
- });
- },
- 'test manual buffer flushing': function (next) {
- var socket = create();
- socket.socket.options['manualFlush'] = true;
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.on('connect', function () {
- socket.socket.connected = false;
- socket.send('buffered');
- socket.socket.onConnect();
- socket.socket.flushBuffer();
- socket.on('message', function (msg) {
- msg.should().equal('buffered');
- socket.disconnect();
- next();
- });
- });
- },
- 'test automatic buffer flushing': function (next) {
- var socket = create();
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.on('connect', function () {
- socket.socket.connected = false;
- socket.send('buffered');
- socket.socket.onConnect();
- socket.on('message', function (msg) {
- msg.should().equal('buffered');
- socket.disconnect();
- next();
- });
- });
- },
- 'test acks sent from client': function (next) {
- var socket = create();
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.on('connect', function () {
- socket.on('message', function (msg) {
- if ('tobi 2' == msg) {
- socket.disconnect();
- next();
- }
- });
- });
- },
- 'test acks sent from server': function (next) {
- var socket = create();
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.on('connect', function () {
- socket.send('ooo', function () {
- socket.disconnect();
- next();
- });
- });
- },
- 'test connecting to namespaces': function (next) {
- var io = create()
- , socket = io.socket
- , namespaces = 2
- , connect = 0;
- function finish () {
- socket.of('').disconnect();
- connect.should().equal(3);
- next();
- }
- socket.on('connect', function(){
- connect++;
- });
- socket.of('/woot').on('connect', function () {
- connect++;
- }).on('message', function (msg) {
- msg.should().equal('connected to woot');
- --namespaces || finish();
- }).on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.of('/chat').on('connect', function () {
- connect++;
- }).on('message', function (msg) {
- msg.should().equal('connected to chat');
- --namespaces || finish();
- }).on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- },
- 'test disconnecting from namespaces': function (next) {
- var socket = create().socket
- , namespaces = 2
- , disconnections = 0;
- function finish () {
- socket.of('').disconnect();
- next();
- };
- socket.of('/a').on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.of('/a').on('connect', function () {
- socket.of('/a').disconnect();
- });
- socket.of('/a').on('disconnect', function () {
- --namespaces || finish();
- });
- socket.of('/b').on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.of('/b').on('connect', function () {
- socket.of('/b').disconnect();
- });
- socket.of('/b').on('disconnect', function () {
- --namespaces || finish();
- });
- },
- 'test authorizing for namespaces': function (next) {
- var socket = create().socket
- function finish () {
- socket.of('').disconnect();
- next();
- };
- socket.of('/a')
- .on('connect_failed', function (msg) {
- next();
- })
- .on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- },
- 'test sending json from server': function (next) {
- var socket = create();
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.on('message', function (msg) {
- msg.should().eql(3141592);
- socket.disconnect();
- next();
- });
- },
- 'test sending json from client': function (next) {
- var socket = create();
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.json.send([1, 2, 3]);
- socket.on('message', function (msg) {
- msg.should().equal('echo');
- socket.disconnect();
- next();
- });
- },
- 'test emitting an event from server': function (next) {
- var socket = create();
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.on('woot', function () {
- socket.disconnect();
- next();
- });
- },
- 'test emitting an event to server': function (next) {
- var socket = create();
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.emit('woot');
- socket.on('echo', function () {
- socket.disconnect();
- next();
- })
- },
- 'test emitting multiple events at once to the server': function (next) {
- var socket = create();
- socket.on('connect', function () {
- socket.emit('print', 'foo');
- socket.emit('print', 'bar');
- });
- socket.on('done', function () {
- socket.disconnect();
- next();
- });
- },
- 'test emitting an event from server and sending back data': function (next) {
- var socket = create();
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.on('woot', function (a, fn) {
- a.should().eql(1);
- fn('test');
- socket.on('done', function () {
- socket.disconnect();
- next();
- });
- });
- },
- 'test emitting an event to server and sending back data': function (next) {
- var socket = create();
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.emit('tobi', 1, 2, function (a) {
- a.should().eql({ hello: 'world' });
- socket.disconnect();
- next();
- });
- },
- 'test encoding a payload': function (next) {
- var socket = create('/woot');
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.on('connect', function () {
- socket.socket.setBuffer(true);
- socket.send('ñ');
- socket.send('ñ');
- socket.send('ñ');
- socket.send('ñ');
- socket.socket.setBuffer(false);
- });
- socket.on('done', function () {
- socket.disconnect();
- next();
- });
- },
- 'test sending query strings to the server': function (next) {
- var socket = create('?foo=bar');
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.on('message', function (data) {
- data.query.foo.should().eql('bar');
- socket.disconnect();
- next();
- });
- },
- 'test sending newline': function (next) {
- var socket = create();
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.send('\n');
- socket.on('done', function () {
- socket.disconnect();
- next();
- });
- },
- 'test sending unicode': function (next) {
- var socket = create();
- socket.on('error', function (msg) {
- throw new Error(msg || 'Received an error');
- });
- socket.json.send({ test: "☃" });
- socket.on('done', function () {
- socket.disconnect();
- next();
- });
- },
- 'test webworker connection': function (next) {
- if (!window.Worker) {
- return next();
- }
- var worker = new Worker('/test/worker.js');
- worker.postMessage(uri());
- worker.onmessage = function (ev) {
- if ('done!' == ev.data) return next();
- throw new Error('Unexpected message: ' + ev.data);
- }
- }
- };
- })(
- 'undefined' == typeof module ? module = {} : module
- , 'undefined' == typeof io ? require('socket.io-client') : io
- , 'undefined' == typeof should ? require('should-browser') : should
- );
|