Skip to content

Commit

Permalink
Merge pull request #213 from satoryu/use_connection_refused_error
Browse files Browse the repository at this point in the history
Raise Net::LDAP::ConnectionRefusedError when new connection is refused.
  • Loading branch information
jch committed Sep 18, 2015
2 parents 49c0265 + b4a3bd2 commit 4abe573
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 2 deletions.
2 changes: 1 addition & 1 deletion lib/net/ldap/connection.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def initialize(server)
rescue SocketError
raise Net::LDAP::Error, "No such address or other socket error."
rescue Errno::ECONNREFUSED
raise Net::LDAP::Error, "Server #{server[:host]} refused connection on port #{server[:port]}."
raise Net::LDAP::ConnectionRefusedError, "Server #{server[:host]} refused connection on port #{server[:port]}."
rescue Errno::EHOSTUNREACH => error
raise Net::LDAP::Error, "Host #{server[:host]} was unreachable (#{error.message})"
rescue Errno::ETIMEDOUT
Expand Down
17 changes: 16 additions & 1 deletion lib/net/ldap/error.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,22 @@ class Error < StandardError; end

class AlreadyOpenedError < Error; end
class SocketError < Error; end
class ConnectionRefusedError < Error; end
class ConnectionRefusedError < Error;
def initialize(*args)
warn_deprecation_message
super
end

def message
warn_deprecation_message
super
end

private
def warn_deprecation_message
warn "Deprecation warning: Net::LDAP::ConnectionRefused will be deprecated. Use Errno::ECONNREFUSED instead."
end
end
class NoOpenSSLError < Error; end
class NoStartTLSResultError < Error; end
class NoSearchBaseError < Error; end
Expand Down
18 changes: 18 additions & 0 deletions test/test_ldap_connection.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
require_relative 'test_helper'

class TestLDAPConnection < Test::Unit::TestCase
def capture_stderr
stderr, $stderr = $stderr, StringIO.new
yield
$stderr.string
ensure
$stderr = stderr
end

def test_unresponsive_host
assert_raise Net::LDAP::Error do
Net::LDAP::Connection.new(:host => 'test.mocked.com', :port => 636)
Expand All @@ -14,6 +22,16 @@ def test_blocked_port
end
end

def test_connection_refused
flexmock(TCPSocket).should_receive(:new).and_raise(Errno::ECONNREFUSED)
stderr = capture_stderr do
assert_raise Net::LDAP::ConnectionRefusedError do
Net::LDAP::Connection.new(:host => 'test.mocked.com', :port => 636)
end
end
assert_equal("Deprecation warning: Net::LDAP::ConnectionRefused will be deprecated. Use Errno::ECONNREFUSED instead.\n", stderr)
end

def test_raises_unknown_exceptions
error = Class.new(StandardError)
flexmock(TCPSocket).should_receive(:new).and_raise(error)
Expand Down

0 comments on commit 4abe573

Please sign in to comment.