Skip to content

Commit

Permalink
fix(config): server_url should return a valid URI for IPv6 hosts (#194)
Browse files Browse the repository at this point in the history
* fix(config): server_url should return a valid URI for IPv6 hosts

* Add more assertions to Config#server_url
  • Loading branch information
chen-anders authored May 2, 2024
1 parent d895cf3 commit 382dda7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/redis_client/config.rb
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,13 @@ def server_url
url = "#{url}?db=#{db}"
end
else
url = "redis#{'s' if ssl?}://#{host}:#{port}"
# add brackets to IPv6 address
redis_host = if host.count(":") >= 2
"[#{host}]"
else
host
end
url = "redis#{'s' if ssl?}://#{redis_host}:#{port}"
if db != 0
url = "#{url}/#{db}"
end
Expand Down
7 changes: 7 additions & 0 deletions test/redis_client/config_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,15 @@ def test_server_url
assert_equal "redis://localhost:6379", Config.new.server_url
assert_equal "redis://localhost:6379", Config.new(username: "george", password: "hunter2").server_url
assert_equal "redis://localhost:6379/5", Config.new(db: 5).server_url
assert_equal "redis://192.168.0.1:6379", Config.new(host: "192.168.0.1", port: 6379).server_url
assert_equal "redis://192.168.0.1:6379/5", Config.new(host: "192.168.0.1", port: 6379, db: 5).server_url
assert_equal "redis://example.com:8080", Config.new(host: "example.com", port: 8080).server_url
assert_equal "rediss://localhost:6379", Config.new(ssl: true).server_url
assert_equal "redis://[::1]:6379", Config.new(host: "::1", port: 6379).server_url
assert_equal "redis://[::1]:6379/2", Config.new(host: "::1", port: 6379, db: 2).server_url
assert_equal "redis://[::1]:6379/2", Config.new(url: "redis://[::1]:6379/2").server_url
assert_equal "redis://[ffff:aaaa:1111::fcf]:6379", Config.new(host: "ffff:aaaa:1111::fcf", port: 6379).server_url
assert_equal "redis://[ffff:aaaa:1111::fcf]:6379/2", Config.new(host: "ffff:aaaa:1111::fcf", port: 6379, db: 2).server_url

assert_equal "unix:///var/redis/redis.sock?db=5", Config.new(path: "/var/redis/redis.sock", db: 5).server_url
end
Expand Down

0 comments on commit 382dda7

Please sign in to comment.