diff --git a/lib/manager.js b/lib/manager.js index 8d637fb21d..1915df4d0f 100644 --- a/lib/manager.js +++ b/lib/manager.js @@ -623,7 +623,12 @@ Manager.prototype.handleClient = function (data, req) { var transport = new transports[data.transport](this, data, req) , handshaken = this.handshaken[data.id]; - + + if (transport.disconnected) { + // failed during transport setup + req.connection.end(); + return; + } if (handshaken) { if (transport.open) { if (this.closed[data.id] && this.closed[data.id].length) { @@ -813,6 +818,7 @@ Manager.prototype.verifyOrigin = function (request) { if (origin) { try { var parts = url.parse(origin); + parts.port = parts.port || 80; var ok = ~origins.indexOf(parts.hostname + ':' + parts.port) || ~origins.indexOf(parts.hostname + ':*') || diff --git a/lib/transports/websocket/hybi-07-12.js b/lib/transports/websocket/hybi-07-12.js index ade99dc8b5..966365880b 100644 --- a/lib/transports/websocket/hybi-07-12.js +++ b/lib/transports/websocket/hybi-07-12.js @@ -159,6 +159,7 @@ WebSocket.prototype.verifyOrigin = function (origin) { if (origin) { try { var parts = url.parse(origin); + parts.port = parts.port || 80; var ok = ~origins.indexOf(parts.hostname + ':' + parts.port) || ~origins.indexOf(parts.hostname + ':*') || @@ -505,7 +506,9 @@ Parser.prototype.expect = function(what, length, handler) { */ Parser.prototype.processPacket = function (data) { - if ((data[0] & 0x70) != 0) this.error('reserved fields must be empty'); + if ((data[0] & 0x70) != 0) { + this.error('reserved fields must be empty'); + } this.state.lastFragment = (data[0] & 0x80) == 0x80; this.state.masked = (data[1] & 0x80) == 0x80; var opcode = data[0] & 0xf; diff --git a/lib/transports/websocket/hybi-16.js b/lib/transports/websocket/hybi-16.js index 03b01839a3..72ed89e2f6 100644 --- a/lib/transports/websocket/hybi-16.js +++ b/lib/transports/websocket/hybi-16.js @@ -159,6 +159,7 @@ WebSocket.prototype.verifyOrigin = function (origin) { if (origin) { try { var parts = url.parse(origin); + parts.port = parts.port || 80; var ok = ~origins.indexOf(parts.hostname + ':' + parts.port) || ~origins.indexOf(parts.hostname + ':*') || @@ -505,7 +506,10 @@ Parser.prototype.expect = function(what, length, handler) { */ Parser.prototype.processPacket = function (data) { - if ((data[0] & 0x70) != 0) this.error('reserved fields must be empty'); + if ((data[0] & 0x70) != 0) { + this.error('reserved fields must be empty'); + return; + } this.state.lastFragment = (data[0] & 0x80) == 0x80; this.state.masked = (data[1] & 0x80) == 0x80; var opcode = data[0] & 0xf; diff --git a/test/common.js b/test/common.js index 98aa502c28..059a8da51a 100644 --- a/test/common.js +++ b/test/common.js @@ -227,7 +227,7 @@ function WSClient (port, sid, transport) { this.transportName = transport || 'websocket'; WebSocket.call( this - , 'ws://localhost:' + port + '/socket.io/' + , 'ws://localhost:' + port + '/socket.io/' + io.protocol + '/' + this.transportName + '/' + sid ); }; diff --git a/test/hybi-common.js b/test/hybi-common.js index 31aad7259a..df080d9ffa 100644 --- a/test/hybi-common.js +++ b/test/hybi-common.js @@ -43,7 +43,7 @@ mask = function(buf, maskString) { if (typeof buf == 'string') buf = new Buffer(buf); var mask = getBufferFromHexString(maskString || '34 83 a8 68'); for (var i = 0; i < buf.length; ++i) { - buf[i] ^= mask[i % 4]; + buf[i] ^= mask[i % 4]; } return buf; } @@ -51,8 +51,8 @@ mask = function(buf, maskString) { /** * Returns a hex string representing the length of a message */ - -getHybiLengthAsHexString = function(len, masked) { + +getHybiLengthAsHexString = function(len, masked) { if (len < 126) { var buf = new Buffer(1); buf[0] = (masked ? 0x80 : 0) | len; @@ -94,6 +94,6 @@ pack = function(length, number) { * Left pads the string 's' to a total length of 'n' with char 'c'. */ -padl = function(s, n, c) { +padl = function(s, n, c) { return new Array(1 + n - s.length).join(c) + s; } diff --git a/test/manager.test.js b/test/manager.test.js index 4d4cad37af..ca5daddf5b 100644 --- a/test/manager.test.js +++ b/test/manager.test.js @@ -277,6 +277,23 @@ module.exports = { }); }, + 'test that a referer with implicit port 80 is accepted for foo.bar.com:80 origin': function (done) { + var port = ++ports + , io = sio.listen(port) + , cl = client(port); + + io.configure(function () { + io.set('origins', 'foo.bar.com:80'); + }); + + cl.get('/socket.io/{protocol}', { headers: { referer: 'http://foo.bar.com/something' } }, function (res, data) { + res.statusCode.should.eql(200); + cl.end(); + io.server.close(); + done(); + }); + }, + 'test that erroneous referer is denied for addr:* origin': function (done) { var port = ++ports , io = sio.listen(port) diff --git a/test/static.test.js b/test/static.test.js index 3a45ab7bdf..94564213c8 100644 --- a/test/static.test.js +++ b/test/static.test.js @@ -94,7 +94,7 @@ module.exports = { 'test that the client is build with the enabled transports': function (done) { var port = ++ports - , io = sio.listen(port) + , io = sio.listen(port) , cl = client(port); io.set('transports', ['websocket']); diff --git a/test/stores.memory.test.js b/test/stores.memory.test.js index ecf9c96ddc..f55ed25b31 100644 --- a/test/stores.memory.test.js +++ b/test/stores.memory.test.js @@ -45,7 +45,7 @@ module.exports = { client.set('b', 'c', function (err) { should.strictEqual(err, null); - + client.set('c', 'd', function (err) { should.strictEqual(err, null); diff --git a/test/stores.redis.test.js b/test/stores.redis.test.js index 69848f32a4..2ce347403e 100644 --- a/test/stores.redis.test.js +++ b/test/stores.redis.test.js @@ -95,7 +95,7 @@ module.exports = { client.set('b', 'c', function (err) { should.strictEqual(err, null); - + client.set('c', 'd', function (err) { should.strictEqual(err, null); diff --git a/test/transports.flashsocket.test.js b/test/transports.flashsocket.test.js index e857be3174..7c6809e3a4 100644 --- a/test/transports.flashsocket.test.js +++ b/test/transports.flashsocket.test.js @@ -30,7 +30,7 @@ function FlashSocket (port, sid) { WebSocket.call( this - , 'ws://localhost:' + port + '/socket.io/' + , 'ws://localhost:' + port + '/socket.io/' + sio.protocol + '/flashsocket/' + sid ); }; diff --git a/test/transports.websocket.hybi07-12.parser.test.js b/test/transports.websocket.hybi07-12.parser.test.js index cc00b13120..dce7effd71 100644 --- a/test/transports.websocket.hybi07-12.parser.test.js +++ b/test/transports.websocket.hybi07-12.parser.test.js @@ -2,7 +2,7 @@ * Test dependencies. */ -var assert = require('assert'); +var assert = require('assert'); var Parser = require('../lib/transports/websocket/hybi-07-12.js').Parser; require('./hybi-common'); @@ -14,38 +14,38 @@ module.exports = { 'can parse unmasked text message': function() { var p = new Parser(); var packet = '81 05 48 65 6c 6c 6f'; - + var gotData = false; p.on('data', function(data) { gotData = true; assert.equal('Hello', data); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, 'can parse close message': function() { var p = new Parser(); var packet = '88 00'; - + var gotClose = false; p.on('close', function(data) { gotClose = true; }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotClose); }, 'can parse masked text message': function() { var p = new Parser(); var packet = '81 93 34 83 a8 68 01 b9 92 52 4f a1 c6 09 59 e6 8a 52 16 e6 cb 00 5b a1 d5'; - + var gotData = false; p.on('data', function(data) { gotData = true; assert.equal('5:::{"name":"echo"}', data); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, @@ -54,13 +54,13 @@ module.exports = { var message = 'A'; for (var i = 0; i < 300; ++i) message += (i % 5).toString(); var packet = '81 FE ' + pack(4, message.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68')); - + var gotData = false; p.on('data', function(data) { gotData = true; assert.equal(message, data); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, @@ -75,7 +75,7 @@ module.exports = { gotData = true; assert.equal(message, data); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, @@ -87,13 +87,13 @@ module.exports = { var msgpiece2 = message.substr(150); var packet1 = '01 FE ' + pack(4, msgpiece1.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece1, '34 83 a8 68')); var packet2 = '80 FE ' + pack(4, msgpiece2.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece2, '34 83 a8 68')); - + var gotData = false; p.on('data', function(data) { - gotData = true; + gotData = true; assert.equal(message, data); }); - + p.add(getBufferFromHexString(packet1)); p.add(getBufferFromHexString(packet2)); assert.ok(gotData); @@ -102,25 +102,25 @@ module.exports = { var p = new Parser(); var message = 'Hello'; var packet = '89 FE ' + pack(4, message.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68')); - + var gotPing = false; p.on('ping', function(data) { gotPing = true; assert.equal(message, data); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotPing); }, 'can parse a ping with no data': function() { var p = new Parser(); var packet = '89 00'; - + var gotPing = false; p.on('ping', function(data) { gotPing = true; }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotPing); }, @@ -128,16 +128,16 @@ module.exports = { var p = new Parser(); var message = 'A'; for (var i = 0; i < 300; ++i) message += (i % 5).toString(); - + var msgpiece1 = message.substr(0, 150); var packet1 = '01 FE ' + pack(4, msgpiece1.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece1, '34 83 a8 68')); - + var pingMessage = 'Hello'; var pingPacket = '89 FE ' + pack(4, pingMessage.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(pingMessage, '34 83 a8 68')); - + var msgpiece2 = message.substr(150); var packet2 = '80 FE ' + pack(4, msgpiece2.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece2, '34 83 a8 68')); - + var gotData = false; p.on('data', function(data) { gotData = true; @@ -148,7 +148,7 @@ module.exports = { gotPing = true; assert.equal(pingMessage, data); }); - + p.add(getBufferFromHexString(packet1)); p.add(getBufferFromHexString(pingPacket)); p.add(getBufferFromHexString(packet2)); @@ -159,16 +159,16 @@ module.exports = { var p = new Parser(); var message = 'A'; for (var i = 0; i < 300; ++i) message += (i % 5).toString(); - + var msgpiece1 = message.substr(0, 150); var packet1 = '01 FE ' + pack(4, msgpiece1.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece1, '34 83 a8 68')); - + var pingMessage = 'Hello'; var pingPacket = '89 FE ' + pack(4, pingMessage.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(pingMessage, '34 83 a8 68')); - + var msgpiece2 = message.substr(150); var packet2 = '80 FE ' + pack(4, msgpiece2.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece2, '34 83 a8 68')); - + var gotData = false; p.on('data', function(data) { gotData = true; @@ -179,7 +179,7 @@ module.exports = { gotPing = true; assert.equal(pingMessage, data); }); - + var buffers = []; buffers = buffers.concat(splitBuffer(getBufferFromHexString(packet1))); buffers = buffers.concat(splitBuffer(getBufferFromHexString(pingPacket))); @@ -195,7 +195,7 @@ module.exports = { var length = 100; var message = new Buffer(length); for (var i = 0; i < length; ++i) message[i] = i % 256; - var originalMessage = getHexStringFromBuffer(message); + var originalMessage = getHexStringFromBuffer(message); var packet = '82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68')); var gotData = false; @@ -203,7 +203,7 @@ module.exports = { gotData = true; assert.equal(originalMessage, getHexStringFromBuffer(data)); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, @@ -212,7 +212,7 @@ module.exports = { var length = 256; var message = new Buffer(length); for (var i = 0; i < length; ++i) message[i] = i % 256; - var originalMessage = getHexStringFromBuffer(message); + var originalMessage = getHexStringFromBuffer(message); var packet = '82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68')); var gotData = false; @@ -220,7 +220,7 @@ module.exports = { gotData = true; assert.equal(originalMessage, getHexStringFromBuffer(data)); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, @@ -229,7 +229,7 @@ module.exports = { var length = 200 * 1024; var message = new Buffer(length); for (var i = 0; i < length; ++i) message[i] = i % 256; - var originalMessage = getHexStringFromBuffer(message); + var originalMessage = getHexStringFromBuffer(message); var packet = '82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68')); var gotData = false; @@ -237,7 +237,7 @@ module.exports = { gotData = true; assert.equal(originalMessage, getHexStringFromBuffer(data)); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, @@ -246,7 +246,7 @@ module.exports = { var length = 200 * 1024; var message = new Buffer(length); for (var i = 0; i < length; ++i) message[i] = i % 256; - var originalMessage = getHexStringFromBuffer(message); + var originalMessage = getHexStringFromBuffer(message); var packet = '82 ' + getHybiLengthAsHexString(length, false) + ' ' + getHexStringFromBuffer(message); var gotData = false; @@ -254,7 +254,7 @@ module.exports = { gotData = true; assert.equal(originalMessage, getHexStringFromBuffer(data)); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, diff --git a/test/transports.websocket.hybi16.parser.test.js b/test/transports.websocket.hybi16.parser.test.js index e04d06af06..7294d114c7 100644 --- a/test/transports.websocket.hybi16.parser.test.js +++ b/test/transports.websocket.hybi16.parser.test.js @@ -2,7 +2,7 @@ * Test dependencies. */ -var assert = require('assert'); +var assert = require('assert'); var Parser = require('../lib/transports/websocket/hybi-16.js').Parser; require('./hybi-common'); @@ -14,38 +14,38 @@ module.exports = { 'can parse unmasked text message': function() { var p = new Parser(); var packet = '81 05 48 65 6c 6c 6f'; - + var gotData = false; p.on('data', function(data) { gotData = true; assert.equal('Hello', data); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, 'can parse close message': function() { var p = new Parser(); var packet = '88 00'; - + var gotClose = false; p.on('close', function(data) { gotClose = true; }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotClose); }, 'can parse masked text message': function() { var p = new Parser(); var packet = '81 93 34 83 a8 68 01 b9 92 52 4f a1 c6 09 59 e6 8a 52 16 e6 cb 00 5b a1 d5'; - + var gotData = false; p.on('data', function(data) { gotData = true; assert.equal('5:::{"name":"echo"}', data); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, @@ -54,13 +54,13 @@ module.exports = { var message = 'A'; for (var i = 0; i < 300; ++i) message += (i % 5).toString(); var packet = '81 FE ' + pack(4, message.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68')); - + var gotData = false; p.on('data', function(data) { gotData = true; assert.equal(message, data); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, @@ -75,7 +75,7 @@ module.exports = { gotData = true; assert.equal(message, data); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, @@ -87,13 +87,13 @@ module.exports = { var msgpiece2 = message.substr(150); var packet1 = '01 FE ' + pack(4, msgpiece1.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece1, '34 83 a8 68')); var packet2 = '80 FE ' + pack(4, msgpiece2.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece2, '34 83 a8 68')); - + var gotData = false; p.on('data', function(data) { - gotData = true; + gotData = true; assert.equal(message, data); }); - + p.add(getBufferFromHexString(packet1)); p.add(getBufferFromHexString(packet2)); assert.ok(gotData); @@ -102,25 +102,25 @@ module.exports = { var p = new Parser(); var message = 'Hello'; var packet = '89 FE ' + pack(4, message.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68')); - + var gotPing = false; p.on('ping', function(data) { gotPing = true; assert.equal(message, data); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotPing); }, 'can parse a ping with no data': function() { var p = new Parser(); var packet = '89 00'; - + var gotPing = false; p.on('ping', function(data) { gotPing = true; }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotPing); }, @@ -128,16 +128,16 @@ module.exports = { var p = new Parser(); var message = 'A'; for (var i = 0; i < 300; ++i) message += (i % 5).toString(); - + var msgpiece1 = message.substr(0, 150); var packet1 = '01 FE ' + pack(4, msgpiece1.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece1, '34 83 a8 68')); - + var pingMessage = 'Hello'; var pingPacket = '89 FE ' + pack(4, pingMessage.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(pingMessage, '34 83 a8 68')); - + var msgpiece2 = message.substr(150); var packet2 = '80 FE ' + pack(4, msgpiece2.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece2, '34 83 a8 68')); - + var gotData = false; p.on('data', function(data) { gotData = true; @@ -148,7 +148,7 @@ module.exports = { gotPing = true; assert.equal(pingMessage, data); }); - + p.add(getBufferFromHexString(packet1)); p.add(getBufferFromHexString(pingPacket)); p.add(getBufferFromHexString(packet2)); @@ -159,16 +159,16 @@ module.exports = { var p = new Parser(); var message = 'A'; for (var i = 0; i < 300; ++i) message += (i % 5).toString(); - + var msgpiece1 = message.substr(0, 150); var packet1 = '01 FE ' + pack(4, msgpiece1.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece1, '34 83 a8 68')); - + var pingMessage = 'Hello'; var pingPacket = '89 FE ' + pack(4, pingMessage.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(pingMessage, '34 83 a8 68')); - + var msgpiece2 = message.substr(150); var packet2 = '80 FE ' + pack(4, msgpiece2.length) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(msgpiece2, '34 83 a8 68')); - + var gotData = false; p.on('data', function(data) { gotData = true; @@ -179,7 +179,7 @@ module.exports = { gotPing = true; assert.equal(pingMessage, data); }); - + var buffers = []; buffers = buffers.concat(splitBuffer(getBufferFromHexString(packet1))); buffers = buffers.concat(splitBuffer(getBufferFromHexString(pingPacket))); @@ -195,7 +195,7 @@ module.exports = { var length = 100; var message = new Buffer(length); for (var i = 0; i < length; ++i) message[i] = i % 256; - var originalMessage = getHexStringFromBuffer(message); + var originalMessage = getHexStringFromBuffer(message); var packet = '82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68')); var gotData = false; @@ -203,7 +203,7 @@ module.exports = { gotData = true; assert.equal(originalMessage, getHexStringFromBuffer(data)); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, @@ -212,7 +212,7 @@ module.exports = { var length = 256; var message = new Buffer(length); for (var i = 0; i < length; ++i) message[i] = i % 256; - var originalMessage = getHexStringFromBuffer(message); + var originalMessage = getHexStringFromBuffer(message); var packet = '82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68')); var gotData = false; @@ -220,7 +220,7 @@ module.exports = { gotData = true; assert.equal(originalMessage, getHexStringFromBuffer(data)); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, @@ -229,7 +229,7 @@ module.exports = { var length = 200 * 1024; var message = new Buffer(length); for (var i = 0; i < length; ++i) message[i] = i % 256; - var originalMessage = getHexStringFromBuffer(message); + var originalMessage = getHexStringFromBuffer(message); var packet = '82 ' + getHybiLengthAsHexString(length, true) + ' 34 83 a8 68 ' + getHexStringFromBuffer(mask(message, '34 83 a8 68')); var gotData = false; @@ -237,7 +237,7 @@ module.exports = { gotData = true; assert.equal(originalMessage, getHexStringFromBuffer(data)); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, @@ -246,7 +246,7 @@ module.exports = { var length = 200 * 1024; var message = new Buffer(length); for (var i = 0; i < length; ++i) message[i] = i % 256; - var originalMessage = getHexStringFromBuffer(message); + var originalMessage = getHexStringFromBuffer(message); var packet = '82 ' + getHybiLengthAsHexString(length, false) + ' ' + getHexStringFromBuffer(message); var gotData = false; @@ -254,7 +254,7 @@ module.exports = { gotData = true; assert.equal(originalMessage, getHexStringFromBuffer(data)); }); - + p.add(getBufferFromHexString(packet)); assert.ok(gotData); }, diff --git a/test/transports.websocket.test.js b/test/transports.websocket.test.js index ff1fe715a4..914232f795 100644 --- a/test/transports.websocket.test.js +++ b/test/transports.websocket.test.js @@ -23,7 +23,7 @@ module.exports = { var cl = client(++ports) , io = create(cl) , ws; - + io.set('transports', ['websocket']); io.sockets.on('connection', function (socket) { socket.manager.transports[socket.id].name.should.equal('websocket'); @@ -35,8 +35,8 @@ module.exports = { cl.handshake(function (sid) { ws = websocket(cl, sid); }); - }, - + }, + 'default websocket draft parser is used for unknown sec-websocket-version': function (done) { var cl = client(++ports) , io = create(cl) @@ -113,6 +113,10 @@ module.exports = { io.set('transports', ['websocket']); io.set('origins', 'foo.bar.com:*'); + var notConnected = true; + io.sockets.on('connection', function() { + notConnected = false; + }); var headers = { 'sec-websocket-version': 8, @@ -121,23 +125,36 @@ module.exports = { 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==' } - // handshake uses correct origin -- we want to block the actuall websocket call + // handshake uses correct origin -- we want to block the actual websocket call cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) { var sid = data.split(':')[0]; var url = '/socket.io/' + sio.protocol + '/websocket/' + sid; - cl.get(url, {headers: headers}, function (res, data) {}); - cl.end(); - io.server.close(); - done(); + var req = cl.get(url, {headers: headers}, function (res, data) {}); + var closed = false; + req.on('close', function() { + if (closed) return; + closed = true; + notConnected.should.be.true; + cl.end(); + try { + io.server.close(); + } + catch (e) {} + done(); + }); }); }, 'hybi-16 origin filter blocks access for mismatched sec-websocket-origin': function (done) { var cl = client(++ports) - , io = create(cl) + , io = create(cl); io.set('transports', ['websocket']); io.set('origins', 'foo.bar.com:*'); + var notConnected = true; + io.sockets.on('connection', function() { + notConnected = false; + }); var headers = { 'sec-websocket-version': 13, @@ -146,14 +163,80 @@ module.exports = { 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==' } - // handshake uses correct origin -- we want to block the actuall websocket call + // handshake uses correct origin -- we want to block the actual websocket call + cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) { + var sid = data.split(':')[0]; + var url = '/socket.io/' + sio.protocol + '/websocket/' + sid; + var req = cl.get(url, {headers: headers}, function (res, data) {}); + var closed = false; + req.on('close', function() { + if (closed) return; + closed = true; + notConnected.should.be.true; + cl.end(); + try { + io.server.close(); + } + catch (e) {} + done(); + }); + }); + }, + + 'hybi-07-12 origin filter accepts implicit port 80 for sec-websocket-origin': function (done) { +done();return; + var cl = client(++ports) + , io = create(cl) + + io.set('transports', ['websocket']); + io.set('origins', 'foo.bar.com:80'); + + var headers = { + 'sec-websocket-version': 8, + 'upgrade': 'websocket', + 'Sec-WebSocket-Origin': 'http://foo.bar.com', + 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==' + } + + io.sockets.on('connection', function() { + cl.end(); + io.server.close(); + done(); + }); + + // handshake uses correct origin -- we want to block the actual websocket call + cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) { + var sid = data.split(':')[0]; + var url = '/socket.io/' + sio.protocol + '/websocket/' + sid; + cl.get(url, {headers: headers}, function (res, data) {}); + }); + }, + + 'hybi-16 origin filter accepts implicit port 80 for sec-websocket-origin': function (done) { + var cl = client(++ports) + , io = create(cl) + + io.set('transports', ['websocket']); + io.set('origins', 'foo.bar.com:80'); + + var headers = { + 'sec-websocket-version': 13, + 'upgrade': 'websocket', + 'origin': 'http://foo.bar.com', + 'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ==' + } + + io.sockets.on('connection', function() { + cl.end(); + io.server.close(); + done(); + }); + + // handshake uses correct origin -- we want to block the actual websocket call cl.get('/socket.io/{protocol}', {headers: {origin: 'http://foo.bar.com'}}, function (res, data) { var sid = data.split(':')[0]; var url = '/socket.io/' + sio.protocol + '/websocket/' + sid; cl.get(url, {headers: headers}, function (res, data) {}); - cl.end(); - io.server.close(); - done(); }); }, @@ -162,25 +245,25 @@ module.exports = { , io = create(cl) , messages = 0 , ws; - + io.configure(function () { io.set('heartbeat interval', .05); io.set('heartbeat timeout', .05); io.set('close timeout', 0); }); - + io.sockets.on('connection', function (socket) { socket.on('disconnect', function (reason) { beat.should.be.true; reason.should.eql('heartbeat timeout'); - + cl.end(); ws.finishClose(); io.server.close(); done(); }); }); - + cl.handshake(function (sid) { ws = websocket(cl, sid); ws.on('message', function (packet) { @@ -193,32 +276,32 @@ module.exports = { }); }); }, - + 'test that responding to a heartbeat maintains session': function (done) { var cl = client(++ports) , io = create(cl) , messages = 0 , heartbeats = 0 , ws; - + io.configure(function () { io.set('heartbeat interval', .05); io.set('heartbeat timeout', .05); io.set('close timeout', 0); }); - + io.sockets.on('connection', function (socket) { socket.on('disconnect', function (reason) { heartbeats.should.eql(2); reason.should.eql('heartbeat timeout'); - + cl.end(); ws.finishClose(); io.server.close(); done(); }); }); - + cl.handshake(function (sid) { ws = websocket(cl, sid); ws.on('message', function (packet) { @@ -227,7 +310,7 @@ module.exports = { } else { packet.type.should.eql('heartbeat'); heartbeats++; - + if (heartbeats == 1) { ws.packet({ type: 'heartbeat' }); } @@ -235,7 +318,7 @@ module.exports = { }); }); }, - + 'test sending deliverable volatile messages': function (done) { var cl = client(++ports) , io = create(cl) @@ -245,10 +328,10 @@ module.exports = { io.configure(function () { io.set('close timeout', .05); }); - + io.sockets.on('connection', function (socket) { socket.volatile.send('tobi'); - + socket.on('disconnect', function () { messaged.should.be.true; cl.end(); @@ -256,7 +339,7 @@ module.exports = { done(); }); }); - + cl.handshake(function (sid) { var ws = websocket(cl, sid); ws.on('message', function (msg) { @@ -274,19 +357,19 @@ module.exports = { }); }); }, - + 'test sending deliverable volatile json': function (done) { var cl = client(++ports) , io = create(cl) , messaged = false; - + io.configure(function () { io.set('close timeout', .05); }); - + io.sockets.on('connection', function (socket) { socket.volatile.json.send([1, 2, 3]); - + socket.on('disconnect', function () { messaged.should.be.true; cl.end(); @@ -294,7 +377,7 @@ module.exports = { done(); }); }); - + cl.handshake(function (sid) { var ws = websocket(cl, sid); ws.on('message', function (msg) { @@ -313,19 +396,19 @@ module.exports = { }); }); }, - + 'test sending deliverable volatile events': function (done) { var cl = client(++ports) , io = create(cl) , messaged = false; - + io.configure(function () { io.set('close timeout', .05); }); - + io.sockets.on('connection', function (socket) { socket.volatile.emit('tobi'); - + socket.on('disconnect', function () { messaged.should.be.true; cl.end(); @@ -333,7 +416,7 @@ module.exports = { done(); }); }); - + cl.handshake(function (sid) { var ws = websocket(cl, sid); ws.on('message', function (msg) { @@ -353,7 +436,7 @@ module.exports = { }); }); }, - + 'test sending to all clients in a namespace': function (done) { var port = ++ports , cl1 = client(port) @@ -362,21 +445,21 @@ module.exports = { , messages = 0 , connections = 0 , disconnections = 0; - + io.configure(function () { io.set('close timeout', 0); }); - + io.sockets.on('connection', function (socket) { connections++; - + if (connections == 2) { io.sockets.send('yup'); } - + socket.on('disconnect', function () { disconnections++; - + if (disconnections == 2) { messages.should.eql(2); cl1.end(); @@ -386,7 +469,7 @@ module.exports = { } }); }); - + cl1.handshake(function (sid) { var ws1 = websocket(cl1, sid); ws1.on('message', function (msg) { @@ -399,13 +482,13 @@ module.exports = { , data: 'yup' , endpoint: '' }); - + messages++; ws1.finishClose(); } }); }); - + cl2.handshake(function (sid) { var ws2 = websocket(cl2, sid); ws2.on('message', function (msg) { @@ -418,14 +501,14 @@ module.exports = { , data: 'yup' , endpoint: '' }); - + messages++; ws2.finishClose(); } }); }); }, - + 'test sending json to all clients in a namespace': function (done) { var port = ++ports , cl1 = client(port) @@ -434,21 +517,21 @@ module.exports = { , messages = 0 , connections = 0 , disconnections = 0; - + io.configure(function () { io.set('close timeout', 0); }); - + io.sockets.on('connection', function (socket) { connections++; - + if (connections == 2) { io.sockets.json.send({ a: 'b' }); } - + socket.on('disconnect', function () { disconnections++; - + if (disconnections == 2) { messages.should.eql(2); cl1.end(); @@ -458,7 +541,7 @@ module.exports = { } }); }); - + cl1.handshake(function (sid) { var ws1 = websocket(cl1, sid); ws1.on('message', function (msg) { @@ -471,13 +554,13 @@ module.exports = { , data: { a: 'b' } , endpoint: '' }); - + messages++; ws1.finishClose(); } }); }); - + cl2.handshake(function (sid) { var ws2 = websocket(cl2, sid); ws2.on('message', function (msg) { @@ -490,14 +573,14 @@ module.exports = { , data: { a: 'b' } , endpoint: '' }); - + messages++; ws2.finishClose(); } }); }); }, - + 'test emitting to all clients in a namespace': function (done) { var port = ++ports , cl1 = client(port) @@ -506,21 +589,21 @@ module.exports = { , messages = 0 , connections = 0 , disconnections = 0; - + io.configure(function () { io.set('close timeout', 0); }); - + io.sockets.on('connection', function (socket) { connections++; - + if (connections == 2) { io.sockets.emit('tobi', 'rapture'); } - + socket.on('disconnect', function () { disconnections++; - + if (disconnections == 2) { messages.should.eql(2); cl1.end(); @@ -530,7 +613,7 @@ module.exports = { } }); }); - + cl1.handshake(function (sid) { var ws1 = websocket(cl1, sid); ws1.on('message', function (msg) { @@ -544,13 +627,13 @@ module.exports = { , args: ['rapture'] , endpoint: '' }); - + messages++; ws1.finishClose(); } }); }); - + cl2.handshake(function (sid) { var ws2 = websocket(cl2, sid); ws2.on('message', function (msg) { @@ -564,14 +647,14 @@ module.exports = { , args: ['rapture'] , endpoint: '' }); - + messages++; ws2.finishClose(); } }); }); }, - + 'test sending to all clients in a room': function (done) { var port = ++ports , cl1 = client(port) @@ -582,18 +665,18 @@ module.exports = { , joins = 0 , connections = 0 , disconnections = 0; - + io.configure(function () { io.set('close timeout', 0); }); - + io.sockets.on('connection', function (socket) { connections++; - + if (connections != 3) { socket.join('woot'); joins++; - + if (joins == 2) { setTimeout(function () { connections.should.eql(3); @@ -601,10 +684,10 @@ module.exports = { }, 20); } } - + socket.on('disconnect', function () { disconnections++; - + if (disconnections == 3) { messages.should.eql(2); cl1.end(); @@ -615,7 +698,7 @@ module.exports = { } }); }); - + cl1.handshake(function (sid) { var ws1 = websocket(cl1, sid); ws1.on('message', function (msg) { @@ -628,16 +711,16 @@ module.exports = { , data: 'hahaha' , endpoint: '' }); - + messages++; } }); - + setTimeout(function () { ws1.finishClose(); }, 50); }); - + cl2.handshake(function (sid) { var ws2 = websocket(cl2, sid); ws2.on('message', function (msg) { @@ -650,16 +733,16 @@ module.exports = { , data: 'hahaha' , endpoint: '' }); - + messages++; } }); - + setTimeout(function () { ws2.finishClose(); }, 50); }); - + cl3.handshake(function (sid) { var ws3 = websocket(cl3, sid); ws3.on('message', function (msg) { @@ -672,17 +755,17 @@ module.exports = { , data: 'hahaha' , endpoint: '' }); - + messages++; } }); - + setTimeout(function () { ws3.finishClose(); }, 50); }); }, - + 'test sending json to all clients in a room': function (done) { var port = ++ports , cl1 = client(port) @@ -693,18 +776,18 @@ module.exports = { , joins = 0 , connections = 0 , disconnections = 0; - + io.configure(function () { io.set('close timeout', 0); }); - + io.sockets.on('connection', function (socket) { connections++; - + if (connections != 3) { socket.join('woot'); joins++; - + if (joins == 2) { setTimeout(function () { connections.should.eql(3); @@ -712,10 +795,10 @@ module.exports = { }, 20); } } - + socket.on('disconnect', function () { disconnections++; - + if (disconnections == 3) { messages.should.eql(2); cl1.end(); @@ -726,7 +809,7 @@ module.exports = { } }); }); - + cl1.handshake(function (sid) { var ws1 = websocket(cl1, sid); ws1.on('message', function (msg) { @@ -739,16 +822,16 @@ module.exports = { , data: 123 , endpoint: '' }); - + messages++; } }); - + setTimeout(function () { ws1.finishClose(); }, 50); }); - + cl2.handshake(function (sid) { var ws2 = websocket(cl2, sid); ws2.on('message', function (msg) { @@ -761,16 +844,16 @@ module.exports = { , data: 123 , endpoint: '' }); - + messages++; } }); - + setTimeout(function () { ws2.finishClose(); }, 50); }); - + cl3.handshake(function (sid) { var ws3 = websocket(cl3, sid); ws3.on('message', function (msg) { @@ -783,17 +866,17 @@ module.exports = { , data: 123 , endpoint: '' }); - + messages++; } }); - + setTimeout(function () { ws3.finishClose(); }, 50); }); }, - + 'test emitting to all clients in a room': function (done) { var port = ++ports , cl1 = client(port) @@ -804,18 +887,18 @@ module.exports = { , joins = 0 , connections = 0 , disconnections = 0; - + io.configure(function () { io.set('close timeout', 0); }); - + io.sockets.on('connection', function (socket) { connections++; - + if (connections != 3) { socket.join('woot'); joins++; - + if (joins == 2) { setTimeout(function () { connections.should.eql(3); @@ -823,10 +906,10 @@ module.exports = { }, 20); } } - + socket.on('disconnect', function () { disconnections++; - + if (disconnections == 3) { messages.should.eql(2); cl1.end(); @@ -837,7 +920,7 @@ module.exports = { } }); }); - + cl1.handshake(function (sid) { var ws1 = websocket(cl1, sid); ws1.on('message', function (msg) { @@ -851,16 +934,16 @@ module.exports = { , args: [] , endpoint: '' }); - + messages++; } }); - + setTimeout(function () { ws1.finishClose(); }, 50); }); - + cl2.handshake(function (sid) { var ws2 = websocket(cl2, sid); ws2.on('message', function (msg) { @@ -874,16 +957,16 @@ module.exports = { , args: [] , endpoint: '' }); - + messages++; } }); - + setTimeout(function () { ws2.finishClose(); }, 50); }); - + cl3.handshake(function (sid) { var ws3 = websocket(cl3, sid); ws3.on('message', function (msg) { @@ -897,17 +980,17 @@ module.exports = { , args: [] , endpoint: '' }); - + messages++; } }); - + setTimeout(function () { ws3.finishClose(); }, 50); }); }, - + 'test leaving a room': function (done) { var port = ++ports , cl1 = client(port) @@ -915,20 +998,20 @@ module.exports = { , io = create(cl1) , joins = 0 , disconnects = 0; - + io.set('close timeout', 0); - + io.sockets.on('connection', function (socket) { socket.join('foo'); io.sockets.clients('foo').should.have.length(++joins); - + socket.on('disconnect', function () { socket.leave('foo'); socket.leave('foo'); socket.leave('foo'); - + io.sockets.clients('foo').should.have.length(--joins); - + if (++disconnects == 2) { io.server.close(); cl1.end(); @@ -937,7 +1020,7 @@ module.exports = { } }) }); - + cl1.handshake(function (sid) { var ws1 = websocket(cl1, sid); ws1.on('message', function (msg) { @@ -948,7 +1031,7 @@ module.exports = { } }); }); - + cl2.handshake(function (sid) { var ws2 = websocket(cl2, sid); ws2.on('message', function (msg) { @@ -960,7 +1043,7 @@ module.exports = { }); }); }, - + 'test message with broadcast flag': function (done) { var port = ++ports , cl1 = client(port) @@ -969,19 +1052,19 @@ module.exports = { , io = create(cl1) , messages = 0 , disconnections = 0; - + io.configure(function () { io.set('close timeout', 0); }); - + io.sockets.on('connection', function (socket) { socket.on('trigger broadcast', function () { socket.broadcast.send('boom'); }); - + socket.on('disconnect', function () { disconnections++; - + if (disconnections == 3) { messages.should.eql(2); cl1.end(); @@ -992,7 +1075,7 @@ module.exports = { } }); }); - + cl1.handshake(function (sid) { var ws1 = websocket(cl1, sid); ws1.on('message', function (msg) { @@ -1005,13 +1088,13 @@ module.exports = { , data: 'boom' , endpoint: '' }); - + messages++; ws1.finishClose(); } }); }); - + cl2.handshake(function (sid) { var ws2 = websocket(cl2, sid); ws2.on('message', function (msg) { @@ -1024,13 +1107,13 @@ module.exports = { , data: 'boom' , endpoint: '' }); - + messages++; ws2.finishClose(); } }); }); - + cl3.handshake(function (sid) { var ws3 = websocket(cl3, sid); ws3.on('open', function () { @@ -1039,12 +1122,12 @@ module.exports = { , name: 'trigger broadcast' , endpoint: '' }); - + setTimeout(function () { ws3.finishClose(); }, 50); }); - + ws3.on('message', function (msg) { if (!ws3.connected) { msg.type.should.eql('connect'); @@ -1055,7 +1138,7 @@ module.exports = { }); }); }, - + 'test json with broadcast flag': function (done) { var port = ++ports , cl1 = client(port) @@ -1064,19 +1147,19 @@ module.exports = { , io = create(cl1) , messages = 0 , disconnections = 0; - + io.configure(function () { io.set('close timeout', 0); }); - + io.sockets.on('connection', function (socket) { socket.on('trigger broadcast', function () { socket.broadcast.json.send([1, 2, 3]); }); - + socket.on('disconnect', function () { disconnections++; - + if (disconnections == 3) { messages.should.eql(2); cl1.end(); @@ -1087,7 +1170,7 @@ module.exports = { } }); }); - + cl1.handshake(function (sid) { var ws1 = websocket(cl1, sid); ws1.on('message', function (msg) { @@ -1100,13 +1183,13 @@ module.exports = { , data: [1, 2, 3] , endpoint: '' }); - + messages++; ws1.finishClose(); } }); }); - + cl2.handshake(function (sid) { var ws2 = websocket(cl2, sid); ws2.on('message', function (msg) { @@ -1119,13 +1202,13 @@ module.exports = { , data: [1, 2, 3] , endpoint: '' }); - + messages++; ws2.finishClose(); } }); }); - + cl3.handshake(function (sid) { var ws3 = websocket(cl3, sid); ws3.on('open', function () { @@ -1134,12 +1217,12 @@ module.exports = { , name: 'trigger broadcast' , endpoint: '' }); - + setTimeout(function () { ws3.finishClose(); }, 50); }); - + ws3.on('message', function (msg) { if (!ws3.connected) { msg.type.should.eql('connect'); @@ -1150,7 +1233,7 @@ module.exports = { }); }); }, - + 'test event with broadcast flag': function (done) { var port = ++ports , cl1 = client(port) @@ -1159,19 +1242,19 @@ module.exports = { , io = create(cl1) , messages = 0 , disconnections = 0; - + io.configure(function () { io.set('close timeout', 0); }); - + io.sockets.on('connection', function (socket) { socket.on('trigger broadcast', function () { socket.broadcast.emit('hey', 'arnold'); }); - + socket.on('disconnect', function () { disconnections++; - + if (disconnections == 3) { messages.should.eql(2); cl1.end(); @@ -1182,7 +1265,7 @@ module.exports = { } }); }); - + cl1.handshake(function (sid) { var ws1 = websocket(cl1, sid); ws1.on('message', function (msg) { @@ -1196,13 +1279,13 @@ module.exports = { , args: ['arnold'] , endpoint: '' }); - + messages++; ws1.finishClose(); } }); }); - + cl2.handshake(function (sid) { var ws2 = websocket(cl2, sid); ws2.on('message', function (msg) { @@ -1216,13 +1299,13 @@ module.exports = { , args: ['arnold'] , endpoint: '' }); - + messages++; ws2.finishClose(); } }); }); - + cl3.handshake(function (sid) { var ws3 = websocket(cl3, sid); ws3.on('open', function () { @@ -1231,12 +1314,12 @@ module.exports = { , name: 'trigger broadcast' , endpoint: '' }); - + setTimeout(function () { ws3.finishClose(); }, 50); }); - + ws3.on('message', function (msg) { if (!ws3.connected) { msg.type.should.eql('connect'); @@ -1247,7 +1330,7 @@ module.exports = { }); }); }, - + 'test message with broadcast flag and to()': function (done) { var port = ++ports , cl1 = client(port) @@ -1257,25 +1340,25 @@ module.exports = { , messages = 0 , connections = 0 , disconnections = 0; - + io.configure(function () { io.set('close timeout', 0); }); - + io.sockets.on('connection', function (socket) { connections++; - + if (connections == 1) { socket.join('losers'); } - + socket.on('trigger broadcast', function () { socket.broadcast.to('losers').send('boom'); }); - + socket.on('disconnect', function () { disconnections++; - + if (disconnections == 3) { messages.should.eql(1); cl1.end(); @@ -1286,7 +1369,7 @@ module.exports = { } }); }); - + cl1.handshake(function (sid) { var ws1 = websocket(cl1, sid); ws1.on('message', function (msg) { @@ -1299,11 +1382,11 @@ module.exports = { , data: 'boom' , endpoint: '' }); - + messages++; } }); - + ws1.on('open', function () { cl2.handshake(function (sid) { var ws2 = websocket(cl2, sid); @@ -1315,7 +1398,7 @@ module.exports = { throw new Error('This socket shouldnt get a message'); } }); - + ws2.on('open', function () { cl3.handshake(function (sid) { var ws3 = websocket(cl3, sid); @@ -1325,14 +1408,14 @@ module.exports = { , name: 'trigger broadcast' , endpoint: '' }); - + setTimeout(function () { ws1.finishClose(); ws2.finishClose(); ws3.finishClose(); }, 50); }); - + ws3.on('message', function (msg) { if (!ws3.connected) { msg.type.should.eql('connect'); @@ -1347,7 +1430,7 @@ module.exports = { }); }); }, - + 'test json with broadcast flag and to()': function (done) { var port = ++ports , cl1 = client(port) @@ -1357,25 +1440,25 @@ module.exports = { , messages = 0 , connections = 0 , disconnections = 0; - + io.configure(function () { io.set('close timeout', 0); }); - + io.sockets.on('connection', function (socket) { connections++; - + if (connections == 1) { socket.join('losers'); } - + socket.on('trigger broadcast', function () { socket.broadcast.json.to('losers').send({ hello: 'world' }); }); - + socket.on('disconnect', function () { disconnections++; - + if (disconnections == 3) { messages.should.eql(1); cl1.end(); @@ -1386,7 +1469,7 @@ module.exports = { } }); }); - + cl1.handshake(function (sid) { var ws1 = websocket(cl1, sid); ws1.on('message', function (msg) { @@ -1399,11 +1482,11 @@ module.exports = { , data: { hello: 'world' } , endpoint: '' }); - + messages++; } }); - + ws1.on('open', function () { cl2.handshake(function (sid) { var ws2 = websocket(cl2, sid); @@ -1415,7 +1498,7 @@ module.exports = { throw new Error('This socket shouldnt get a message'); } }); - + ws2.on('open', function () { cl3.handshake(function (sid) { var ws3 = websocket(cl3, sid); @@ -1425,14 +1508,14 @@ module.exports = { , name: 'trigger broadcast' , endpoint: '' }); - + setTimeout(function () { ws1.finishClose(); ws2.finishClose(); ws3.finishClose(); }, 50); }); - + ws3.on('message', function (msg) { if (!ws3.connected) { msg.type.should.eql('connect'); @@ -1447,7 +1530,7 @@ module.exports = { }); }); }, - + 'test event with broadcast flag and to()': function (done) { var port = ++ports , cl1 = client(port) @@ -1457,25 +1540,25 @@ module.exports = { , messages = 0 , connections = 0 , disconnections = 0; - + io.configure(function () { io.set('close timeout', 0); }); - + io.sockets.on('connection', function (socket) { connections++; - + if (connections == 1) { socket.join('losers'); } - + socket.on('trigger broadcast', function () { socket.broadcast.to('losers').emit('victory'); }); - + socket.on('disconnect', function () { disconnections++; - + if (disconnections == 3) { messages.should.eql(1); cl1.end(); @@ -1486,7 +1569,7 @@ module.exports = { } }); }); - + cl1.handshake(function (sid) { var ws1 = websocket(cl1, sid); ws1.on('message', function (msg) { @@ -1500,11 +1583,11 @@ module.exports = { , args: [] , endpoint: '' }); - + messages++; } }); - + ws1.on('open', function () { cl2.handshake(function (sid) { var ws2 = websocket(cl2, sid); @@ -1516,7 +1599,7 @@ module.exports = { throw new Error('This socket shouldnt get a message'); }; }); - + ws2.on('open', function () { cl3.handshake(function (sid) { var ws3 = websocket(cl3, sid); @@ -1526,14 +1609,14 @@ module.exports = { , name: 'trigger broadcast' , endpoint: '' }); - + setTimeout(function () { ws1.finishClose(); ws2.finishClose(); ws3.finishClose(); }, 50); }); - + ws3.on('message', function (msg) { if (!ws3.connected) { msg.type.should.eql('connect'); @@ -1548,19 +1631,19 @@ module.exports = { }); }); }, - + 'test accessing handshake data from sockets': function (done) { var cl = client(++ports) , io = create(cl) , ws; - + io.sockets.on('connection', function (socket) { (!!socket.handshake.address.address).should.be.true; (!!socket.handshake.address.port).should.be.true; socket.handshake.headers.host.should.equal('localhost'); socket.handshake.headers.connection.should.equal('keep-alive'); socket.handshake.time.should.match(/GMT/); - + socket.on('disconnect', function () { setTimeout(function () { ws.finishClose(); @@ -1569,10 +1652,10 @@ module.exports = { done(); }, 10); }); - + socket.disconnect(); }); - + cl.handshake(function (sid) { ws = websocket(cl, sid); ws.on('message', function (msg) { @@ -1583,7 +1666,7 @@ module.exports = { }); }); }, - + 'test accessing the array of clients': function (done) { var port = ++ports , cl1 = client(port) @@ -1591,14 +1674,14 @@ module.exports = { , io = create(cl1) , total = 2 , ws1, ws2; - + io.sockets.on('connection', function (socket) { socket.on('join ferrets', function () { socket.join('ferrets'); socket.send('done'); }); }); - + function check() { io.sockets.clients('ferrets').should.have.length(1); io.sockets.clients('ferrets')[0].should.be.an.instanceof(sio.Socket); @@ -1608,7 +1691,7 @@ module.exports = { io.sockets.clients()[0].id.should.equal(ws1.sid); io.sockets.clients()[1].should.be.an.instanceof(sio.Socket); io.sockets.clients()[1].id.should.equal(ws2.sid); - + ws1.finishClose(); ws2.finishClose(); cl1.end(); @@ -1616,7 +1699,7 @@ module.exports = { io.server.close(); done(); }; - + cl1.handshake(function (sid) { ws1 = websocket(cl1, sid); ws1.sid = sid; @@ -1645,21 +1728,21 @@ module.exports = { }); }); }, - + 'test accessing handshake data from sockets on disconnect': function (done) { var cl = client(++ports) , io = create(cl) , ws; - + io.sockets.on('connection', function (socket) { socket.on('disconnect', function () { - + (!!socket.handshake.address.address).should.be.true; (!!socket.handshake.address.port).should.be.true; socket.handshake.headers.host.should.equal('localhost'); socket.handshake.headers.connection.should.equal('keep-alive'); socket.handshake.time.should.match(/GMT/); - + setTimeout(function () { ws.finishClose(); cl.end(); @@ -1667,10 +1750,10 @@ module.exports = { done(); }, 10); }); - + socket.disconnect(); }); - + cl.handshake(function (sid) { ws = websocket(cl, sid); ws.on('message', function (msg) { @@ -1681,42 +1764,42 @@ module.exports = { }); }); }, - + 'test for intentional and unintentional disconnects': function (done) { var cl = client(++ports) , io = create(cl) , calls = 0 , ws; - + function close () { cl.end(); io.server.close(); ws.finishClose(); done(); } - + io.configure(function () { io.set('heartbeat interval', .05); io.set('heartbeat timeout', .05); io.set('close timeout', 0); }); - + io.of('/foo').on('connection', function (socket) { socket.on('disconnect', function (reason) { reason.should.equal('packet'); - + if (++calls == 2) close(); }); }); - + io.of('/bar').on('connection', function (socket) { socket.on('disconnect', function (reason) { reason.should.equal('socket end'); - + if (++calls == 2) close(); }); }); - + cl.handshake(function (sid) { var messages = 0; ws = websocket(cl, sid); @@ -1730,7 +1813,7 @@ module.exports = { , endpoint: '/bar' }); }); - + ws.on('message', function (packet) { if (packet.type == 'connect') { if (++messages === 3) { @@ -1741,20 +1824,20 @@ module.exports = { }); }); }, - + 'test socket clean up': function (done) { var cl = client(++ports) , io = create(cl) , ws; - + io.sockets.on('connection', function (socket) { var self = this , id = socket.id; - + socket.on('disconnect', function () { setTimeout(function () { var available = !!self.sockets[id]; - + available.should.be.false; ws.finishClose(); cl.end(); @@ -1762,10 +1845,10 @@ module.exports = { done(); }, 10); }); - + socket.disconnect(); }); - + cl.handshake(function (sid) { ws = websocket(cl, sid); ws.on('message', function (msg) { diff --git a/test/transports.xhr-polling.test.js b/test/transports.xhr-polling.test.js index a8c4b02d81..5e9175bbdc 100644 --- a/test/transports.xhr-polling.test.js +++ b/test/transports.xhr-polling.test.js @@ -768,7 +768,7 @@ module.exports = { var cl = client(++ports) , io = create(cl) , messages = 0; - + io.configure(function () { io.set('polling duration', 0); io.set('close timeout', .1); @@ -2751,7 +2751,7 @@ module.exports = { } ); }); - + }); }); }