Files
socket.io/test/socket.test.js
2011-06-19 21:18:15 -03:00

105 lines
2.4 KiB
JavaScript

/*!
* 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('connect', function () {
socket.disconnect();
next();
});
},
'test receiving messages': function (next) {
var socket = create()
, connected = false
, messages = 0;
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('connect', function () {
socket.send('echo');
socket.on('message', function (msg) {
msg.should().equal('echo');
socket.disconnect();
next();
});
});
},
'test acks sent from client': function (next) {
var socket = create();
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('connect', function () {
socket.send('ooo', function () {
socket.disconnect();
next();
});
});
},
'test connecting to namespaces': function (next) {
var socket = create().socket
, namespaces = 2;
socket.of('/woot').on('message', function (msg) {
msg.should().equal('connected to woot');
--namespaces || next();
});
socket.of('/chat').on('message', function (msg) {
msg.should().equal('connected to chat');
--namespaces || next();
});
}
};
})(
'undefined' == typeof module ? module = {} : module
, 'undefined' == typeof io ? require('socket.io-client') : io
, 'undefined' == typeof should ? require('should-browser') : should
);