module WebTest::Util
Public Class Methods
connection(follow: false)
click to toggle source
private
# File lib/web_test/util.rb, line 64 def self.connection(follow: false) Faraday.new do |c| c.options[:timeout] = TIMEOUT_IN_SECONDS c.options[:open_timeout] = OPEN_TIMEOUT_IN_SECONDS c.use(FaradayMiddleware::FollowRedirects, limit: 4) if follow c.adapter :net_http end end
error_message(errors)
click to toggle source
# File lib/web_test/util.rb, line 10 def self.error_message(errors) return errors.message if errors.respond_to?(:message) errors .map(&:to_s) .join('; ') .capitalize end
get(url_or_domain_name, follow: false)
click to toggle source
# File lib/web_test/util.rb, line 29 def self.get(url_or_domain_name, follow: false) request(:get, url_or_domain_name, follow: follow) end
head(url_or_domain_name, follow: false)
click to toggle source
# File lib/web_test/util.rb, line 25 def self.head(url_or_domain_name, follow: false) request(:head, url_or_domain_name, follow: follow) end
make_domain_name(url_or_domain_name)
click to toggle source
Return just the domain name portion of a URL if it's simply of the form name.tld
# File lib/web_test/util.rb, line 85 def self.make_domain_name(url_or_domain_name) if %r{^https?://(.+)} =~ url_or_domain_name $1 else url_or_domain_name end end
make_url(url_or_domain_name)
click to toggle source
Ensure that the given string is a URL, making it into one if necessary.
# File lib/web_test/util.rb, line 75 def self.make_url(url_or_domain_name) if %r{^https?://} =~ url_or_domain_name url_or_domain_name else "http://#{url_or_domain_name}" end end
recheck_on_timeout() { || ... }
click to toggle source
# File lib/web_test/util.rb, line 99 def self.recheck_on_timeout yield rescue Faraday::TimeoutError yield end
remove_protocol(domain_name_or_url)
click to toggle source
Normalize the input: remove 'http(s)://' if it's there
# File lib/web_test/util.rb, line 94 def self.remove_protocol(domain_name_or_url) %r{^https?://(?<name>.+)$} =~ domain_name_or_url name || domain_name_or_url end
request(method, url_or_domain_name, follow: false)
click to toggle source
# File lib/web_test/util.rb, line 33 def self.request(method, url_or_domain_name, follow: false) url = make_url(url_or_domain_name) response = recheck_on_timeout { connection(follow: follow).send(method, url) } [response.status, response.headers] end
status(url_or_domain_name, follow: false)
click to toggle source
# File lib/web_test/util.rb, line 19 def self.status(url_or_domain_name, follow: false) code = head(url_or_domain_name, follow: follow)[0] return code if code != 405 get(url_or_domain_name, follow: follow)[0] end
try_ssl_connection(domain_name_or_url)
click to toggle source
# File lib/web_test/util.rb, line 56 def self.try_ssl_connection(domain_name_or_url) url = "https://#{remove_protocol(domain_name_or_url)}" recheck_on_timeout { connection.head(url) } true end
up?(url_or_domain_name)
click to toggle source
@return true if the given page has status 200, and follow a few redirects if necessary.
# File lib/web_test/util.rb, line 41 def self.up?(url_or_domain_name) url = make_url(url_or_domain_name) conn = connection(follow: true) response = recheck_on_timeout { conn.head(url) } response.status == 200 end
valid_cert?(domain_name_or_url)
click to toggle source
# File lib/web_test/util.rb, line 48 def self.valid_cert?(domain_name_or_url) try_ssl_connection(domain_name_or_url) true rescue # Not serving SSL, expired, or incorrect domain name in certificate false end