Skip to content

Commit 1aab8c9

Browse files
committed
set socket_class in initialize
1 parent d35d3bf commit 1aab8c9

File tree

3 files changed

+23
-30
lines changed

3 files changed

+23
-30
lines changed

Diff for: lib/net/ldap/connection.rb

+13-13
Original file line numberDiff line numberDiff line change
@@ -21,19 +21,10 @@ def initialize(server = {})
2121
@server = server
2222
@instrumentation_service = server[:instrumentation_service]
2323

24-
yield self if block_given?
25-
end
24+
# Allows tests to parameterize what socket class to use
25+
@socket_class = server.fetch(:socket_class, DefaultSocket)
2626

27-
# Allows tests to parameterize what socket class to use
28-
def socket_class
29-
@socket_class || DefaultSocket
30-
end
31-
32-
# Wrap around Socket.tcp to normalize with other Socket initializers
33-
class DefaultSocket
34-
def self.new(host, port, socket_opts = {})
35-
Socket.tcp(host, port, socket_opts)
36-
end
27+
yield self if block_given?
3728
end
3829

3930
def socket_class=(socket_class)
@@ -59,7 +50,7 @@ def open_connection(server)
5950
errors = []
6051
hosts.each do |host, port|
6152
begin
62-
prepare_socket(server.merge(socket: socket_class.new(host, port, socket_opts)))
53+
prepare_socket(server.merge(socket: @socket_class.new(host, port, socket_opts)))
6354
return
6455
rescue Net::LDAP::Error, SocketError, SystemCallError,
6556
OpenSSL::SSL::SSLError => e
@@ -690,4 +681,13 @@ def socket
690681

691682
@conn
692683
end
684+
685+
private
686+
687+
# Wrap around Socket.tcp to normalize with other Socket initializers
688+
class DefaultSocket
689+
def self.new(host, port, socket_opts = {})
690+
Socket.tcp(host, port, socket_opts)
691+
end
692+
end
693693
end # class Connection

Diff for: test/test_auth_adapter.rb

+1-2
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ def initialize(*args)
77
end
88

99
def test_undefined_auth_adapter
10-
conn = Net::LDAP::Connection.new(host: 'ldap.example.com', port: 379)
11-
conn.socket_class = FakeSocket
10+
conn = Net::LDAP::Connection.new(host: 'ldap.example.com', port: 379, :socket_class => FakeSocket)
1211
assert_raise Net::LDAP::AuthMethodUnsupportedError, "Unsupported auth method (foo)" do
1312
conn.bind(method: :foo)
1413
end

Diff for: test/test_ldap_connection.rb

+9-15
Original file line numberDiff line numberDiff line change
@@ -29,8 +29,7 @@ def test_list_of_hosts_with_first_host_successful
2929
["fail.SocketError", 636],
3030
]
3131

32-
connection = Net::LDAP::Connection.new(:hosts => hosts)
33-
connection.socket_class = FakeTCPSocket
32+
connection = Net::LDAP::Connection.new(:hosts => hosts, :socket_class => FakeTCPSocket)
3433
connection.socket
3534
end
3635

@@ -41,8 +40,7 @@ def test_list_of_hosts_with_first_host_failure
4140
["fail.SocketError", 636],
4241
]
4342

44-
connection = Net::LDAP::Connection.new(:hosts => hosts)
45-
connection.socket_class = FakeTCPSocket
43+
connection = Net::LDAP::Connection.new(:hosts => hosts, :socket_class => FakeTCPSocket)
4644
connection.socket
4745
end
4846

@@ -53,8 +51,7 @@ def test_list_of_hosts_with_all_hosts_failure
5351
["fail.SocketError", 636],
5452
]
5553

56-
connection = Net::LDAP::Connection.new(:hosts => hosts)
57-
connection.socket_class = FakeTCPSocket
54+
connection = Net::LDAP::Connection.new(:hosts => hosts, :socket_class => FakeTCPSocket)
5855
assert_raise Net::LDAP::ConnectionError do
5956
connection.socket
6057
end
@@ -75,24 +72,21 @@ def test_result_for_connection_failed_is_set
7572
end
7673

7774
def test_unresponsive_host
78-
connection = Net::LDAP::Connection.new(:host => "fail.Errno::ETIMEDOUT", :port => 636)
79-
connection.socket_class = FakeTCPSocket
75+
connection = Net::LDAP::Connection.new(:host => "fail.Errno::ETIMEDOUT", :port => 636, :socket_class => FakeTCPSocket)
8076
assert_raise Net::LDAP::Error do
8177
connection.socket
8278
end
8379
end
8480

8581
def test_blocked_port
86-
connection = Net::LDAP::Connection.new(:host => "fail.SocketError", :port => 636)
87-
connection.socket_class = FakeTCPSocket
82+
connection = Net::LDAP::Connection.new(:host => "fail.SocketError", :port => 636, :socket_class => FakeTCPSocket)
8883
assert_raise Net::LDAP::Error do
8984
connection.socket
9085
end
9186
end
9287

9388
def test_connection_refused
94-
connection = Net::LDAP::Connection.new(:host => "fail.Errno::ECONNREFUSED", :port => 636)
95-
connection.socket_class = FakeTCPSocket
89+
connection = Net::LDAP::Connection.new(:host => "fail.Errno::ECONNREFUSED", :port => 636, :socket_class => FakeTCPSocket)
9690
stderr = capture_stderr do
9791
assert_raise Net::LDAP::ConnectionRefusedError do
9892
connection.socket
@@ -102,7 +96,7 @@ def test_connection_refused
10296
end
10397

10498
def test_connection_timeout
105-
connection = Net::LDAP::Connection.new(:host => "fail.Errno::ETIMEDOUT", :port => 636)
99+
connection = Net::LDAP::Connection.new(:host => "fail.Errno::ETIMEDOUT", :port => 636, :socket_class => FakeTCPSocket)
106100
stderr = capture_stderr do
107101
assert_raise Net::LDAP::Error do
108102
connection.socket
@@ -111,8 +105,8 @@ def test_connection_timeout
111105
end
112106

113107
def test_raises_unknown_exceptions
114-
connection = Net::LDAP::Connection.new(:host => "fail.StandardError", :port => 636)
115-
assert_raise Net::LDAP::Error do
108+
connection = Net::LDAP::Connection.new(:host => "fail.StandardError", :port => 636, :socket_class => FakeTCPSocket)
109+
assert_raise StandardError do
116110
connection.socket
117111
end
118112
end

0 commit comments

Comments
 (0)