Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Fallback to GET on all errors.

Some sites don't respond to HEAD requests at all, which gives false negatives.
  • Loading branch information...
commit 3374719d0c5c760ec063d4a233f2c422ea94f0b4 1 parent 42eae4a
George Brocklehurst authored January 11, 2013
14  lib/helpers/uri_validator.rb
@@ -22,10 +22,18 @@ def uris
22 22
   end
23 23
 
24 24
   def valid_uri?(uri)
  25
+    head_request_ok?(uri) || get_request_ok?(uri)
  26
+  end
  27
+
  28
+  def head_request_ok?(uri)
25 29
     response = HTTParty.head(uri)
26  
-    if response.method_not_allowed?
27  
-      response = HTTParty.get(uri)
28  
-    end
  30
+    return response.ok?
  31
+  rescue SocketError
  32
+    return false
  33
+  end
  34
+
  35
+  def get_request_ok?(uri)
  36
+    response = HTTParty.get(uri)
29 37
     return response.ok?
30 38
   rescue SocketError
31 39
     return false
15  spec/uri_validator_spec.rb
@@ -18,7 +18,9 @@
18 18
 
19 19
   it 'outputs an error for each invalid URI' do
20 20
     stub_request(:head, 'http://bad.example.org').to_raise(SocketError)
  21
+    stub_request(:get, 'http://bad.example.org').to_raise(SocketError)
21 22
     stub_request(:head, 'http://thoughtbot.com/404').to_return(status: 404)
  23
+    stub_request(:get, 'http://thoughtbot.com/404').to_return(status: 404)
22 24
     file_name = 'path/to/bad_json_file.json'
23 25
     URIExtractor.stubs(:new).with(file_name).returns %w(
24 26
       http://bad.example.org
@@ -44,4 +46,17 @@
44 46
     WebMock.should have_requested(:head, 'http://example.com')
45 47
     WebMock.should have_requested(:get, 'http://example.com')
46 48
   end
  49
+
  50
+  it 'retries with a GET request on an exception' do
  51
+    stub_request(:head, 'http://example.com').to_raise(SocketError)
  52
+    stub_request(:get, 'http://example.com')
  53
+    file_name = 'rails.json'
  54
+    URIExtractor.stubs(:new).with(file_name).returns %w(http://example.com)
  55
+    $stdout = io = StringIO.new
  56
+
  57
+    URIValidator.new(file_name).run
  58
+
  59
+    WebMock.should have_requested(:head, 'http://example.com')
  60
+    WebMock.should have_requested(:get, 'http://example.com')
  61
+  end
47 62
 end

0 notes on commit 3374719

Please sign in to comment.
Something went wrong with that request. Please try again.