I think it depends on why you want to "check" for broken links.
Scenario 1) Proper use of URLs contributed by users may use a method similar to this:
def active_link?(url) uri = URI.parse(url) response = nil Net::HTTP.start(uri.host, uri.port) { |http| response = http.head(uri.path.size > 0 ? uri.path : "/") } return response.code == "200" end
Then you can use this in your Rspec tests:
active_link?('http://example.com').should be
Scenario 2) . You just want to make sure all the links on your site work:
If so, you can try using the wget 'Unix command:
wget --spider -r -l 1 --header='User-Agent: Mozilla/5.0' http://example.com 2>&1 | grep -B 2 '404'
With Scenario 2, it will output all 404s to your terminal screen. This is a pretty simple question to put on a rake team; Jason Seir has a wonderful blog entry (http://jasonseifer.com/2010/04/06/rake-tutorial)
samullen
source share