Skip to content

Commit

Permalink
support a connection callback for proxies
Browse files Browse the repository at this point in the history
  • Loading branch information
ccutrer committed Dec 19, 2014
1 parent 443fdb1 commit 9cc68db
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
7 changes: 7 additions & 0 deletions lib/net/ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -441,6 +441,10 @@ def self.result2string(code) #:nodoc:
# described below. The following arguments are supported:
# * :host => the LDAP server's IP-address (default 127.0.0.1)
# * :port => the LDAP server's TCP port (default 389)
# * :connect_cb => a Proc that will be called when a new connection is
# needed. This should return an actual Ruby IO object. Useful for
# manually handling connecting, like if you want to go through a proxy
# server. It will receive :host: and :port: as arguments.
# * :auth => a Hash containing authorization parameters. Currently
# supported values include: {:method => :anonymous} and {:method =>
# :simple, :username => your_user_name, :password => your_password }
Expand Down Expand Up @@ -469,6 +473,7 @@ def self.result2string(code) #:nodoc:
def initialize(args = {})
@host = args[:host] || DefaultHost
@port = args[:port] || DefaultPort
@connect_cb = args[:connect_cb]
@verbose = false # Make this configurable with a switch on the class.
@auth = args[:auth] || DefaultAuth
@base = args[:base] || DefaultTreebase
Expand Down Expand Up @@ -1221,7 +1226,9 @@ def use_connection(args)

# Establish a new connection to the LDAP server
def new_connection
socket = @connect_cb.call(@host, @port) if @connect_cb
Net::LDAP::Connection.new \
:socket => socket,
:host => @host,
:port => @port,
:encryption => @encryption,
Expand Down
18 changes: 18 additions & 0 deletions test/test_ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,4 +57,22 @@ def test_instrument_search_with_size
assert_equal "(uid=user1)", payload[:filter]
assert_equal result.size, payload[:size]
end

def test_connect_cb
flexmock(Net::LDAP::Connection).should_receive(:new).with(
:socket => 42,
:host => "test.mocked.com",
:port => 636,
:encryption => nil,
:instrumentation_service => @service).and_return(@connection)
flexmock(@connection).should_receive(:bind).and_return(flexmock(:bind_result, :result_code => Net::LDAP::ResultCodeSuccess))

@subject = Net::LDAP.new \
:connect_cb => lambda { |host, port| 42 },
:host => "test.mocked.com", :port => 636,
:force_no_page => true, # so server capabilities are not queried
:instrumentation_service => @service

@subject.open {}
end
end

0 comments on commit 9cc68db

Please sign in to comment.