From 7bcd67e60f06e63a61f8529a7402b3b7c463953e Mon Sep 17 00:00:00 2001 From: Zihua Li Date: Tue, 29 Sep 2015 18:35:21 +0800 Subject: [PATCH 1/2] Emit auth error for wrong password --- lib/redis/event_handler.js | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/lib/redis/event_handler.js b/lib/redis/event_handler.js index 3482192c..650f5a9c 100644 --- a/lib/redis/event_handler.js +++ b/lib/redis/event_handler.js @@ -12,9 +12,9 @@ exports.connectHandler = function (self) { // AUTH command should be processed before any other commands if (self.condition.auth) { - self.auth(self.condition.auth, function (err, res) { - if (err && err.message.match('no password is set')) { - console.warn('`auth` is specified in the client but not in the server.'); + self.auth(self.condition.auth, function (err) { + if (err) { + self.emit('auth error', err); } }); } From a8399fc513ace75398b57799f9a89da95260ec45 Mon Sep 17 00:00:00 2001 From: Zihua Li Date: Fri, 2 Oct 2015 19:36:21 +0800 Subject: [PATCH 2/2] Update tests and doc --- README.md | 1 + lib/redis/event_handler.js | 2 +- test/functional/auth.js | 23 +++++++++++++++++++---- 3 files changed, 21 insertions(+), 5 deletions(-) diff --git a/README.md b/README.md index 30b8361c..c13c2407 100644 --- a/README.md +++ b/README.md @@ -521,6 +521,7 @@ error | client will emit `error` when an error occurs while connecting.
Ho close | client will emit `close` when an established Redis server connection has closed. reconnecting | client will emit `reconnecting` after `close` when a reconnection will be made. The argument of the event is the time (in ms) before reconnecting. end | client will emit `end` after `close` when no more reconnections will be made. +authError | client will emit `authError` when the password specified in the options is wrong or the server doesn't require a password. You can also check out the `Redis#status` property to get the current connection status. diff --git a/lib/redis/event_handler.js b/lib/redis/event_handler.js index 650f5a9c..d7896b7d 100644 --- a/lib/redis/event_handler.js +++ b/lib/redis/event_handler.js @@ -14,7 +14,7 @@ exports.connectHandler = function (self) { if (self.condition.auth) { self.auth(self.condition.auth, function (err) { if (err) { - self.emit('auth error', err); + self.emit('authError', err); } }); } diff --git a/test/functional/auth.js b/test/functional/auth.js index 16620139..a76b246c 100644 --- a/test/functional/auth.js +++ b/test/functional/auth.js @@ -41,18 +41,33 @@ describe('auth', function () { }); }); - it('should warn when the server doesn\'t need auth', function (done) { - stub(console, 'warn', function () { - console.warn.restore(); + it('should emit "authError" when the server doesn\'t need auth', function (done) { + var server = new MockServer(17379, function (argv) { + if (argv[0] === 'auth' && argv[1] === 'pass') { + return new Error('ERR Client sent AUTH, but no password is set'); + } + }); + var redis = new Redis({ port: 17379, password: 'pass' }); + redis.on('authError', function (error) { + expect(error).to.have.property('message', 'ERR Client sent AUTH, but no password is set'); redis.disconnect(); server.disconnect(); done(); }); + }); + + it('should emit "authError" when the password is wrong', function (done) { var server = new MockServer(17379, function (argv) { if (argv[0] === 'auth' && argv[1] === 'pass') { - return new Error('ERR Client sent AUTH, but no password is set'); + return new Error('ERR invalid password'); } }); var redis = new Redis({ port: 17379, password: 'pass' }); + redis.on('authError', function (error) { + expect(error).to.have.property('message', 'ERR invalid password'); + redis.disconnect(); + server.disconnect(); + done(); + }); }); });