Skip to content

Commit c3fe34f

Browse files
committed
Fix order of protocols in endpoint construction.
1 parent 6ec3e84 commit c3fe34f

File tree

4 files changed

+39
-33
lines changed

4 files changed

+39
-33
lines changed

lib/async/redis/endpoint.rb

+11-5
Original file line numberDiff line numberDiff line change
@@ -118,14 +118,14 @@ def secure?
118118
def protocol
119119
protocol = @options.fetch(:protocol, Protocol::RESP2)
120120

121-
if database = self.database
122-
protocol = Protocol::Selected.new(database, protocol)
123-
end
124-
125121
if credentials = self.credentials
126122
protocol = Protocol::Authenticated.new(credentials, protocol)
127123
end
128124

125+
if database = self.database
126+
protocol = Protocol::Selected.new(database, protocol)
127+
end
128+
129129
return protocol
130130
end
131131

@@ -151,7 +151,13 @@ def scheme
151151
end
152152

153153
def database
154-
@options[:database] || @url.path[1..-1].to_i
154+
@options[:database] || extract_database(@url.path)
155+
end
156+
157+
private def extract_database(path)
158+
if path =~ /\/(\d+)$/
159+
return $1.to_i
160+
end
155161
end
156162

157163
def credentials

lib/async/redis/protocol/authenticated.rb

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def initialize(credentials, protocol = Async::Redis::Protocol::RESP2)
2323
@protocol = protocol
2424
end
2525

26+
attr :credentials
27+
2628
# Create a new client and authenticate it.
2729
def client(stream)
2830
client = @protocol.client(stream)

lib/async/redis/protocol/selected.rb

+2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ def initialize(index, protocol = Async::Redis::Protocol::RESP2)
2323
@protocol = protocol
2424
end
2525

26+
attr :index
27+
2628
# Create a new client and authenticate it.
2729
def client(stream)
2830
client = @protocol.client(stream)

test/async/redis/endpoint.rb

+24-28
Original file line numberDiff line numberDiff line change
@@ -7,37 +7,33 @@
77
require 'async/redis/protocol/authenticated'
88
require 'sus/fixtures/async'
99

10-
describe Async::Redis::Protocol::Authenticated do
10+
describe Async::Redis::Endpoint do
1111
include Sus::Fixtures::Async::ReactorContext
1212

1313
let(:endpoint) {Async::Redis.local_endpoint}
14-
let(:credentials) {["testuser", "testpassword"]}
15-
let(:protocol) {subject.new(credentials)}
16-
let(:client) {Async::Redis::Client.new(endpoint, protocol: protocol)}
1714

18-
before do
19-
# Setup ACL user with limited permissions for testing.
20-
admin_client = Async::Redis::Client.new(endpoint)
21-
admin_client.call("ACL", "SETUSER", "testuser", "on", ">" + credentials[1], "+ping", "+auth")
22-
ensure
23-
admin_client.close
24-
end
25-
26-
after do
27-
# Cleanup ACL user after tests.
28-
admin_client = Async::Redis::Client.new(endpoint)
29-
admin_client.call("ACL", "DELUSER", "testuser")
30-
admin_client.close
31-
end
32-
33-
it "can authenticate and send allowed commands" do
34-
response = client.call("PING")
35-
expect(response).to be == "PONG"
36-
end
37-
38-
it "rejects commands not allowed by ACL" do
39-
expect do
40-
client.call("SET", "key", "value")
41-
end.to raise_exception(Protocol::Redis::ServerError, message: be =~ /NOPERM/)
15+
with '#protocol' do
16+
it "defaults to RESP2" do
17+
expect(endpoint.protocol).to be == Async::Redis::Protocol::RESP2
18+
end
19+
20+
with 'database selection' do
21+
let(:endpoint) {Async::Redis.local_endpoint(database: 1)}
22+
23+
it "selects the database" do
24+
expect(endpoint.protocol).to be_a(Async::Redis::Protocol::Selected)
25+
expect(endpoint.protocol.index).to be == 1
26+
end
27+
end
28+
29+
with 'credentials' do
30+
let(:credentials) {["testuser", "testpassword"]}
31+
let(:endpoint) {Async::Redis.local_endpoint(credentials: credentials)}
32+
33+
it "authenticates with credentials" do
34+
expect(endpoint.protocol).to be_a(Async::Redis::Protocol::Authenticated)
35+
expect(endpoint.protocol.credentials).to be == credentials
36+
end
37+
end
4238
end
4339
end

0 commit comments

Comments
 (0)