Skip to content

Commit 711d51c

Browse files
author
Ruben Bridgewater
committed
Add unify_options / createClient tests
1 parent 6013ee7 commit 711d51c

File tree

2 files changed

+241
-24
lines changed

2 files changed

+241
-24
lines changed

test/connection.spec.js

Lines changed: 0 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -327,30 +327,6 @@ describe("connection tests", function () {
327327
assert(create_stream_string === String(redis.RedisClient.prototype.create_stream));
328328
});
329329

330-
it("throws on strange connection info", function () {
331-
client = {
332-
end: function() {}
333-
};
334-
try {
335-
redis.createClient(true);
336-
throw new Error('failed');
337-
} catch (err) {
338-
assert.equal(err.message, 'Unknown type of connection in createClient()');
339-
}
340-
});
341-
342-
it("throws on protocol other than redis in the redis url", function () {
343-
client = {
344-
end: function() {}
345-
};
346-
try {
347-
redis.createClient(config.HOST[ip] + ':' + config.PORT);
348-
throw new Error('failed');
349-
} catch (err) {
350-
assert.equal(err.message, 'Connection string must use the "redis:" protocol or begin with slashes //');
351-
}
352-
});
353-
354330
if (ip === 'IPv4') {
355331
it('allows connecting with the redis url to the default host and port, select db 3 and warn about duplicate db option', function (done) {
356332
client = redis.createClient('redis:///3?db=3');

test/unify_options.spec.js

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,241 @@
1+
'use strict';
2+
3+
var assert = require('assert');
4+
var unifyOptions = require('../lib/createClient');
5+
var intercept = require('intercept-stdout');
6+
7+
describe('createClient options', function () {
8+
9+
describe('port as first parameter', function () {
10+
it('pass the options in the second parameter after a port', function () {
11+
var options = unifyOptions(1234, {
12+
option1: true,
13+
option2: function () {}
14+
});
15+
assert.strictEqual(Object.keys(options).length, 4);
16+
assert(options.option1);
17+
assert.strictEqual(options.port, 1234);
18+
assert.strictEqual(options.host, undefined);
19+
assert.strictEqual(typeof options.option2, 'function');
20+
});
21+
22+
it('pass the options in the third parameter after a port and host being set to null', function () {
23+
var options = unifyOptions(1234, null, {
24+
option1: true,
25+
option2: function () {}
26+
});
27+
assert.strictEqual(Object.keys(options).length, 4);
28+
assert(options.option1);
29+
assert.strictEqual(options.port, 1234);
30+
assert.strictEqual(options.host, undefined);
31+
assert.strictEqual(typeof options.option2, 'function');
32+
});
33+
34+
it('pass the options in the third parameter after a port and host being set to undefined', function () {
35+
var options = unifyOptions(1234, undefined, {
36+
option1: true,
37+
option2: function () {}
38+
});
39+
assert.strictEqual(Object.keys(options).length, 4);
40+
assert(options.option1);
41+
assert.strictEqual(options.port, 1234);
42+
assert.strictEqual(options.host, undefined);
43+
assert.strictEqual(typeof options.option2, 'function');
44+
});
45+
46+
it('pass the options in the third parameter after a port and host', function () {
47+
var options = unifyOptions('1234', 'localhost', {
48+
option1: true,
49+
option2: function () {}
50+
});
51+
assert.strictEqual(Object.keys(options).length, 4);
52+
assert(options.option1);
53+
assert.strictEqual(options.port, '1234');
54+
assert.strictEqual(options.host, 'localhost');
55+
assert.strictEqual(typeof options.option2, 'function');
56+
});
57+
58+
it('should throw with three parameters all set to a truthy value', function () {
59+
try {
60+
unifyOptions(1234, {}, {});
61+
throw new Error('failed');
62+
} catch (err) {
63+
assert.strictEqual(err.message, 'Unknown type of connection in createClient()');
64+
}
65+
});
66+
});
67+
68+
describe('unix socket as first parameter', function () {
69+
it('pass the options in the second parameter after a port', function () {
70+
var options = unifyOptions('/tmp/redis.sock', {
71+
option1: true,
72+
option2: function () {},
73+
option3: [1, 2, 3]
74+
});
75+
assert.strictEqual(Object.keys(options).length, 4);
76+
assert(options.option1);
77+
assert.strictEqual(options.path, '/tmp/redis.sock');
78+
assert.strictEqual(typeof options.option2, 'function');
79+
assert.strictEqual(options.option3.length, 3);
80+
});
81+
82+
it('pass the options in the third parameter after a port and host being set to null', function () {
83+
var options = unifyOptions('/tmp/redis.sock', null, {
84+
option1: true,
85+
option2: function () {}
86+
});
87+
assert.strictEqual(Object.keys(options).length, 3);
88+
assert(options.option1);
89+
assert.strictEqual(options.path, '/tmp/redis.sock');
90+
assert.strictEqual(typeof options.option2, 'function');
91+
});
92+
});
93+
94+
describe('redis url as first parameter', function () {
95+
it('empty redis url including options as second parameter', function () {
96+
var options = unifyOptions('redis://', {
97+
option: [1, 2, 3]
98+
});
99+
assert.strictEqual(Object.keys(options).length, 1);
100+
assert.strictEqual(options.option.length, 3);
101+
});
102+
103+
it('begin with two slashes including options as third parameter', function () {
104+
var options = unifyOptions('//:abc@/3?port=123', {
105+
option: [1, 2, 3]
106+
});
107+
assert.strictEqual(Object.keys(options).length, 4);
108+
assert.strictEqual(options.option.length, 3);
109+
assert.strictEqual(options.port, '123');
110+
assert.strictEqual(options.db, '3');
111+
assert.strictEqual(options.password, 'abc');
112+
});
113+
114+
it('duplicated, identical query options including options obj', function () {
115+
var text = '';
116+
var unhookIntercept = intercept(function(data) {
117+
text += data;
118+
return '';
119+
});
120+
var options = unifyOptions('//:abc@localhost:123/3?db=3&port=123&password=abc', null, {
121+
option: [1, 2, 3]
122+
});
123+
unhookIntercept();
124+
assert.strictEqual(text,
125+
'node_redis: WARNING: You passed the db option twice!\n' +
126+
'node_redis: WARNING: You passed the port option twice!\n' +
127+
'node_redis: WARNING: You passed the password option twice!\n'
128+
);
129+
assert.strictEqual(Object.keys(options).length, 5);
130+
assert.strictEqual(options.option.length, 3);
131+
assert.strictEqual(options.host, 'localhost');
132+
assert.strictEqual(options.port, '123');
133+
assert.strictEqual(options.db, '3');
134+
assert.strictEqual(options.password, 'abc');
135+
});
136+
137+
it('should throw on duplicated, non-identical query options', function () {
138+
try {
139+
unifyOptions('//:abc@localhost:1234/3?port=123&password=abc');
140+
throw new Error('failed');
141+
} catch (err) {
142+
assert.equal(err.message, 'The port option is added twice and does not match');
143+
}
144+
});
145+
146+
it('should throw without protocol slashes', function () {
147+
try {
148+
unifyOptions('redis:abc@localhost:123/3?db=3&port=123&password=abc');
149+
throw new Error('failed');
150+
} catch (err) {
151+
assert.equal(err.message, 'The redis url must begin with slashes "//" or contain slashes after the redis protocol');
152+
}
153+
});
154+
155+
it("warns on protocol other than redis in the redis url", function () {
156+
var text = '';
157+
var unhookIntercept = intercept(function (data) {
158+
text += data;
159+
return '';
160+
});
161+
var options = unifyOptions('http://abc');
162+
unhookIntercept();
163+
assert.strictEqual(Object.keys(options).length, 1);
164+
assert.strictEqual(options.host, 'abc');
165+
assert.strictEqual(text, 'node_redis: WARNING: You passed "http" as protocol instead of the "redis" protocol!\n');
166+
});
167+
});
168+
169+
describe('no parameters or set to null / undefined', function () {
170+
it('no parameters', function () {
171+
var options = unifyOptions();
172+
assert.strictEqual(Object.keys(options).length, 1);
173+
assert.strictEqual(options.host, undefined);
174+
});
175+
176+
it('set to null', function () {
177+
var options = unifyOptions(null, null);
178+
assert.strictEqual(Object.keys(options).length, 1);
179+
assert.strictEqual(options.host, null);
180+
});
181+
182+
it('set to undefined', function () {
183+
var options = unifyOptions(undefined, undefined);
184+
assert.strictEqual(Object.keys(options).length, 1);
185+
assert.strictEqual(options.host, undefined);
186+
});
187+
});
188+
189+
describe('only an options object is passed', function () {
190+
it('with options', function () {
191+
var options = unifyOptions({
192+
option: true
193+
});
194+
assert.strictEqual(Object.keys(options).length, 2);
195+
assert.strictEqual(options.host, undefined);
196+
assert.strictEqual(options.option, true);
197+
});
198+
199+
it('without options', function () {
200+
var options = unifyOptions({});
201+
assert.strictEqual(Object.keys(options).length, 1);
202+
assert.strictEqual(options.host, undefined);
203+
});
204+
205+
it('should throw with more parameters', function () {
206+
try {
207+
unifyOptions({
208+
option: true
209+
}, undefined);
210+
throw new Error('failed');
211+
} catch (err) {
212+
assert.strictEqual(err.message, 'To many arguments passed to createClient. Please only pass the options object');
213+
}
214+
});
215+
216+
it('including url as option', function () {
217+
var options = unifyOptions({
218+
option: [1, 2, 3],
219+
url: '//hm:abc@localhost:123/3'
220+
});
221+
assert.strictEqual(Object.keys(options).length, 6);
222+
assert.strictEqual(options.option.length, 3);
223+
assert.strictEqual(options.host, 'localhost');
224+
assert.strictEqual(options.port, '123');
225+
assert.strictEqual(options.db, '3');
226+
assert.strictEqual(options.url, '//hm:abc@localhost:123/3');
227+
assert.strictEqual(options.password, 'abc');
228+
});
229+
});
230+
231+
describe('faulty data', function () {
232+
it("throws on strange connection info", function () {
233+
try {
234+
unifyOptions(true);
235+
throw new Error('failed');
236+
} catch (err) {
237+
assert.equal(err.message, 'Unknown type of connection in createClient()');
238+
}
239+
});
240+
});
241+
});

0 commit comments

Comments
 (0)