Skip to content

Commit

Permalink
manager tests now pass properly
Browse files Browse the repository at this point in the history
  • Loading branch information
einaros committed Oct 24, 2011
1 parent c1e64b9 commit a1f0b6c
Show file tree
Hide file tree
Showing 3 changed files with 75 additions and 66 deletions.
27 changes: 12 additions & 15 deletions support/node-websocket-client/lib/websocket.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ var events = require('events');
var http = require('http');
var net = require('net');
var urllib = require('url');
var sys = require('sys');
var sys = require('util');

var FRAME_NO = 0;
var FRAME_LO = 1;
Expand Down Expand Up @@ -347,7 +347,6 @@ var WebSocket = function(url, proto, opts) {
// that we've closed.
var finishClose = self.finishClose = function() {
readyState = CLOSED;

if (stream) {
stream.end();
stream.destroy();
Expand Down Expand Up @@ -469,31 +468,29 @@ var WebSocket = function(url, proto, opts) {
// that http.Client passes its constructor arguments through,
// un-inspected to net.Stream.connect(). The latter accepts a
// string as its first argument to connect to a UNIX socket.
var httpClient = undefined;
var opt = {};
switch (getUrlScheme(url)) {
case 'ws':
var u = urllib.parse(url);
httpClient = http.createClient(u.port || 80, u.hostname);
httpPath = (u.pathname || '/') + (u.search || '');
httpHeaders.Host = u.hostname + (u.port ? (":" + u.port) : "");
opt.host = u.hostname;
opt.port = u.port || 80;
opt.path = (u.pathname || '/') + (u.search || '');
opt.headers = httpHeaders;
break;

case 'ws+unix':
var sockPath = url.substring('ws+unix://'.length, url.length);
httpClient = http.createClient(sockPath);
httpHeaders.Host = 'localhost';
var u = urllib.parse(url);
opt.host = 'localhost';
opt.path = sockPath;
opt.headers = httpHeaders;
break;

default:
throw new Error('Invalid URL scheme \'' + urlScheme + '\' specified.');
}

httpClient.on('error', function(e) {
// httpClient.destroy();
errorListener(e);
});

var httpReq = httpClient.request(httpPath, httpHeaders);
var httpReq = http.request(opt, function() { });
httpReq.on('upgrade', (function() {
var data = undefined;

Expand Down Expand Up @@ -560,7 +557,7 @@ var WebSocket = function(url, proto, opts) {
//
// XXX: This is lame. We should only remove the listeners
// that we added.
httpClient.removeAllListeners('upgrade');
httpReq.removeAllListeners('upgrade');
stream.removeAllListeners('data');
stream.on('data', dataListener);

Expand Down
102 changes: 55 additions & 47 deletions test/manager.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,7 @@ module.exports = {
io.disable('foo');

calls.should.eql(3);

done();
},

Expand Down Expand Up @@ -384,80 +385,79 @@ module.exports = {
done();
});
},

'test setting a custom heartbeat timeout': function (done) {
var port = ++ports
, io = sio.listen(port)
, cl = client(port);

io.configure(function () {
io.set('heartbeat timeout', 33);
});

cl.get('/socket.io/{protocol}/', function (res, data) {
res.statusCode.should.eql(200);
data.should.match(/([^:]+):33:([0-9]+)?:(.*)/);

cl.end();
io.server.close();
done();
});
},

'test disabling timeouts': function (done) {
var port = ++ports
, io = sio.listen(port)
, cl = client(port);

io.configure(function () {
io.set('heartbeat timeout', null);
io.set('close timeout', '');
});

cl.get('/socket.io/{protocol}/', function (res, data) {
res.statusCode.should.eql(200);
data.should.match(/([^:]+)::?:(.*)/);

cl.end();
io.server.close();
done();
});
},

'test disabling heartbeats': function (done) {
var port = ++ports
, io = sio.listen(port)
, cl = client(port)
, io = create(cl)
, messages = 0
, beat = false
, ws;

io.configure(function () {
io.disable('heartbeats');
io.set('heartbeat interval', .05);
io.set('heartbeat timeout', .05);
io.set('close timeout', .05);
});

io.sockets.on('connection', function (socket) {
setTimeout(function () {
socket.disconnect();
}, io.get('heartbeat timeout') * 1000 + 100);

socket.on('disconnect', function (reason) {
beat.should.be.false;

cl.end();
ws.finishClose();
cl.end();
io.server.close();
done();
});
});

cl.get('/socket.io/{protocol}/', function (res, data) {
res.statusCode.should.eql(200);
data.should.match(/([^:]+)::[\.0-9]+:(.*)/);

cl.handshake(function (sid) {
ws = websocket(cl, sid);
ws.on('message', function (packet) {
Expand All @@ -470,95 +470,103 @@ module.exports = {
});
});
},

'no duplicate room members': function (done) {
var port = ++ports
, io = sio.listen(port);

Object.keys(io.rooms).length.should.equal(0);

io.onJoin(123, 'foo');
io.rooms.foo.length.should.equal(1);

io.onJoin(123, 'foo');
io.rooms.foo.length.should.equal(1);

io.onJoin(124, 'foo');
io.rooms.foo.length.should.equal(2);

io.onJoin(124, 'foo');
io.rooms.foo.length.should.equal(2);

io.onJoin(123, 'bar');
io.rooms.foo.length.should.equal(2);
io.rooms.bar.length.should.equal(1);

io.onJoin(123, 'bar');
io.rooms.foo.length.should.equal(2);
io.rooms.bar.length.should.equal(1);

io.onJoin(124, 'bar');
io.rooms.foo.length.should.equal(2);
io.rooms.bar.length.should.equal(2);

io.onJoin(124, 'bar');
io.rooms.foo.length.should.equal(2);
io.rooms.bar.length.should.equal(2);

io.server.close();
done();

process.nextTick(function() {
io.server.close();
done();
});
},

'test passing options directly to the Manager through listen': function (done) {
var port = ++ports
, io = sio.listen(port, { resource: '/my resource', custom: 'opt' });

io.get('resource').should.equal('/my resource');
io.get('custom').should.equal('opt');
io.server.close();
done();
process.nextTick(function() {
io.server.close();
done();
});
},

'test disabling the log': function (done) {
var port = ++ports
, io = sio.listen(port, { log: false })
, _console = console.log
, calls = 0;

// the logger uses console.log to output data, override it to see if get's
// used
console.log = function () { ++calls };

io.log.debug('test');
io.log.log('testing');

console.log = _console;
calls.should.equal(0);

io.server.close();
done();

process.nextTick(function() {
io.server.close();
done();
});
},

'test disabling logging with colors': function (done) {
var port = ++ports
, io = sio.listen(port, { 'log colors': false })
, _console = console.log
, calls = 0;

// the logger uses console.log to output data, override it to see if get's
// used
console.log = function (data) {
++calls;
data.indexOf('\033').should.equal(-1);
};

io.log.debug('test');
io.log.log('testing');

console.log = _console;
calls.should.equal(2);

io.server.close();
done();

process.nextTick(function() {
io.server.close();
done();
});
}
};
12 changes: 8 additions & 4 deletions test/static.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,10 @@ module.exports = {
(!!io.static.has('/static/flashsocket/WebSocketMain.swf')).should.be.true;
(!!io.static.has('/static/flashsocket/WebSocketMainInsecure.swf')).should.be.true;

io.server.close();
done();
process.nextTick(function() {
io.server.close();
done();
});
},

'test that static files are correctly looked up': function (done) {
Expand All @@ -41,8 +43,10 @@ module.exports = {
(!!io.static.has('/socket.io.js')).should.be.true;
(!!io.static.has('/invalidfilehereplease.js')).should.be.false;

io.server.close();
done();
process.nextTick(function() {
io.server.close();
done();
});
},

'test that the client is served': function (done) {
Expand Down

0 comments on commit a1f0b6c

Please sign in to comment.