You've already forked node-redis
mirror of
https://github.com/redis/node-redis.git
synced 2025-08-09 00:22:08 +03:00
Merge pull request #841 from fintura/command
Add .command_used to errors returned by the parser. Fix authentication failure being emitted instead of returned by a callback if present. Fixes #427
This commit is contained in:
13
index.js
13
index.js
@@ -206,20 +206,20 @@ RedisClient.prototype.do_auth = function () {
|
||||
debug("Warning: Redis server does not require a password, but a password was supplied.");
|
||||
err = null;
|
||||
res = "OK";
|
||||
} else if (self.auth_callback) {
|
||||
self.auth_callback(err);
|
||||
self.auth_callback = null;
|
||||
} else {
|
||||
return self.emit("error", new Error("Auth error: " + err.message));
|
||||
self.emit("error", err);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
res = res.toString();
|
||||
if (res !== "OK") {
|
||||
return self.emit("error", new Error("Auth failed: " + res));
|
||||
}
|
||||
|
||||
debug("Auth succeeded " + self.address + " id " + self.connection_id);
|
||||
|
||||
if (self.auth_callback) {
|
||||
self.auth_callback(err, res);
|
||||
self.auth_callback(null, res);
|
||||
self.auth_callback = null;
|
||||
}
|
||||
|
||||
@@ -509,6 +509,7 @@ RedisClient.prototype.on_data = function (data) {
|
||||
|
||||
RedisClient.prototype.return_error = function (err) {
|
||||
var command_obj = this.command_queue.shift(), queue_len = this.command_queue.length;
|
||||
err.command_used = command_obj.command.toUpperCase();
|
||||
|
||||
if (this.pub_sub_mode === false && queue_len === 0) {
|
||||
this.command_queue = new Queue();
|
||||
|
@@ -34,19 +34,32 @@ describe("client authentication", function () {
|
||||
});
|
||||
});
|
||||
|
||||
it("raises error when auth is bad", function (done) {
|
||||
it("emits error when auth is bad without callback", function (done) {
|
||||
if (helper.redisProcess().spawnFailed()) this.skip();
|
||||
|
||||
client = redis.createClient.apply(redis.createClient, args);
|
||||
|
||||
client.once('error', function (error) {
|
||||
assert.ok(/ERR invalid password/.test(error));
|
||||
client.once('error', function (err) {
|
||||
assert.strictEqual(err.command_used, 'AUTH');
|
||||
assert.ok(/ERR invalid password/.test(err.message));
|
||||
return done();
|
||||
});
|
||||
|
||||
client.auth(auth + 'bad');
|
||||
});
|
||||
|
||||
it("returns an error when auth is bad with a callback", function (done) {
|
||||
if (helper.redisProcess().spawnFailed()) this.skip();
|
||||
|
||||
client = redis.createClient.apply(redis.createClient, args);
|
||||
|
||||
client.auth(auth + 'bad', function (err, res) {
|
||||
assert.strictEqual(err.command_used, 'AUTH');
|
||||
assert.ok(/ERR invalid password/.test(err.message));
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
if (ip === 'IPv4') {
|
||||
it('allows auth to be provided as part of redis url', function (done) {
|
||||
if (helper.redisProcess().spawnFailed()) this.skip();
|
||||
@@ -70,6 +83,19 @@ describe("client authentication", function () {
|
||||
});
|
||||
});
|
||||
|
||||
it('allows auth and no_ready_check to be provided as config option for client', function (done) {
|
||||
if (helper.redisProcess().spawnFailed()) this.skip();
|
||||
|
||||
var args = config.configureClient(parser, ip, {
|
||||
auth_pass: auth,
|
||||
no_ready_check: true
|
||||
});
|
||||
client = redis.createClient.apply(redis.createClient, args);
|
||||
client.on("ready", function () {
|
||||
done();
|
||||
});
|
||||
});
|
||||
|
||||
it('allows auth to be provided post-hoc with auth method', function (done) {
|
||||
if (helper.redisProcess().spawnFailed()) this.skip();
|
||||
|
||||
|
@@ -94,6 +94,7 @@ describe("The 'select' method", function () {
|
||||
assert.strictEqual(client.selected_db, null, "default db should be null");
|
||||
|
||||
client.on('error', function (err) {
|
||||
assert.strictEqual(err.command_used, 'SELECT');
|
||||
assert.equal(err.message, 'ERR invalid DB index');
|
||||
done();
|
||||
});
|
||||
|
Reference in New Issue
Block a user