Skip to content

Commit a7cdc1e

Browse files
committed
Better handling of url userinfo.
1 parent c3fe34f commit a7cdc1e

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

lib/async/redis/endpoint.rb

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,17 @@ def database
161161
end
162162

163163
def credentials
164-
@options[:credentials] || @url.userinfo&.split(":")
164+
@options[:credentials] || extract_userinfo(@url.userinfo)
165+
end
166+
167+
private def extract_userinfo(userinfo)
168+
if userinfo
169+
credentials = userinfo.split(":").reject(&:empty?)
170+
171+
if credentials.any?
172+
return credentials
173+
end
174+
end
165175
end
166176

167177
def localhost?

test/async/redis/endpoint.rb

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,23 @@
1212

1313
let(:endpoint) {Async::Redis.local_endpoint}
1414

15+
with '#credentials' do
16+
it "can parse a url with username and password" do
17+
endpoint = Async::Redis::Endpoint.parse("redis://testuser:testpassword@localhost")
18+
expect(endpoint.credentials).to be == ["testuser", "testpassword"]
19+
end
20+
21+
it "can parse a url with a blank username and password" do
22+
endpoint = Async::Redis::Endpoint.parse("redis://:testpassword@localhost")
23+
expect(endpoint.credentials).to be == ["testpassword"]
24+
end
25+
26+
it "can parse a url with a password only" do
27+
endpoint = Async::Redis::Endpoint.parse("redis://testpassword@localhost")
28+
expect(endpoint.credentials).to be == ["testpassword"]
29+
end
30+
end
31+
1532
with '#protocol' do
1633
it "defaults to RESP2" do
1734
expect(endpoint.protocol).to be == Async::Redis::Protocol::RESP2

0 commit comments

Comments
 (0)