Permalink
Browse files

Fallback to GET on all errors.

Some sites don't respond to HEAD requests at all, which gives false negatives.
  • Loading branch information...
1 parent 42eae4a commit 3374719d0c5c760ec063d4a233f2c422ea94f0b4 @georgebrock georgebrock committed Jan 11, 2013
Showing with 26 additions and 3 deletions.
  1. +11 −3 lib/helpers/uri_validator.rb
  2. +15 −0 spec/uri_validator_spec.rb
View
14 lib/helpers/uri_validator.rb
@@ -22,10 +22,18 @@ def uris
end
def valid_uri?(uri)
+ head_request_ok?(uri) || get_request_ok?(uri)
+ end
+
+ def head_request_ok?(uri)
response = HTTParty.head(uri)
- if response.method_not_allowed?
- response = HTTParty.get(uri)
- end
+ return response.ok?
+ rescue SocketError
+ return false
+ end
+
+ def get_request_ok?(uri)
+ response = HTTParty.get(uri)
return response.ok?
rescue SocketError
return false
View
15 spec/uri_validator_spec.rb
@@ -18,7 +18,9 @@
it 'outputs an error for each invalid URI' do
stub_request(:head, 'http://bad.example.org').to_raise(SocketError)
+ stub_request(:get, 'http://bad.example.org').to_raise(SocketError)
stub_request(:head, 'http://thoughtbot.com/404').to_return(status: 404)
+ stub_request(:get, 'http://thoughtbot.com/404').to_return(status: 404)
file_name = 'path/to/bad_json_file.json'
URIExtractor.stubs(:new).with(file_name).returns %w(
http://bad.example.org
@@ -44,4 +46,17 @@
WebMock.should have_requested(:head, 'http://example.com')
WebMock.should have_requested(:get, 'http://example.com')
end
+
+ it 'retries with a GET request on an exception' do
+ stub_request(:head, 'http://example.com').to_raise(SocketError)
+ stub_request(:get, 'http://example.com')
+ file_name = 'rails.json'
+ URIExtractor.stubs(:new).with(file_name).returns %w(http://example.com)
+ $stdout = io = StringIO.new
+
+ URIValidator.new(file_name).run
+
+ WebMock.should have_requested(:head, 'http://example.com')
+ WebMock.should have_requested(:get, 'http://example.com')
+ end
end

0 comments on commit 3374719

Please sign in to comment.