Browse files

added testing, simplified code

  • Loading branch information...
1 parent 885c339 commit 63f18821a307d68c7958a6999d157d89628663e8 @kevinladenheim committed May 9, 2012
Showing with 35 additions and 12 deletions.
  1. +5 −5 HTTPStatus.rb
  2. +26 −0 TestHTTPStatus.rb
  3. +4 −7 fetcher.rb
View
10 HTTPStatus.rb
@@ -2,12 +2,12 @@
require_relative "fetcher"
-unless ARGV.length == 1
- print "need url\n\n"
- exit
-end
+#unless ARGV.length == 1
+# print "need url\n\n"
+# exit
+#end
-report = Fetchers::HTTP_Status.new(ARGV[0])
+report = Fetchers::HTTPStatus.new(ARGV[0])
status = report.fetch
if status != nil
View
26 TestHTTPStatus.rb
@@ -0,0 +1,26 @@
+require 'test/unit'
+require_relative 'HTTPStatus'
+
+class TestHTTPStatus < Test::Unit::TestCase
+ def test_fetch
+ page = Fetchers::HTTPStatus.new("http://www.google.com")
+ status = page.fetch
+ assert_equal status, {:available=>true}
+
+ page = Fetchers::HTTPStatus.new("http://www.cnn.com/index/")
+ status = page.fetch
+ assert_equal status, {:available=>true}
+
+ page = Fetchers::HTTPStatus.new("http://127.0.0.1")
+ status = page.fetch
+ assert_equal status, nil
+
+ page = Fetchers::HTTPStatus.new("http:thisistrulygarbage")
+ status = page.fetch
+ assert_equal status, nil
+
+ page = Fetchers::HTTPStatus.new("https://msp.f-secure.com/web-test/common/test.html")
+ status = page.fetch
+ assert_equal status, {:available=>true}
+ end
+end
View
11 fetcher.rb
@@ -2,7 +2,7 @@
require "uri"
module Fetchers
- class HTTP_Status
+ class HTTPStatus
TIMEOUT = 30
@@ -14,21 +14,18 @@ def fetch
begin
uri = URI.parse(@url)
+ http = Net::HTTP.new(uri.host, uri.port)
+ http.read_timeout = TIMEOUT
+
if @url[4] == 's'
- http = Net::HTTP.new(uri.host, uri.port)
http.use_ssl = true
# fake cert? no problem!
http.verify_mode = OpenSSL::SSL::VERIFY_NONE
- else
- http = Net::HTTP.new(uri.host, uri.port)
end
-
- http.read_timeout = TIMEOUT
response = http.request(Net::HTTP::Get.new(uri.request_uri))
print "\n\n", response.code, " ", response.message, "\n\n"
-
rescue Timeout::Error
printf "\n\nTimeout error\n\n"
rescue Errno::ECONNREFUSED

0 comments on commit 63f1882

Please sign in to comment.