Skip to content

Commit a7d86ef

Browse files
committed
adjust createClient() to support a larger subset of constructor patterns supported by node_redis
1 parent cf21d68 commit a7d86ef

File tree

2 files changed

+32
-2
lines changed

2 files changed

+32
-2
lines changed

main.js

+13-2
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,23 @@ var index = require("redis")
2323
exports.RedisClient = index.RedisClient;
2424
exports.Multi = index.Multi;
2525
exports.print = index.print;
26-
26+
exports.backends = backends;
2727

2828
// Overriden client factory.
2929

3030
exports.createClient = function(port, host, options) {
31-
var id = !port && !host ? 'fake_' + (++anon) : (host || "") + (port || "")
31+
if (arguments.length == 1 && typeof port == "object") {
32+
options = port;
33+
if (options.port || options.host) {
34+
port = options.port;
35+
host = options.host;
36+
}
37+
if (options.url || options.path) {
38+
host = options.url || options.path;
39+
port = "";
40+
}
41+
}
42+
var id = !port && !host ? 'fake_' + (++anon) : (host || "") + ((port) ? ":" + port : null || "")
3243
, lat = options && options.fast || exports.fast ? 1 : null
3344
, c = new Connection(backends[id] || (backends[id] = new Backend), lat, lat)
3445
, real_create_stream = RedisClient.prototype.create_stream

redis.test.js

+19
Original file line numberDiff line numberDiff line change
@@ -2261,6 +2261,25 @@ tests.unref = function () {
22612261
}, 500);
22622262
};*/
22632263

2264+
tests.CONSTRUCTOR_ARGUMENTS = function () {
2265+
var name = "constructor arguments";
2266+
var client1 = redis.createClient({ host: "testdomain.com", port: 1790 });
2267+
var client2 = redis.createClient({ url: "redis://testdomain.com:1791" });
2268+
var client3 = redis.createClient({ path: "testdomain.com:1792" });
2269+
var client4 = redis.createClient({ path: "testdomain.com:1793", return_buffers: true });
2270+
assert.equal("object", typeof redis.backends["testdomain.com:1790"], name + " host & port");
2271+
assert.equal("object", typeof redis.backends["redis://testdomain.com:1791"], name + " url");
2272+
assert.equal("object", typeof redis.backends["testdomain.com:1792"], name + " path");
2273+
client4.set("string key 1", "string value");
2274+
client4.get("string key 1", require_string("string value", name));
2275+
client4.get(new Buffer("string key 1"), function (err, reply) {
2276+
assert.strictEqual(null, err, name);
2277+
assert.strictEqual(true, Buffer.isBuffer(reply), name);
2278+
assert.strictEqual("<Buffer 73 74 72 69 6e 67 20 76 61 6c 75 65>", reply.inspect(), name);
2279+
});
2280+
next(name);
2281+
};
2282+
22642283
all_tests = Object.keys(tests);
22652284
all_start = new Date();
22662285
test_count = 0;

0 commit comments

Comments
 (0)