@@ -9,44 +9,55 @@ def capture_stderr
99 $stderr = stderr
1010 end
1111
12+ # Fake socket for testing
13+ #
14+ # FakeTCPSocket.new("success", 636)
15+ # FakeTCPSocket.new("fail.SocketError", 636) # raises SocketError
16+ class FakeTCPSocket
17+ def initialize ( host , port , socket_opts = { } )
18+ status , error = host . split ( "." )
19+ if status == "fail"
20+ raise Object . const_get ( error )
21+ end
22+ end
23+ end
24+
1225 def test_list_of_hosts_with_first_host_successful
1326 hosts = [
14- [ 'test.mocked.com' , 636 ] ,
15- [ 'test2.mocked.com' , 636 ] ,
16- [ 'test3.mocked.com' , 636 ] ,
27+ [ "success.host" , 636 ] ,
28+ [ "fail.SocketError" , 636 ] ,
29+ [ "fail.SocketError" , 636 ] ,
1730 ]
18- flexmock ( Socket ) . should_receive ( :tcp ) . ordered . with ( * hosts [ 0 ] , { connect_timeout : 5 } ) . once . and_return ( nil )
19- flexmock ( Socket ) . should_receive ( :tcp ) . ordered . never
20- Net :: LDAP :: Connection . new ( :hosts => hosts )
31+
32+ connection = Net :: LDAP :: Connection . new ( :hosts => hosts , :socket_class => FakeTCPSocket )
33+ connection . socket
2134 end
2235
2336 def test_list_of_hosts_with_first_host_failure
2437 hosts = [
25- [ 'test.mocked.com' , 636 ] ,
26- [ 'test2.mocked.com' , 636 ] ,
27- [ 'test3.mocked.com' , 636 ] ,
38+ [ "fail.SocketError" , 636 ] ,
39+ [ "success.host" , 636 ] ,
40+ [ "fail.SocketError" , 636 ] ,
2841 ]
29- flexmock ( Socket ) . should_receive ( :tcp ) . ordered . with ( *hosts [ 0 ] , { connect_timeout : 5 } ) . once . and_raise ( SocketError )
30- flexmock ( Socket ) . should_receive ( :tcp ) . ordered . with ( *hosts [ 1 ] , { connect_timeout : 5 } ) . once . and_return ( nil )
31- flexmock ( Socket ) . should_receive ( :tcp ) . ordered . never
32- Net ::LDAP ::Connection . new ( :hosts => hosts )
42+
43+ connection = Net ::LDAP ::Connection . new ( :hosts => hosts , :socket_class => FakeTCPSocket )
44+ connection . socket
3345 end
3446
3547 def test_list_of_hosts_with_all_hosts_failure
3648 hosts = [
37- [ 'test.mocked.com' , 636 ] ,
38- [ 'test2.mocked.com' , 636 ] ,
39- [ 'test3.mocked.com' , 636 ] ,
49+ [ "fail.SocketError" , 636 ] ,
50+ [ "fail.SocketError" , 636 ] ,
51+ [ "fail.SocketError" , 636 ] ,
4052 ]
41- flexmock ( Socket ) . should_receive ( :tcp ) . ordered . with ( *hosts [ 0 ] , { connect_timeout : 5 } ) . once . and_raise ( SocketError )
42- flexmock ( Socket ) . should_receive ( :tcp ) . ordered . with ( *hosts [ 1 ] , { connect_timeout : 5 } ) . once . and_raise ( SocketError )
43- flexmock ( Socket ) . should_receive ( :tcp ) . ordered . with ( *hosts [ 2 ] , { connect_timeout : 5 } ) . once . and_raise ( SocketError )
44- flexmock ( Socket ) . should_receive ( :tcp ) . ordered . never
53+
54+ connection = Net ::LDAP ::Connection . new ( :hosts => hosts , :socket_class => FakeTCPSocket )
4555 assert_raise Net ::LDAP ::ConnectionError do
46- Net :: LDAP :: Connection . new ( :hosts => hosts )
56+ connection . socket
4757 end
4858 end
4959
60+ # This belongs in test_ldap, not test_ldap_connection
5061 def test_result_for_connection_failed_is_set
5162 flexmock ( Socket ) . should_receive ( :tcp ) . and_raise ( Errno ::ECONNREFUSED )
5263
@@ -61,42 +72,42 @@ def test_result_for_connection_failed_is_set
6172 end
6273
6374 def test_unresponsive_host
75+ connection = Net ::LDAP ::Connection . new ( :host => "fail.Errno::ETIMEDOUT" , :port => 636 , :socket_class => FakeTCPSocket )
6476 assert_raise Net ::LDAP ::Error do
65- Net :: LDAP :: Connection . new ( :host => 'test.mocked.com' , :port => 636 )
77+ connection . socket
6678 end
6779 end
6880
6981 def test_blocked_port
70- flexmock ( Socket ) . should_receive ( :tcp ) . and_raise ( SocketError )
82+ connection = Net :: LDAP :: Connection . new ( :host => "fail. SocketError" , :port => 636 , :socket_class => FakeTCPSocket )
7183 assert_raise Net ::LDAP ::Error do
72- Net :: LDAP :: Connection . new ( :host => 'test.mocked.com' , :port => 636 )
84+ connection . socket
7385 end
7486 end
7587
7688 def test_connection_refused
77- flexmock ( Socket ) . should_receive ( :tcp ) . and_raise ( Errno ::ECONNREFUSED )
89+ connection = Net :: LDAP :: Connection . new ( :host => "fail. Errno::ECONNREFUSED" , :port => 636 , :socket_class => FakeTCPSocket )
7890 stderr = capture_stderr do
7991 assert_raise Net ::LDAP ::ConnectionRefusedError do
80- Net :: LDAP :: Connection . new ( :host => 'test.mocked.com' , :port => 636 )
92+ connection . socket
8193 end
8294 end
8395 assert_equal ( "Deprecation warning: Net::LDAP::ConnectionRefused will be deprecated. Use Errno::ECONNREFUSED instead.\n " , stderr )
8496 end
8597
86- def test_connection_timedout
87- flexmock ( Socket ) . should_receive ( :tcp ) . and_raise ( Errno ::ETIMEDOUT )
98+ def test_connection_timeout
99+ connection = Net :: LDAP :: Connection . new ( :host => "fail. Errno::ETIMEDOUT" , :port => 636 , :socket_class => FakeTCPSocket )
88100 stderr = capture_stderr do
89101 assert_raise Net ::LDAP ::Error do
90- Net :: LDAP :: Connection . new ( :host => 'test.mocked.com' , :port => 636 )
102+ connection . socket
91103 end
92104 end
93105 end
94106
95107 def test_raises_unknown_exceptions
96- error = Class . new ( StandardError )
97- flexmock ( Socket ) . should_receive ( :tcp ) . and_raise ( error )
98- assert_raise error do
99- Net ::LDAP ::Connection . new ( :host => 'test.mocked.com' , :port => 636 )
108+ connection = Net ::LDAP ::Connection . new ( :host => "fail.StandardError" , :port => 636 , :socket_class => FakeTCPSocket )
109+ assert_raise StandardError do
110+ connection . socket
100111 end
101112 end
102113
0 commit comments