2
1
mirror of https://github.com/Thream/socketio-jwt.git synced 2024-11-09 22:20:08 +01:00

improve tests

This commit is contained in:
José F. Romaniello 2015-05-29 09:39:49 -03:00
parent de40574362
commit 54dfbdf987
3 changed files with 17 additions and 9 deletions

View File

@ -18,6 +18,7 @@ describe('authorizer', function () {
socket.on('error', function(err){ socket.on('error', function(err){
err.message.should.eql("jwt malformed"); err.message.should.eql("jwt malformed");
err.code.should.eql("invalid_token"); err.code.should.eql("invalid_token");
socket.close();
done(); done();
}); });
}); });
@ -43,6 +44,7 @@ describe('authorizer', function () {
'query': 'token=' + this.token 'query': 'token=' + this.token
}); });
socket.on('connect', function(){ socket.on('connect', function(){
socket.close();
done(); done();
}).on('error', done); }).on('error', done);
}); });
@ -59,8 +61,10 @@ describe('authorizer', function () {
'query': 'token=' + this.token 'query': 'token=' + this.token
}); });
socket.on('connect', function () { socket.on('connect', function () {
socket.close();
done(new Error('this shouldnt happen')); done(new Error('this shouldnt happen'));
}).on('error', function (err) { }).on('error', function (err) {
socket.close();
err.message.should.eql("jwt signature is required"); err.message.should.eql("jwt signature is required");
done(); done();
}); });

View File

@ -55,9 +55,10 @@ describe('authorizer without querystring', function () {
}); });
var token = this.token; var token = this.token;
socket.on('connect', function(){ socket.on('connect', function(){
console.log('connected'); socket.on('echo-response', function () {
socket.on('echo-response', done) socket.close();
.on('authenticated', function () { done();
}).on('authenticated', function () {
socket.emit('echo'); socket.emit('echo');
}).emit('authenticate', { token: token }); }).emit('authenticate', { token: token });
}); });

View File

@ -8,7 +8,7 @@ var jwt = require('jsonwebtoken');
var xtend = require('xtend'); var xtend = require('xtend');
var server; var server, sio;
exports.start = function (options, callback) { exports.start = function (options, callback) {
@ -46,13 +46,12 @@ exports.start = function (options, callback) {
server = http.createServer(app); server = http.createServer(app);
var sio = socketIo.listen(server); sio = socketIo.listen(server);
if (options.handshake) { if (options.handshake) {
// this.set('authorization', socketio_jwt.authorize(options)); // this.set('authorization', socketio_jwt.authorize(options));
sio.use(socketio_jwt.authorize(options)); sio.use(socketio_jwt.authorize(options));
} }
sio.set('log level', 0);
if (options.handshake) { if (options.handshake) {
sio.sockets.on('echo', function (m) { sio.sockets.on('echo', function (m) {
@ -68,10 +67,14 @@ exports.start = function (options, callback) {
}); });
} }
server.__sockets = [];
server.on('connection', function (c) {
server.__sockets.push(c);
});
server.listen(9000, callback); server.listen(9000, callback);
}; };
exports.stop = function (callback) { exports.stop = function (callback) {
server.close(); sio.close();
callback(); callback();
}; };