From df0d0b47b331e6130956e5548ac4bd3074f2e3c6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dennis=20G=C3=BCnnewig?= Date: Thu, 5 Nov 2015 17:32:10 +0100 Subject: [PATCH] Set operation result if LDAP server is not accessible --- lib/net/ldap.rb | 6 ++++++ test/test_ldap_connection.rb | 13 +++++++++++++ 2 files changed, 19 insertions(+) diff --git a/lib/net/ldap.rb b/lib/net/ldap.rb index 7c151895..0ec7fbb7 100644 --- a/lib/net/ldap.rb +++ b/lib/net/ldap.rb @@ -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 diff --git a/test/test_ldap_connection.rb b/test/test_ldap_connection.rb index 73752631..b4c77615 100644 --- a/test/test_ldap_connection.rb +++ b/test/test_ldap_connection.rb @@ -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)