Skip to content

Instantly share code, notes, and snippets.

@lukesmith
Created February 17, 2012 17:34
Show Gist options
  • Save lukesmith/1854513 to your computer and use it in GitHub Desktop.
Save lukesmith/1854513 to your computer and use it in GitHub Desktop.
url-tester
require 'csv'
require 'net/http'
require 'rainbow'
require 'uri'
csv = 'mycsv.csv'
@host_port = 'http://localhost:9292'
def make_request(url_path)
url = URI.parse(URI.escape(@host_port + url_path))
found = false
until found
puts " making request to #{url}"
host, port = url.host, url.port if url.host && url.port
req = Net::HTTP::Head.new(url.path)
res = Net::HTTP.start(host, port) {|http| http.request(req) }
res.header['location'] ? url = URI.parse(res.header['location']) : found = true
unless url.host == 'localhost'
found = true
end
end
res
end
CSV.foreach(csv) do |row|
url_path = row[0]
puts "Checking #{@host_port}#{url_path}"
res = make_request(url_path)
acceptable_codes = ['200', '303']
unless acceptable_codes.include?(res.code)
puts " #{res.code} - #{@host_port}#{url_path}".color(:red).bright
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment