net-ping 1.7.4-universal-mingw32 → 1.7.5-universal-mingw32

Sign up to get free protection for your applications and to get access to all the features.
data/net-ping.gemspec CHANGED
@@ -1,39 +1,39 @@
1
- require 'rubygems'
2
- require 'rbconfig'
3
-
4
- Gem::Specification.new do |spec|
5
- spec.name = 'net-ping'
6
- spec.version = '1.7.4'
7
- spec.license = 'Artistic 2.0'
8
- spec.author = 'Daniel J. Berger'
9
- spec.email = 'djberg96@gmail.com'
10
- spec.homepage = 'https://github.com/djberg96/net-ping'
11
- spec.summary = 'A ping interface for Ruby.'
12
- spec.test_file = 'test/test_net_ping.rb'
13
- spec.files = Dir['**/*'].reject{ |f| f.include?('git') }
14
-
15
- spec.extra_rdoc_files = ['README', 'CHANGES', 'doc/ping.txt']
16
-
17
- # The TCP Ping class requires this for non-blocking sockets.
18
- spec.required_ruby_version = ">= 1.9.3"
19
-
20
- spec.add_development_dependency('test-unit')
21
- spec.add_development_dependency('fakeweb')
22
- spec.add_development_dependency('rake')
23
-
24
- if File::ALT_SEPARATOR
25
- require 'rbconfig'
26
- arch = RbConfig::CONFIG['build_os']
27
- spec.platform = Gem::Platform.new(['universal', arch])
28
- spec.platform.version = nil
29
-
30
- # Used for icmp pings.
31
- spec.add_dependency('win32-security', '>= 0.2.0')
32
- end
33
-
34
- spec.description = <<-EOF
35
- The net-ping library provides a ping interface for Ruby. It includes
36
- separate TCP, HTTP, LDAP, ICMP, UDP, WMI (for Windows) and external ping
37
- classes.
38
- EOF
39
- end
1
+ require 'rubygems'
2
+ require 'rbconfig'
3
+
4
+ Gem::Specification.new do |spec|
5
+ spec.name = 'net-ping'
6
+ spec.version = '1.7.5'
7
+ spec.license = 'Artistic 2.0'
8
+ spec.author = 'Daniel J. Berger'
9
+ spec.email = 'djberg96@gmail.com'
10
+ spec.homepage = 'https://github.com/djberg96/net-ping'
11
+ spec.summary = 'A ping interface for Ruby.'
12
+ spec.test_file = 'test/test_net_ping.rb'
13
+ spec.files = Dir['**/*'].reject{ |f| f.include?('git') }
14
+
15
+ spec.extra_rdoc_files = ['README', 'CHANGES', 'doc/ping.txt']
16
+
17
+ # The TCP Ping class requires this for non-blocking sockets.
18
+ spec.required_ruby_version = ">= 1.9.3"
19
+
20
+ spec.add_development_dependency('test-unit')
21
+ spec.add_development_dependency('fakeweb')
22
+ spec.add_development_dependency('rake')
23
+
24
+ if File::ALT_SEPARATOR
25
+ require 'rbconfig'
26
+ arch = RbConfig::CONFIG['build_os']
27
+ spec.platform = Gem::Platform.new(['universal', arch])
28
+ spec.platform.version = nil
29
+
30
+ # Used for icmp pings.
31
+ spec.add_dependency('win32-security', '>= 0.2.0')
32
+ end
33
+
34
+ spec.description = <<-EOF
35
+ The net-ping library provides a ping interface for Ruby. It includes
36
+ separate TCP, HTTP, LDAP, ICMP, UDP, WMI (for Windows) and external ping
37
+ classes.
38
+ EOF
39
+ end
@@ -1,35 +1,35 @@
1
- ######################################################################
2
- # test_net_ping.rb
3
- #
4
- # Test suite for all the Ping subclasses. Note that the Ping::ICMP
5
- # class test won't be run unless this is run as a privileged process.
6
- ######################################################################
7
- require 'test_net_ping_external'
8
- require 'test_net_ping_http'
9
- require 'test_net_ping_tcp'
10
- require 'test_net_ping_udp'
11
- require 'fakeweb'
12
-
13
- if File::ALT_SEPARATOR
14
- require 'win32/security'
15
-
16
- if Win32::Security.elevated_security?
17
- require 'test_net_ping_icmp'
18
- end
19
- else
20
- if Process.euid == 0
21
- require 'test_net_ping_icmp'
22
- end
23
- end
24
-
25
- if File::ALT_SEPARATOR
26
- require 'test_net_ping_wmi'
27
- end
28
-
29
- class TC_Net_Ping < Test::Unit::TestCase
30
- def test_net_ping_version
31
- assert_equal('1.7.4', Net::Ping::VERSION)
32
- end
33
- end
34
-
35
- FakeWeb.allow_net_connect = false
1
+ ######################################################################
2
+ # test_net_ping.rb
3
+ #
4
+ # Test suite for all the Ping subclasses. Note that the Ping::ICMP
5
+ # class test won't be run unless this is run as a privileged process.
6
+ ######################################################################
7
+ require 'test_net_ping_external'
8
+ require 'test_net_ping_http'
9
+ require 'test_net_ping_tcp'
10
+ require 'test_net_ping_udp'
11
+ require 'fakeweb'
12
+
13
+ if File::ALT_SEPARATOR
14
+ require 'win32/security'
15
+
16
+ if Win32::Security.elevated_security?
17
+ require 'test_net_ping_icmp'
18
+ end
19
+ else
20
+ if Process.euid == 0
21
+ require 'test_net_ping_icmp'
22
+ end
23
+ end
24
+
25
+ if File::ALT_SEPARATOR
26
+ require 'test_net_ping_wmi'
27
+ end
28
+
29
+ class TC_Net_Ping < Test::Unit::TestCase
30
+ def test_net_ping_version
31
+ assert_equal('1.7.5', Net::Ping::VERSION)
32
+ end
33
+ end
34
+
35
+ FakeWeb.allow_net_connect = false
@@ -1,143 +1,143 @@
1
- #########################################################################
2
- # test_net_ping_external.rb
3
- #
4
- # Test case for the Net::PingExternal class. Run this via the 'test' or
5
- # 'test:external' rake task.
6
- #
7
- # WARNING: I've noticed that test failures will occur if you're using
8
- # OpenDNS. This is apparently caused by them messing with upstream
9
- # replies for advertising purposes.
10
- #########################################################################
11
- require 'test-unit'
12
- require 'net/ping/external'
13
-
14
- class TC_Net_Ping_External < Test::Unit::TestCase
15
- def setup
16
- @host = 'localhost'
17
- @bogus = 'foo.bar.baz'
18
- @pe = Net::Ping::External.new(@host)
19
- @bad = Net::Ping::External.new(@bogus)
20
- end
21
-
22
- test "ping basic functionality" do
23
- assert_respond_to(@pe, :ping)
24
- end
25
-
26
- test "ping with no arguments" do
27
- assert_nothing_raised{ @pe.ping }
28
- end
29
-
30
- test "ping accepts a hostname" do
31
- assert_nothing_raised{ @pe.ping(@host) }
32
- end
33
-
34
- test "ping returns a boolean" do
35
- assert_boolean(@pe.ping)
36
- assert_boolean(@bad.ping)
37
- end
38
-
39
- test "ping? alias" do
40
- assert_respond_to(@pe, :ping?)
41
- assert_alias_method(@pe, :ping?, :ping)
42
- end
43
-
44
- test "pingecho alias" do
45
- assert_nothing_raised{ @pe.pingecho }
46
- assert_alias_method(@pe, :pingecho, :ping)
47
- end
48
-
49
- test "pinging a good host returns true" do
50
- assert_true(@pe.ping?)
51
- end
52
-
53
- test "pinging a bogus host returns false" do
54
- assert_false(@bad.ping?)
55
- end
56
-
57
- test "duration basic functionality" do
58
- assert_nothing_raised{ @pe.ping }
59
- assert_respond_to(@pe, :duration)
60
- assert_kind_of(Float, @pe.duration)
61
- end
62
-
63
- test "duration is unset if a bad ping follows a good ping" do
64
- assert_nothing_raised{ @pe.ping }
65
- assert_not_nil(@pe.duration)
66
- assert_false(@pe.ping?(@bogus))
67
- assert_nil(@pe.duration)
68
- end
69
-
70
- test "host getter basic functionality" do
71
- assert_respond_to(@pe, :host)
72
- assert_equal('localhost', @pe.host)
73
- end
74
-
75
- test "host setter basic functionality" do
76
- assert_respond_to(@pe, :host=)
77
- assert_nothing_raised{ @pe.host = @bad }
78
- assert_equal(@bad, @pe.host)
79
- end
80
-
81
- test "port getter basic functionality" do
82
- assert_respond_to(@pe, :port)
83
- assert_equal(7, @pe.port)
84
- end
85
-
86
- test "port setter basic functionality" do
87
- assert_respond_to(@pe, :port=)
88
- assert_nothing_raised{ @pe.port = 90 }
89
- assert_equal(90, @pe.port)
90
- end
91
-
92
- test "timeout getter basic functionality" do
93
- assert_respond_to(@pe, :timeout)
94
- assert_equal(5, @pe.timeout)
95
- end
96
-
97
- test "timeout setter basic functionality" do
98
- assert_respond_to(@pe, :timeout=)
99
- assert_nothing_raised{ @pe.timeout = 30 }
100
- assert_equal(30, @pe.timeout)
101
- end
102
-
103
- test "exception method basic functionality" do
104
- assert_respond_to(@pe, :exception)
105
- assert_nil(@pe.exception)
106
- end
107
-
108
- test "pinging a bogus host stores exception data" do
109
- assert_nothing_raised{ @bad.ping? }
110
- assert_not_nil(@bad.exception)
111
- end
112
-
113
- test "pinging a good host results in no exception data" do
114
- assert_nothing_raised{ @pe.ping }
115
- assert_nil(@pe.exception)
116
- end
117
-
118
- test "warning basic functionality" do
119
- assert_respond_to(@pe, :warning)
120
- assert_nil(@pe.warning)
121
- end
122
-
123
- test "timing out causes expected result" do
124
- ext = Net::Ping::External.new('foo.bar.baz', nil, 1)
125
- start = Time.now
126
- assert_false(ext.ping?)
127
- elapsed = Time.now - start
128
- assert_true(elapsed < 2.5, "Actual elapsed: #{elapsed}")
129
- assert_not_nil(ext.exception)
130
- end
131
-
132
- test "pinging an unreachable host on the same subnet returns false" do
133
- @bad = Net::Ping::External.new('192.168.0.99')
134
- assert_false(@bad.ping?)
135
- end
136
-
137
- def teardown
138
- @host = nil
139
- @bogus = nil
140
- @pe = nil
141
- @bad = nil
142
- end
143
- end
1
+ #########################################################################
2
+ # test_net_ping_external.rb
3
+ #
4
+ # Test case for the Net::PingExternal class. Run this via the 'test' or
5
+ # 'test:external' rake task.
6
+ #
7
+ # WARNING: I've noticed that test failures will occur if you're using
8
+ # OpenDNS. This is apparently caused by them messing with upstream
9
+ # replies for advertising purposes.
10
+ #########################################################################
11
+ require 'test-unit'
12
+ require 'net/ping/external'
13
+
14
+ class TC_Net_Ping_External < Test::Unit::TestCase
15
+ def setup
16
+ @host = 'localhost'
17
+ @bogus = 'foo.bar.baz'
18
+ @pe = Net::Ping::External.new(@host)
19
+ @bad = Net::Ping::External.new(@bogus)
20
+ end
21
+
22
+ test "ping basic functionality" do
23
+ assert_respond_to(@pe, :ping)
24
+ end
25
+
26
+ test "ping with no arguments" do
27
+ assert_nothing_raised{ @pe.ping }
28
+ end
29
+
30
+ test "ping accepts a hostname" do
31
+ assert_nothing_raised{ @pe.ping(@host) }
32
+ end
33
+
34
+ test "ping returns a boolean" do
35
+ assert_boolean(@pe.ping)
36
+ assert_boolean(@bad.ping)
37
+ end
38
+
39
+ test "ping? alias" do
40
+ assert_respond_to(@pe, :ping?)
41
+ assert_alias_method(@pe, :ping?, :ping)
42
+ end
43
+
44
+ test "pingecho alias" do
45
+ assert_nothing_raised{ @pe.pingecho }
46
+ assert_alias_method(@pe, :pingecho, :ping)
47
+ end
48
+
49
+ test "pinging a good host returns true" do
50
+ assert_true(@pe.ping?)
51
+ end
52
+
53
+ test "pinging a bogus host returns false" do
54
+ assert_false(@bad.ping?)
55
+ end
56
+
57
+ test "duration basic functionality" do
58
+ assert_nothing_raised{ @pe.ping }
59
+ assert_respond_to(@pe, :duration)
60
+ assert_kind_of(Float, @pe.duration)
61
+ end
62
+
63
+ test "duration is unset if a bad ping follows a good ping" do
64
+ assert_nothing_raised{ @pe.ping }
65
+ assert_not_nil(@pe.duration)
66
+ assert_false(@pe.ping?(@bogus))
67
+ assert_nil(@pe.duration)
68
+ end
69
+
70
+ test "host getter basic functionality" do
71
+ assert_respond_to(@pe, :host)
72
+ assert_equal('localhost', @pe.host)
73
+ end
74
+
75
+ test "host setter basic functionality" do
76
+ assert_respond_to(@pe, :host=)
77
+ assert_nothing_raised{ @pe.host = @bad }
78
+ assert_equal(@bad, @pe.host)
79
+ end
80
+
81
+ test "port getter basic functionality" do
82
+ assert_respond_to(@pe, :port)
83
+ assert_equal(7, @pe.port)
84
+ end
85
+
86
+ test "port setter basic functionality" do
87
+ assert_respond_to(@pe, :port=)
88
+ assert_nothing_raised{ @pe.port = 90 }
89
+ assert_equal(90, @pe.port)
90
+ end
91
+
92
+ test "timeout getter basic functionality" do
93
+ assert_respond_to(@pe, :timeout)
94
+ assert_equal(5, @pe.timeout)
95
+ end
96
+
97
+ test "timeout setter basic functionality" do
98
+ assert_respond_to(@pe, :timeout=)
99
+ assert_nothing_raised{ @pe.timeout = 30 }
100
+ assert_equal(30, @pe.timeout)
101
+ end
102
+
103
+ test "exception method basic functionality" do
104
+ assert_respond_to(@pe, :exception)
105
+ assert_nil(@pe.exception)
106
+ end
107
+
108
+ test "pinging a bogus host stores exception data" do
109
+ assert_nothing_raised{ @bad.ping? }
110
+ assert_not_nil(@bad.exception)
111
+ end
112
+
113
+ test "pinging a good host results in no exception data" do
114
+ assert_nothing_raised{ @pe.ping }
115
+ assert_nil(@pe.exception)
116
+ end
117
+
118
+ test "warning basic functionality" do
119
+ assert_respond_to(@pe, :warning)
120
+ assert_nil(@pe.warning)
121
+ end
122
+
123
+ test "timing out causes expected result" do
124
+ ext = Net::Ping::External.new('foo.bar.baz', nil, 1)
125
+ start = Time.now
126
+ assert_false(ext.ping?)
127
+ elapsed = Time.now - start
128
+ assert_true(elapsed < 2.5, "Actual elapsed: #{elapsed}")
129
+ assert_not_nil(ext.exception)
130
+ end
131
+
132
+ test "pinging an unreachable host on the same subnet returns false" do
133
+ @bad = Net::Ping::External.new('192.168.0.99')
134
+ assert_false(@bad.ping?)
135
+ end
136
+
137
+ def teardown
138
+ @host = nil
139
+ @bogus = nil
140
+ @pe = nil
141
+ @bad = nil
142
+ end
143
+ end
@@ -1,240 +1,240 @@
1
- #################################################################################
2
- # test_net_ping_http.rb
3
- #
4
- # Test case for the Net::PingHTTP class. This should be run via the 'test' or
5
- # 'test:http' Rake task.
6
- #################################################################################
7
- require 'test-unit'
8
- require 'fakeweb'
9
- require 'net/ping/http'
10
-
11
- class TC_Net_Ping_HTTP < Test::Unit::TestCase
12
- def setup
13
- ENV['http_proxy'] = ENV['https_proxy'] = ENV['no_proxy'] = nil
14
- @uri = 'http://www.google.com/index.html'
15
- @uri_https = 'https://encrypted.google.com'
16
- @proxy = 'http://username:password@proxymoxie:3128'
17
- FakeWeb.allow_net_connect = false
18
-
19
- FakeWeb.register_uri(:get, @uri, :body => "PONG")
20
- FakeWeb.register_uri(:head, @uri, :body => "PONG")
21
- FakeWeb.register_uri(:head, @uri_https, :body => "PONG")
22
- FakeWeb.register_uri(:get, @uri_https, :body => "PONG")
23
- FakeWeb.register_uri(:head, "http://jigsaw.w3.org/HTTP/300/302.html",
24
- :body => "PONG",
25
- :location => "#{@uri}",
26
- :status => ["302", "Found"])
27
-
28
- FakeWeb.register_uri(:any, 'http://www.blabfoobarurghxxxx.com', :exception => SocketError)
29
- FakeWeb.register_uri(:head, 'http://http502.com',
30
- :body => "",
31
- :status => ["502", "Bad Gateway"])
32
-
33
- @http = Net::Ping::HTTP.new(@uri, 80, 30)
34
- @bad = Net::Ping::HTTP.new('http://www.blabfoobarurghxxxx.com') # One hopes not
35
- end
36
-
37
- test 'ping basic functionality' do
38
- assert_respond_to(@http, :ping)
39
- assert_nothing_raised{ @http.ping }
40
- end
41
-
42
- test 'ping returns a boolean value' do
43
- assert_boolean(@http.ping?)
44
- assert_boolean(@bad.ping?)
45
- end
46
-
47
- test 'ping? is an alias for ping' do
48
- assert_alias_method(@http, :ping?, :ping)
49
- end
50
-
51
- test 'pingecho is an alias for ping' do
52
- assert_alias_method(@http, :pingecho, :ping)
53
- end
54
-
55
- test 'ping should succeed for a valid website' do
56
- assert_true(@http.ping?)
57
- end
58
-
59
- test 'ping should fail for an invalid website' do
60
- assert_false(@bad.ping?)
61
- end
62
-
63
- test 'duration basic functionality' do
64
- assert_respond_to(@http, :duration)
65
- assert_nothing_raised{ @http.ping }
66
- end
67
-
68
- test 'duration returns a float value on a successful ping' do
69
- assert_true(@http.ping)
70
- assert_kind_of(Float, @http.duration)
71
- end
72
-
73
- test 'duration is nil on an unsuccessful ping' do
74
- assert_false(@bad.ping)
75
- assert_nil(@http.duration)
76
- end
77
-
78
- test 'host attribute basic functionality' do
79
- assert_respond_to(@http, :host)
80
- assert_respond_to(@http, :host=)
81
- assert_equal('http://www.google.com/index.html', @http.host)
82
- end
83
-
84
- test 'uri is an alias for host' do
85
- assert_alias_method(@http, :uri, :host)
86
- assert_alias_method(@http, :uri=, :host=)
87
- end
88
-
89
- test 'port attribute basic functionality' do
90
- assert_respond_to(@http, :port)
91
- assert_respond_to(@http, :port=)
92
- end
93
-
94
- test 'port attribute is set to expected value' do
95
- assert_equal(80, @http.port)
96
- assert_equal(443, Net::Ping::HTTP.new('https://github.com/path').port)
97
- assert_equal(80, Net::Ping::HTTP.new.port)
98
- end
99
-
100
- test 'timeout attribute basic functionality' do
101
- assert_respond_to(@http, :timeout)
102
- assert_respond_to(@http, :timeout=)
103
- end
104
-
105
- test 'timeout attribute expected values' do
106
- assert_equal(30, @http.timeout)
107
- assert_equal(5, @bad.timeout)
108
- end
109
-
110
- # TODO: Figure out how to do this with FakeWeb.
111
- test 'ping fails if timeout exceeded' do
112
- FakeWeb.allow_net_connect = true
113
- @http = Net::Ping::HTTP.new('https://github.com/path', 80, 0.01)
114
- assert_false(@http.ping?)
115
- assert_equal('execution expired', @http.exception)
116
- end
117
-
118
- test 'exception attribute basic functionality' do
119
- assert_respond_to(@http, :exception)
120
- assert_nil(@http.exception)
121
- end
122
-
123
- test 'exception attribute is nil if the ping is successful' do
124
- assert_true(@http.ping)
125
- assert_nil(@http.exception)
126
- end
127
-
128
- test 'exception attribute is not nil if the ping is unsuccessful' do
129
- assert_false(@bad.ping)
130
- assert_not_nil(@bad.exception)
131
- end
132
-
133
- test 'warning attribute basic functionality' do
134
- assert_respond_to(@http, :warning)
135
- assert_nil(@http.warning)
136
- end
137
-
138
- test 'code attribute is set' do
139
- assert_true(@http.ping)
140
- assert_equal('200', @http.code)
141
- end
142
-
143
- test 'user_agent accessor is defined' do
144
- assert_respond_to(@http, :user_agent)
145
- assert_respond_to(@http, :user_agent=)
146
- end
147
-
148
- test 'user_agent defaults to nil' do
149
- assert_nil(@http.user_agent)
150
- end
151
-
152
- test 'ping with user agent' do
153
- @http.user_agent = "KDDI-CA32"
154
- assert_true(@http.ping)
155
- end
156
-
157
- test 'redirect_limit accessor is defined' do
158
- assert_respond_to(@http, :redirect_limit)
159
- assert_respond_to(@http, :redirect_limit=)
160
- end
161
-
162
- test 'redirect_limit defaults to 5' do
163
- assert_equal(5, @http.redirect_limit)
164
- end
165
-
166
- test 'redirects succeed by default' do
167
- @http = Net::Ping::HTTP.new("http://jigsaw.w3.org/HTTP/300/302.html")
168
- assert_true(@http.ping)
169
- end
170
-
171
- test 'redirect fail if follow_redirect is set to false' do
172
- @http = Net::Ping::HTTP.new("http://jigsaw.w3.org/HTTP/300/302.html")
173
- @http.follow_redirect = false
174
- assert_false(@http.ping)
175
- end
176
-
177
- test 'ping with redirect limit set to zero fails' do
178
- @http = Net::Ping::HTTP.new("http://jigsaw.w3.org/HTTP/300/302.html")
179
- @http.redirect_limit = 0
180
- assert_false(@http.ping)
181
- assert_equal("Redirect limit exceeded", @http.exception)
182
- end
183
-
184
- test 'http 502 sets exception' do
185
- @http = Net::Ping::HTTP.new("http://http502.com")
186
- assert_false(@http.ping)
187
- assert_equal('Bad Gateway', @http.exception)
188
- end
189
-
190
- test 'http 502 sets code' do
191
- @http = Net::Ping::HTTP.new("http://http502.com")
192
- assert_false(@http.ping)
193
- assert_equal('502', @http.code)
194
- end
195
-
196
- test 'ping against https site defaults to port 443' do
197
- @http = Net::Ping::HTTP.new(@uri_https)
198
- assert_equal(443, @http.port)
199
- end
200
-
201
- test 'ping against https site works as expected' do
202
- @http = Net::Ping::HTTP.new(@uri_https)
203
- assert_true(@http.ping)
204
- end
205
-
206
- test 'ping with get option' do
207
- @http = Net::Ping::HTTP.new(@uri)
208
- @http.get_request = true
209
- assert_true(@http.ping)
210
- end
211
-
212
- test 'ping with http proxy' do
213
- ENV['http_proxy'] = "http://proxymoxie:3128"
214
- @http = Net::Ping::HTTP.new(@uri)
215
- @http.get_request = true
216
- assert_true(@http.ping)
217
- assert_true(@http.proxied)
218
- end
219
-
220
- test 'ping with https proxy' do
221
- ENV['https_proxy'] = "http://proxymoxie:3128"
222
- @http = Net::Ping::HTTP.new(@uri_https)
223
- @http.get_request = true
224
- assert_true(@http.ping)
225
- assert_true(@http.proxied)
226
- end
227
-
228
- test 'ping with no_proxy' do
229
- ENV['no_proxy'] = "google.com"
230
- @http = Net::Ping::HTTP.new(@uri)
231
- @http.get_request = true
232
- assert_true(@http.ping)
233
- assert_false(@http.proxied)
234
- end
235
-
236
- def teardown
237
- @uri = nil
238
- @http = nil
239
- end
240
- end
1
+ #################################################################################
2
+ # test_net_ping_http.rb
3
+ #
4
+ # Test case for the Net::PingHTTP class. This should be run via the 'test' or
5
+ # 'test:http' Rake task.
6
+ #################################################################################
7
+ require 'test-unit'
8
+ require 'fakeweb'
9
+ require 'net/ping/http'
10
+
11
+ class TC_Net_Ping_HTTP < Test::Unit::TestCase
12
+ def setup
13
+ ENV['http_proxy'] = ENV['https_proxy'] = ENV['no_proxy'] = nil
14
+ @uri = 'http://www.google.com/index.html'
15
+ @uri_https = 'https://encrypted.google.com'
16
+ @proxy = 'http://username:password@proxymoxie:3128'
17
+ FakeWeb.allow_net_connect = false
18
+
19
+ FakeWeb.register_uri(:get, @uri, :body => "PONG")
20
+ FakeWeb.register_uri(:head, @uri, :body => "PONG")
21
+ FakeWeb.register_uri(:head, @uri_https, :body => "PONG")
22
+ FakeWeb.register_uri(:get, @uri_https, :body => "PONG")
23
+ FakeWeb.register_uri(:head, "http://jigsaw.w3.org/HTTP/300/302.html",
24
+ :body => "PONG",
25
+ :location => "#{@uri}",
26
+ :status => ["302", "Found"])
27
+
28
+ FakeWeb.register_uri(:any, 'http://www.blabfoobarurghxxxx.com', :exception => SocketError)
29
+ FakeWeb.register_uri(:head, 'http://http502.com',
30
+ :body => "",
31
+ :status => ["502", "Bad Gateway"])
32
+
33
+ @http = Net::Ping::HTTP.new(@uri, 80, 30)
34
+ @bad = Net::Ping::HTTP.new('http://www.blabfoobarurghxxxx.com') # One hopes not
35
+ end
36
+
37
+ test 'ping basic functionality' do
38
+ assert_respond_to(@http, :ping)
39
+ assert_nothing_raised{ @http.ping }
40
+ end
41
+
42
+ test 'ping returns a boolean value' do
43
+ assert_boolean(@http.ping?)
44
+ assert_boolean(@bad.ping?)
45
+ end
46
+
47
+ test 'ping? is an alias for ping' do
48
+ assert_alias_method(@http, :ping?, :ping)
49
+ end
50
+
51
+ test 'pingecho is an alias for ping' do
52
+ assert_alias_method(@http, :pingecho, :ping)
53
+ end
54
+
55
+ test 'ping should succeed for a valid website' do
56
+ assert_true(@http.ping?)
57
+ end
58
+
59
+ test 'ping should fail for an invalid website' do
60
+ assert_false(@bad.ping?)
61
+ end
62
+
63
+ test 'duration basic functionality' do
64
+ assert_respond_to(@http, :duration)
65
+ assert_nothing_raised{ @http.ping }
66
+ end
67
+
68
+ test 'duration returns a float value on a successful ping' do
69
+ assert_true(@http.ping)
70
+ assert_kind_of(Float, @http.duration)
71
+ end
72
+
73
+ test 'duration is nil on an unsuccessful ping' do
74
+ assert_false(@bad.ping)
75
+ assert_nil(@http.duration)
76
+ end
77
+
78
+ test 'host attribute basic functionality' do
79
+ assert_respond_to(@http, :host)
80
+ assert_respond_to(@http, :host=)
81
+ assert_equal('http://www.google.com/index.html', @http.host)
82
+ end
83
+
84
+ test 'uri is an alias for host' do
85
+ assert_alias_method(@http, :uri, :host)
86
+ assert_alias_method(@http, :uri=, :host=)
87
+ end
88
+
89
+ test 'port attribute basic functionality' do
90
+ assert_respond_to(@http, :port)
91
+ assert_respond_to(@http, :port=)
92
+ end
93
+
94
+ test 'port attribute is set to expected value' do
95
+ assert_equal(80, @http.port)
96
+ assert_equal(443, Net::Ping::HTTP.new('https://github.com/path').port)
97
+ assert_equal(80, Net::Ping::HTTP.new.port)
98
+ end
99
+
100
+ test 'timeout attribute basic functionality' do
101
+ assert_respond_to(@http, :timeout)
102
+ assert_respond_to(@http, :timeout=)
103
+ end
104
+
105
+ test 'timeout attribute expected values' do
106
+ assert_equal(30, @http.timeout)
107
+ assert_equal(5, @bad.timeout)
108
+ end
109
+
110
+ # TODO: Figure out how to do this with FakeWeb.
111
+ test 'ping fails if timeout exceeded' do
112
+ FakeWeb.allow_net_connect = true
113
+ @http = Net::Ping::HTTP.new('https://github.com/path', 80, 0.01)
114
+ assert_false(@http.ping?)
115
+ assert_equal('execution expired', @http.exception)
116
+ end
117
+
118
+ test 'exception attribute basic functionality' do
119
+ assert_respond_to(@http, :exception)
120
+ assert_nil(@http.exception)
121
+ end
122
+
123
+ test 'exception attribute is nil if the ping is successful' do
124
+ assert_true(@http.ping)
125
+ assert_nil(@http.exception)
126
+ end
127
+
128
+ test 'exception attribute is not nil if the ping is unsuccessful' do
129
+ assert_false(@bad.ping)
130
+ assert_not_nil(@bad.exception)
131
+ end
132
+
133
+ test 'warning attribute basic functionality' do
134
+ assert_respond_to(@http, :warning)
135
+ assert_nil(@http.warning)
136
+ end
137
+
138
+ test 'code attribute is set' do
139
+ assert_true(@http.ping)
140
+ assert_equal('200', @http.code)
141
+ end
142
+
143
+ test 'user_agent accessor is defined' do
144
+ assert_respond_to(@http, :user_agent)
145
+ assert_respond_to(@http, :user_agent=)
146
+ end
147
+
148
+ test 'user_agent defaults to nil' do
149
+ assert_nil(@http.user_agent)
150
+ end
151
+
152
+ test 'ping with user agent' do
153
+ @http.user_agent = "KDDI-CA32"
154
+ assert_true(@http.ping)
155
+ end
156
+
157
+ test 'redirect_limit accessor is defined' do
158
+ assert_respond_to(@http, :redirect_limit)
159
+ assert_respond_to(@http, :redirect_limit=)
160
+ end
161
+
162
+ test 'redirect_limit defaults to 5' do
163
+ assert_equal(5, @http.redirect_limit)
164
+ end
165
+
166
+ test 'redirects succeed by default' do
167
+ @http = Net::Ping::HTTP.new("http://jigsaw.w3.org/HTTP/300/302.html")
168
+ assert_true(@http.ping)
169
+ end
170
+
171
+ test 'redirect fail if follow_redirect is set to false' do
172
+ @http = Net::Ping::HTTP.new("http://jigsaw.w3.org/HTTP/300/302.html")
173
+ @http.follow_redirect = false
174
+ assert_false(@http.ping)
175
+ end
176
+
177
+ test 'ping with redirect limit set to zero fails' do
178
+ @http = Net::Ping::HTTP.new("http://jigsaw.w3.org/HTTP/300/302.html")
179
+ @http.redirect_limit = 0
180
+ assert_false(@http.ping)
181
+ assert_equal("Redirect limit exceeded", @http.exception)
182
+ end
183
+
184
+ test 'http 502 sets exception' do
185
+ @http = Net::Ping::HTTP.new("http://http502.com")
186
+ assert_false(@http.ping)
187
+ assert_equal('Bad Gateway', @http.exception)
188
+ end
189
+
190
+ test 'http 502 sets code' do
191
+ @http = Net::Ping::HTTP.new("http://http502.com")
192
+ assert_false(@http.ping)
193
+ assert_equal('502', @http.code)
194
+ end
195
+
196
+ test 'ping against https site defaults to port 443' do
197
+ @http = Net::Ping::HTTP.new(@uri_https)
198
+ assert_equal(443, @http.port)
199
+ end
200
+
201
+ test 'ping against https site works as expected' do
202
+ @http = Net::Ping::HTTP.new(@uri_https)
203
+ assert_true(@http.ping)
204
+ end
205
+
206
+ test 'ping with get option' do
207
+ @http = Net::Ping::HTTP.new(@uri)
208
+ @http.get_request = true
209
+ assert_true(@http.ping)
210
+ end
211
+
212
+ test 'ping with http proxy' do
213
+ ENV['http_proxy'] = "http://proxymoxie:3128"
214
+ @http = Net::Ping::HTTP.new(@uri)
215
+ @http.get_request = true
216
+ assert_true(@http.ping)
217
+ assert_true(@http.proxied)
218
+ end
219
+
220
+ test 'ping with https proxy' do
221
+ ENV['https_proxy'] = "http://proxymoxie:3128"
222
+ @http = Net::Ping::HTTP.new(@uri_https)
223
+ @http.get_request = true
224
+ assert_true(@http.ping)
225
+ assert_true(@http.proxied)
226
+ end
227
+
228
+ test 'ping with no_proxy' do
229
+ ENV['no_proxy'] = "google.com"
230
+ @http = Net::Ping::HTTP.new(@uri)
231
+ @http.get_request = true
232
+ assert_true(@http.ping)
233
+ assert_false(@http.proxied)
234
+ end
235
+
236
+ def teardown
237
+ @uri = nil
238
+ @http = nil
239
+ end
240
+ end