diff --git a/lib/socket.js b/lib/socket.js index e3c64b996..cf441c3fa 100644 --- a/lib/socket.js +++ b/lib/socket.js @@ -222,7 +222,10 @@ Socket.prototype.onclose = function (reason) { */ Socket.prototype.onpacket = function (packet) { - if (packet.nsp !== this.nsp) return; + var sameNamespace = packet.nsp === this.nsp; + var rootNamespaceError = packet.type === parser.ERROR && packet.nsp === '/'; + + if (!sameNamespace && !rootNamespaceError) return; switch (packet.type) { case parser.CONNECT: diff --git a/test/socket.js b/test/socket.js index b20bfc02e..91abd9d81 100644 --- a/test/socket.js +++ b/test/socket.js @@ -158,4 +158,22 @@ describe('socket', function () { }); }); }); + + it('should fire an error event on middleware failure from main namespace', function (done) { + var socket = io('/foo', { forceNew: true, query: { 'fail': true } }); + socket.on('error', function (err) { + expect(err).to.eql('Auth failed (main namespace)'); + socket.disconnect(); + done(); + }); + }); + + it('should fire an error event on middleware failure from custom namespace', function (done) { + var socket = io('/no', { forceNew: true }); + socket.on('error', function (err) { + expect(err).to.eql('Auth failed (custom namespace)'); + socket.disconnect(); + done(); + }); + }); }); diff --git a/test/support/server.js b/test/support/server.js index 120ce14cf..23dd49f32 100644 --- a/test/support/server.js +++ b/test/support/server.js @@ -25,6 +25,15 @@ server.of('/abc').on('connection', function (socket) { socket.emit('handshake', socket.handshake); }); +server.use(function (socket, next) { + if (socket.request._query.fail) return next(new Error('Auth failed (main namespace)')); + next(); +}); + +server.of('/no').use(function (socket, next) { + next(new Error('Auth failed (custom namespace)')); +}); + server.on('connection', function (socket) { // simple test socket.on('hi', function () {