|
| 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