File tree Expand file tree Collapse file tree 3 files changed +54
-3
lines changed
Expand file tree Collapse file tree 3 files changed +54
-3
lines changed Original file line number Diff line number Diff line change 11require 'openssl'
22require 'ostruct'
3-
43require 'socket'
54
65require 'net/ber'
@@ -1122,8 +1121,10 @@ class Connection # :nodoc:
11221121 def initialize server
11231122 begin
11241123 @conn = TCPSocket . new ( server [ :host ] , server [ :port ] )
1125- rescue
1126- raise LdapError . new ( "no connection to server" )
1124+ rescue SocketError
1125+ raise LdapError , "No such address or other socket error."
1126+ rescue Errno ::ECONNREFUSED
1127+ raise LdapError , "Server #{ server [ :host ] } refused connection on port #{ server [ :port ] } ."
11271128 end
11281129
11291130 if server [ :encryption ]
Original file line number Diff line number Diff line change 1+ require 'net/ldap'
2+
13Spec ::Runner . configure do |config |
24 config . mock_with :flexmock
35end
Original file line number Diff line number Diff line change 1+ require 'spec_helper'
2+
3+ describe Net ::LDAP ::Connection do
4+ describe "initialize" do
5+ context "when host is not responding" do
6+ before ( :each ) do
7+ flexmock ( TCPSocket ) .
8+ should_receive ( :new ) . and_raise ( Errno ::ECONNREFUSED )
9+ end
10+
11+ it "should raise LdapError" do
12+ lambda {
13+ Net ::LDAP ::Connection . new (
14+ :server => 'test.mocked.com' ,
15+ :port => 636 )
16+ } . should raise_error ( Net ::LDAP ::LdapError )
17+ end
18+ end
19+ context "when host is blocking the port" do
20+ before ( :each ) do
21+ flexmock ( TCPSocket ) .
22+ should_receive ( :new ) . and_raise ( SocketError )
23+ end
24+
25+ it "should raise LdapError" do
26+ lambda {
27+ Net ::LDAP ::Connection . new (
28+ :server => 'test.mocked.com' ,
29+ :port => 636 )
30+ } . should raise_error ( Net ::LDAP ::LdapError )
31+ end
32+ end
33+ context "on other exceptions" do
34+ before ( :each ) do
35+ flexmock ( TCPSocket ) .
36+ should_receive ( :new ) . and_raise ( NameError )
37+ end
38+
39+ it "should rethrow the exception" do
40+ lambda {
41+ Net ::LDAP ::Connection . new (
42+ :server => 'test.mocked.com' ,
43+ :port => 636 )
44+ } . should raise_error ( NameError )
45+ end
46+ end
47+ end
48+ end
You can’t perform that action at this time.
0 commit comments