Skip to content

Commit cef1583

Browse files
committed
test: middleware tests
1 parent 5f843fe commit cef1583

File tree

1 file changed

+109
-0
lines changed

1 file changed

+109
-0
lines changed

test/socket.io.js

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -537,4 +537,113 @@ describe('socket.io', function(){
537537
});
538538
});
539539
});
540+
541+
describe('middleware', function(done){
542+
var Socket = require('../lib/socket');
543+
544+
it('should call functions', function(done){
545+
var srv = http();
546+
var sio = io(srv);
547+
var run = 0;
548+
sio.use(function(socket, next){
549+
expect(socket).to.be.a(Socket);
550+
run++;
551+
next();
552+
});
553+
sio.use(function(socket, next){
554+
expect(socket).to.be.a(Socket);
555+
run++;
556+
next();
557+
});
558+
srv.listen(function(){
559+
var socket = client(srv);
560+
socket.on('connect', function(){
561+
expect(run).to.be(2);
562+
done();
563+
});
564+
});
565+
});
566+
567+
it('should pass errors', function(done){
568+
var srv = http();
569+
var sio = io(srv);
570+
var run = 0;
571+
sio.use(function(socket, next){
572+
next(new Error('Authentication error'));
573+
});
574+
sio.use(function(socket, next){
575+
done(new Error('nope'));
576+
});
577+
srv.listen(function(){
578+
var socket = client(srv);
579+
socket.on('connect', function(){
580+
done(new Error('nope'));
581+
});
582+
socket.on('error', function(err){
583+
expect(err).to.be('Authentication error');
584+
done();
585+
});
586+
});
587+
});
588+
589+
it('should pass `data` of error object', function(done){
590+
var srv = http();
591+
var sio = io(srv);
592+
var run = 0;
593+
sio.use(function(socket, next){
594+
var err = new Error('Authentication error');
595+
err.data = { a: 'b', c: 3 };
596+
next(err);
597+
});
598+
srv.listen(function(){
599+
var socket = client(srv);
600+
socket.on('connect', function(){
601+
done(new Error('nope'));
602+
});
603+
socket.on('error', function(err){
604+
expect(err).to.eql({ a: 'b', c: 3 });
605+
done();
606+
});
607+
});
608+
});
609+
610+
it('should only call connection after fns', function(done){
611+
var srv = http();
612+
var sio = io(srv);
613+
sio.use(function(socket, next){
614+
socket.name = 'guillermo';
615+
next();
616+
});
617+
srv.listen(function(){
618+
var socket = client(srv);
619+
sio.on('connection', function(socket){
620+
expect(socket.name).to.be('guillermo');
621+
done();
622+
});
623+
});
624+
});
625+
626+
it('should be ignored if socket gets closed', function(done){
627+
var srv = http();
628+
var sio = io(srv);
629+
var socket;
630+
sio.use(function(s, next){
631+
socket.io.engine.on('open', function(){
632+
socket.io.engine.close();
633+
s.client.conn.on('close', function(){
634+
process.nextTick(next);
635+
setTimeout(function(){
636+
done();
637+
}, 50);
638+
});
639+
});
640+
});
641+
srv.listen(function(){
642+
socket = client(srv);
643+
sio.on('connection', function(socket){
644+
done(new Error('should not fire'));
645+
});
646+
});
647+
});
648+
});
540649
});

0 commit comments

Comments
 (0)