Skip to content

Commit

Permalink
Merge pull request ruby-ldap#232 from dg-ratiodata/feature/set_operat…
Browse files Browse the repository at this point in the history
…ion_result_connection_refused

Set operation result if LDAP server is not accessible
  • Loading branch information
jch committed Nov 6, 2015
2 parents 578b3d6 + df0d0b4 commit 01ee008
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 0 deletions.
6 changes: 6 additions & 0 deletions lib/net/ldap.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1243,5 +1243,11 @@ def new_connection
:hosts => @hosts,
:encryption => @encryption,
:instrumentation_service => @instrumentation_service
rescue Errno::ECONNREFUSED, Net::LDAP::ConnectionRefusedError => e
@result = {
:resultCode => 52,
:errorMessage => ResultStrings[ResultCodeUnavailable]
}
raise e
end
end # class LDAP
13 changes: 13 additions & 0 deletions test/test_ldap_connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,19 @@ def test_list_of_hosts_with_all_hosts_failure
end
end

def test_result_for_connection_failed_is_set
flexmock(TCPSocket).should_receive(:new).and_raise(Errno::ECONNREFUSED)

ldap_client = Net::LDAP.new(host: '127.0.0.1', port: 12345)

assert_raise Net::LDAP::ConnectionRefusedError do
ldap_client.bind(method: :simple, username: 'asdf', password: 'asdf')
end

assert_equal(ldap_client.get_operation_result.code, 52)
assert_equal(ldap_client.get_operation_result.message, 'Unavailable')
end

def test_unresponsive_host
assert_raise Net::LDAP::Error do
Net::LDAP::Connection.new(:host => 'test.mocked.com', :port => 636)
Expand Down

0 comments on commit 01ee008

Please sign in to comment.