@@ -223,8 +223,8 @@ location /t {
223223 path = " unix://tmp/redis.sock" ,
224224 }): connect()
225225
226- assert(not redis and err == " no such file or directory" ,
227- " bad domain socket should fail" )
226+ assert(not redis and err == " no such file or directory" ,
227+ " bad domain socket should fail" )
228228 }
229229}
230230-- - request
@@ -241,13 +241,13 @@ location /t {
241241 content_by_lua_block {
242242 local rc = require (" resty.redis.connector" )
243243
244- local params = {
245- url = " redis://foo@127.0.0.1:$ TEST_NGINX_REDIS_PORT /4"
246- }
244+ local user_params = {
245+ url = " redis://foo@127.0.0.1:$ TEST_NGINX_REDIS_PORT /4"
246+ }
247247
248- local ok , err = rc. parse_dsn(params )
249- assert(ok and not err,
250- " url should parse without error: " .. tostring(err))
248+ local params , err = rc. parse_dsn(user_params )
249+ assert(params and not err,
250+ " url should parse without error: " .. tostring(err))
251251
252252 assert(params. host == " 127.0.0.1" , " host should be localhost" )
253253 assert(tonumber(params. port) == $ TEST_NGINX_REDIS_PORT ,
@@ -256,12 +256,12 @@ location /t {
256256 assert(params. password == " foo" , " password should be foo" )
257257
258258
259- local params = {
259+ local user_params = {
260260 url = " sentinel://foo@ foomaster :s/2"
261261 }
262262
263- local ok , err = rc. parse_dsn(params )
264- assert(ok and not err,
263+ local params , err = rc. parse_dsn(user_params )
264+ assert(params and not err,
265265 " url should parse without error: " .. tostring(err))
266266
267267 assert(params. master_name == " foomaster" , " master_name should be foomaster" )
@@ -282,3 +282,81 @@ location /t {
282282GET / t
283283-- - no_error_log
284284[error]
285+
286+
287+ === TEST 9 : params override dsn components
288+ -- - http_config eval: $::HttpConfig
289+ -- - config
290+ location / t {
291+ lua_socket_log_errors Off;
292+ content_by_lua_block {
293+ local rc = require (" resty.redis.connector" )
294+
295+ local user_params = {
296+ url = " redis://foo@127.0.0.1:6381/4" ,
297+ db = 2 ,
298+ password = " bar" ,
299+ host = " example.com" ,
300+ }
301+
302+ local params, err = rc. parse_dsn(user_params)
303+ assert(params and not err,
304+ " url should parse without error: " .. tostring(err))
305+
306+ assert(tonumber(params. db) == 2 , " db should be 2" )
307+ assert(params. password == " bar" , " password should be bar" )
308+ assert(params. host == " example.com" , " host should be example.com" )
309+
310+ assert(tonumber(params. port) == 6381 , " ort should still be 6381" )
311+
312+ }
313+ }
314+ -- - request
315+ GET / t
316+ -- - no_error_log
317+ [error]
318+
319+
320+ === TEST 9 : Integration test for parse_dsn
321+ -- - http_config eval: $::HttpConfig
322+ -- - config
323+ location / t {
324+ lua_socket_log_errors Off;
325+ content_by_lua_block {
326+ local user_params = {
327+ url = " redis://foo.example:$ TEST_NGINX_REDIS_PORT /4" ,
328+ db = 2 ,
329+ host = " 127.0.0.1" ,
330+ }
331+
332+ local rc, err = require (" resty.redis.connector" ). new (user_params)
333+ assert(rc and not err, " new should return positively" )
334+
335+ local redis, err = rc: connect()
336+ assert(redis and not err, " connect should return positively" )
337+ assert(redis: set(" cat" , " dog" ) and redis: get(" cat" ) == " dog" )
338+
339+ local redis, err = rc: connect({
340+ url = " redis://foo.example:$ TEST_NGINX_REDIS_PORT /4" ,
341+ db = 2 ,
342+ host = " 127.0.0.1" ,
343+ })
344+ assert(redis and not err, " connect should return positively" )
345+ assert(redis: set(" cat" , " dog" ) and redis: get(" cat" ) == " dog" )
346+
347+
348+ local rc2, err = require (" resty.redis.connector" ). new ()
349+ local redis, err = rc2: connect({
350+ url = " redis://foo.example:$ TEST_NGINX_REDIS_PORT /4" ,
351+ db = 2 ,
352+ host = " 127.0.0.1" ,
353+ })
354+ assert(redis and not err, " connect should return positively" )
355+ assert(redis: set(" cat" , " dog" ) and redis: get(" cat" ) == " dog" )
356+
357+ }
358+ }
359+ -- - request
360+ GET / t
361+ -- - no_error_log
362+ [error]
0 commit comments