Skip to content

Commit 8705116

Browse files
committed
support a connection callback for proxies
1 parent 8cbbfc0 commit 8705116

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

lib/net/ldap.rb

+7
Original file line numberDiff line numberDiff line change
@@ -441,6 +441,10 @@ def self.result2string(code) #:nodoc:
441441
# described below. The following arguments are supported:
442442
# * :host => the LDAP server's IP-address (default 127.0.0.1)
443443
# * :port => the LDAP server's TCP port (default 389)
444+
# * :connect_cb => a Proc that will be called when a new connection is
445+
# needed. This should return an actual Ruby IO object. Useful for
446+
# manually handling connecting, like if you want to go through a proxy
447+
# server. It will receive :host: and :port: as arguments.
444448
# * :auth => a Hash containing authorization parameters. Currently
445449
# supported values include: {:method => :anonymous} and {:method =>
446450
# :simple, :username => your_user_name, :password => your_password }
@@ -469,6 +473,7 @@ def self.result2string(code) #:nodoc:
469473
def initialize(args = {})
470474
@host = args[:host] || DefaultHost
471475
@port = args[:port] || DefaultPort
476+
@connect_cb = args[:connect_cb]
472477
@verbose = false # Make this configurable with a switch on the class.
473478
@auth = args[:auth] || DefaultAuth
474479
@base = args[:base] || DefaultTreebase
@@ -1219,7 +1224,9 @@ def use_connection(args)
12191224

12201225
# Establish a new connection to the LDAP server
12211226
def new_connection
1227+
socket = @connect_cb.call(@host, @port) if @connect_cb
12221228
Net::LDAP::Connection.new \
1229+
:socket => socket,
12231230
:host => @host,
12241231
:port => @port,
12251232
:encryption => @encryption,

test/test_ldap.rb

+18
Original file line numberDiff line numberDiff line change
@@ -57,4 +57,22 @@ def test_instrument_search_with_size
5757
assert_equal "(uid=user1)", payload[:filter]
5858
assert_equal result.size, payload[:size]
5959
end
60+
61+
def test_connect_cb
62+
flexmock(Net::LDAP::Connection).should_receive(:new).with(
63+
:socket => 42,
64+
:host => "test.mocked.com",
65+
:port => 636,
66+
:encryption => nil,
67+
:instrumentation_service => @service).and_return(@connection)
68+
flexmock(@connection).should_receive(:bind).and_return(flexmock(:bind_result, :result_code => Net::LDAP::ResultCodeSuccess))
69+
70+
@subject = Net::LDAP.new \
71+
:connect_cb => lambda { |host, port| 42 },
72+
:host => "test.mocked.com", :port => 636,
73+
:force_no_page => true, # so server capabilities are not queried
74+
:instrumentation_service => @service
75+
76+
@subject.open {}
77+
end
6078
end

0 commit comments

Comments
 (0)