! A proper fix for the error I've found yesterday.
We should further look for empty rescue statements, as they may sometimes catch stuff that we don't want to be caught.master
parent
6a17e6a2c2
commit
31ba47cf1d
|
@ -1,6 +1,5 @@
|
|||
require 'openssl'
|
||||
require 'ostruct'
|
||||
|
||||
require 'socket'
|
||||
|
||||
require 'net/ber'
|
||||
|
@ -1122,8 +1121,10 @@ module Net
|
|||
def initialize server
|
||||
begin
|
||||
@conn = TCPSocket.new( server[:host], server[:port] )
|
||||
rescue
|
||||
raise LdapError.new( "no connection to server" )
|
||||
rescue SocketError
|
||||
raise LdapError, "No such address or other socket error."
|
||||
rescue Errno::ECONNREFUSED
|
||||
raise LdapError, "Server #{server[:host]} refused connection on port #{server[:port]}."
|
||||
end
|
||||
|
||||
if server[:encryption]
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
require 'net/ldap'
|
||||
|
||||
Spec::Runner.configure do |config|
|
||||
config.mock_with :flexmock
|
||||
end
|
|
@ -0,0 +1,48 @@
|
|||
require 'spec_helper'
|
||||
|
||||
describe Net::LDAP::Connection do
|
||||
describe "initialize" do
|
||||
context "when host is not responding" do
|
||||
before(:each) do
|
||||
flexmock(TCPSocket).
|
||||
should_receive(:new).and_raise(Errno::ECONNREFUSED)
|
||||
end
|
||||
|
||||
it "should raise LdapError" do
|
||||
lambda {
|
||||
Net::LDAP::Connection.new(
|
||||
:server => 'test.mocked.com',
|
||||
:port => 636)
|
||||
}.should raise_error(Net::LDAP::LdapError)
|
||||
end
|
||||
end
|
||||
context "when host is blocking the port" do
|
||||
before(:each) do
|
||||
flexmock(TCPSocket).
|
||||
should_receive(:new).and_raise(SocketError)
|
||||
end
|
||||
|
||||
it "should raise LdapError" do
|
||||
lambda {
|
||||
Net::LDAP::Connection.new(
|
||||
:server => 'test.mocked.com',
|
||||
:port => 636)
|
||||
}.should raise_error(Net::LDAP::LdapError)
|
||||
end
|
||||
end
|
||||
context "on other exceptions" do
|
||||
before(:each) do
|
||||
flexmock(TCPSocket).
|
||||
should_receive(:new).and_raise(NameError)
|
||||
end
|
||||
|
||||
it "should rethrow the exception" do
|
||||
lambda {
|
||||
Net::LDAP::Connection.new(
|
||||
:server => 'test.mocked.com',
|
||||
:port => 636)
|
||||
}.should raise_error(NameError)
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
Loading…
Reference in New Issue