From 9cc68dbe4a103d8d6c320ee86acb8fe013caf3b1 Mon Sep 17 00:00:00 2001 From: Cody Cutrer Date: Wed, 17 Dec 2014 14:49:21 -0700 Subject: [PATCH] support a connection callback for proxies --- lib/net/ldap.rb | 7 +++++++ test/test_ldap.rb | 18 ++++++++++++++++++ 2 files changed, 25 insertions(+) diff --git a/lib/net/ldap.rb b/lib/net/ldap.rb index d61f3dc4..937a5d5e 100644 --- a/lib/net/ldap.rb +++ b/lib/net/ldap.rb @@ -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 } @@ -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 @@ -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, diff --git a/test/test_ldap.rb b/test/test_ldap.rb index 9704b346..b58cbe2f 100644 --- a/test/test_ldap.rb +++ b/test/test_ldap.rb @@ -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