Skip to content

Commit

Permalink
Fixed websocket tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
rauchg committed Oct 7, 2011
1 parent 11f1a7c commit 51782fc
Showing 1 changed file with 48 additions and 27 deletions.
75 changes: 48 additions & 27 deletions test/transports.websocket.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -19,51 +19,59 @@ var sio = require('socket.io')
*/

module.exports = {
'websocket identifies as websocket': function (done) {
var cl = client(++ports)
, io = create(cl)
, messages = 0
, ws;
io.set('transports', ['websocket']);
io.sockets.on('connection', function (socket) {
socket.manager.transports[socket.id].name.should.equal('websocket');
done();
});
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)
, messages = 0
, ws;

io.set('transports', ['websocket']);
io.sockets.on('connection', function (socket) {
socket.manager.transports[socket.id].protocolVersion.should.equal('hixie-76');
done();

socket.on('disconnect', function () {
setTimeout(function () {
ws.finishClose();
cl.end();
io.server.close();
done();
}, 10);
});

socket.disconnect();
});

cl.handshake(function (sid) {
ws = websocket(cl, sid);
});
},

'hybi-07-12 websocket draft parser is used for sec-websocket-version: 8': function (done) {
var cl = client(++ports)
, io = create(cl)
, messages = 0
, ws;

io.set('transports', ['websocket']);
io.sockets.on('connection', function (socket) {
socket.manager.transports[socket.id].protocolVersion.should.equal('07-12');
done();

socket.on('disconnect', function () {
setTimeout(function () {
cl.end();
io.server.close();
done();
}, 10);
});

socket.disconnect();
});

var headers = {
'sec-websocket-version': 8,
'upgrade': 'websocket',
'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
}

cl.get('/socket.io/{protocol}', {}, function (res, data) {
var sid = data.split(':')[0];
var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
Expand All @@ -74,18 +82,29 @@ module.exports = {
'hybi-16 websocket draft parser is used for sec-websocket-version: 13': function (done) {
var cl = client(++ports)
, io = create(cl)
, messages = 0
, ws;

io.set('transports', ['websocket']);

io.sockets.on('connection', function (socket) {
socket.manager.transports[socket.id].protocolVersion.should.equal('16');
done();

socket.on('disconnect', function () {
setTimeout(function () {
cl.end();
io.server.close();
done();
}, 10);
});

socket.disconnect();
});

var headers = {
'sec-websocket-version': 13,
'upgrade': 'websocket',
'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
}

cl.get('/socket.io/{protocol}', {}, function (res, data) {
var sid = data.split(':')[0];
var url = '/socket.io/' + sio.protocol + '/websocket/' + sid;
Expand All @@ -96,8 +115,7 @@ module.exports = {
'hybi-07-12 origin filter blocks access for mismatched sec-websocket-origin': function (done) {
var cl = client(++ports)
, io = create(cl)
, messages = 0
, ws;

io.set('transports', ['websocket']);
io.set('origins', 'foo.bar.com:*');

Expand All @@ -107,12 +125,14 @@ module.exports = {
'Sec-WebSocket-Origin': 'http://baz.bar.com',
'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
}

// handshake uses correct origin -- we want to block the actuall 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) {});
res.client.onend = function() {
io.server.close();
done();
}
});
Expand All @@ -121,8 +141,7 @@ module.exports = {
'hybi-16 origin filter blocks access for mismatched sec-websocket-origin': function (done) {
var cl = client(++ports)
, io = create(cl)
, messages = 0
, ws;

io.set('transports', ['websocket']);
io.set('origins', 'foo.bar.com:*');

Expand All @@ -132,12 +151,14 @@ module.exports = {
'origin': 'http://baz.bar.com',
'Sec-WebSocket-Key': 'dGhlIHNhbXBsZSBub25jZQ=='
}

// handshake uses correct origin -- we want to block the actuall 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) {});
res.client.onend = function() {
io.server.close();
done();
}
});
Expand Down

0 comments on commit 51782fc

Please sign in to comment.