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