@@ -9,44 +9,55 @@ def capture_stderr
9
9
$stderr = stderr
10
10
end
11
11
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
+
12
25
def test_list_of_hosts_with_first_host_successful
13
26
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 ] ,
17
30
]
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
21
34
end
22
35
23
36
def test_list_of_hosts_with_first_host_failure
24
37
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 ] ,
28
41
]
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
33
45
end
34
46
35
47
def test_list_of_hosts_with_all_hosts_failure
36
48
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 ] ,
40
52
]
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 )
45
55
assert_raise Net ::LDAP ::ConnectionError do
46
- Net :: LDAP :: Connection . new ( :hosts => hosts )
56
+ connection . socket
47
57
end
48
58
end
49
59
60
+ # This belongs in test_ldap, not test_ldap_connection
50
61
def test_result_for_connection_failed_is_set
51
62
flexmock ( Socket ) . should_receive ( :tcp ) . and_raise ( Errno ::ECONNREFUSED )
52
63
@@ -61,42 +72,42 @@ def test_result_for_connection_failed_is_set
61
72
end
62
73
63
74
def test_unresponsive_host
75
+ connection = Net ::LDAP ::Connection . new ( :host => "fail.Errno::ETIMEDOUT" , :port => 636 , :socket_class => FakeTCPSocket )
64
76
assert_raise Net ::LDAP ::Error do
65
- Net :: LDAP :: Connection . new ( :host => 'test.mocked.com' , :port => 636 )
77
+ connection . socket
66
78
end
67
79
end
68
80
69
81
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 )
71
83
assert_raise Net ::LDAP ::Error do
72
- Net :: LDAP :: Connection . new ( :host => 'test.mocked.com' , :port => 636 )
84
+ connection . socket
73
85
end
74
86
end
75
87
76
88
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 )
78
90
stderr = capture_stderr do
79
91
assert_raise Net ::LDAP ::ConnectionRefusedError do
80
- Net :: LDAP :: Connection . new ( :host => 'test.mocked.com' , :port => 636 )
92
+ connection . socket
81
93
end
82
94
end
83
95
assert_equal ( "Deprecation warning: Net::LDAP::ConnectionRefused will be deprecated. Use Errno::ECONNREFUSED instead.\n " , stderr )
84
96
end
85
97
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 )
88
100
stderr = capture_stderr do
89
101
assert_raise Net ::LDAP ::Error do
90
- Net :: LDAP :: Connection . new ( :host => 'test.mocked.com' , :port => 636 )
102
+ connection . socket
91
103
end
92
104
end
93
105
end
94
106
95
107
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
100
111
end
101
112
end
102
113
0 commit comments