Skip to content

Commit 5b0e6f7

Browse files
committed
Improve Config#server_url
- Don't include the db if it's `0` - Show unix sockets with `unix://` scheme
1 parent d37fb19 commit 5b0e6f7

File tree

4 files changed

+17
-10
lines changed

4 files changed

+17
-10
lines changed

lib/redis_client/config.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -124,10 +124,17 @@ def ssl_context
124124

125125
def server_url
126126
if path
127-
"#{path}/#{db}"
127+
url = "unix://#{path}"
128+
if db != 0
129+
url = "#{url}?db=#{db}"
130+
end
128131
else
129-
"redis#{'s' if ssl?}://#{host}:#{port}/#{db}"
132+
url = "redis#{'s' if ssl?}://#{host}:#{port}"
133+
if db != 0
134+
url = "#{url}/#{db}"
135+
end
130136
end
137+
url
131138
end
132139

133140
private

test/redis_client/config_test.rb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -198,13 +198,13 @@ def test_overriding
198198
end
199199

200200
def test_server_url
201-
assert_equal "redis://localhost:6379/0", Config.new.server_url
202-
assert_equal "redis://localhost:6379/0", Config.new(username: "george", password: "hunter2").server_url
201+
assert_equal "redis://localhost:6379", Config.new.server_url
202+
assert_equal "redis://localhost:6379", Config.new(username: "george", password: "hunter2").server_url
203203
assert_equal "redis://localhost:6379/5", Config.new(db: 5).server_url
204-
assert_equal "redis://example.com:8080/0", Config.new(host: "example.com", port: 8080).server_url
205-
assert_equal "rediss://localhost:6379/0", Config.new(ssl: true).server_url
204+
assert_equal "redis://example.com:8080", Config.new(host: "example.com", port: 8080).server_url
205+
assert_equal "rediss://localhost:6379", Config.new(ssl: true).server_url
206206

207-
assert_equal "/var/redis/redis.sock/5", Config.new(path: "/var/redis/redis.sock", db: 5).server_url
207+
assert_equal "unix:///var/redis/redis.sock?db=5", Config.new(path: "/var/redis/redis.sock", db: 5).server_url
208208
end
209209

210210
def test_custom_field

test/redis_client/connection_test.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -319,7 +319,7 @@ def test_protocol_error
319319
new_client(host: "127.0.0.1", port: port).call("PING")
320320
end
321321

322-
assert_match(%r{ \(rediss?://127.0.0.1:#{port}/0\)$}, error.message)
322+
assert_match(%r{ \(rediss?://127.0.0.1:#{port}\)$}, error.message)
323323
ensure
324324
server_thread&.kill
325325
end
@@ -386,7 +386,7 @@ def test_reconnect_on_readonly
386386
client.call("SET", "foo", "bar")
387387
end
388388
refute_predicate client, :connected?
389-
assert_match(%r{ \(rediss?://127.0.0.1:#{port}/0\)$}, error.message)
389+
assert_match(%r{ \(rediss?://127.0.0.1:#{port}\)$}, error.message)
390390
ensure
391391
server_thread&.kill
392392
end

test/redis_client_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,7 @@ def test_measure_round_trip_delay
127127
end
128128

129129
def test_server_url
130-
assert_equal "redis://#{Servers::HOST}:#{Servers::REDIS_TCP_PORT}/0", @redis.server_url
130+
assert_equal "redis://#{Servers::HOST}:#{Servers::REDIS_TCP_PORT}", @redis.server_url
131131
end
132132

133133
def test_timeout

0 commit comments

Comments
 (0)