Module: RSpec::WebserviceMatchers::Util
- Defined in:
- lib/rspec/webservice_matchers/util.rb
Class Method Summary collapse
- .connection(follow: false) ⇒ Object
- .error_message(errors) ⇒ Object
- .head(url_or_domain_name, follow: false) ⇒ Object
-
.make_url(url_or_domain_name) ⇒ Object
Ensure that the given string is a URL, making it into one if necessary.
- .recheck_on_timeout ⇒ Object
-
.remove_protocol(domain_name_or_url) ⇒ Object
Normalize the input: remove ‘http(s)://’ if it’s there.
- .status(url_or_domain_name, follow: false) ⇒ Object
- .try_ssl_connection(domain_name_or_url) ⇒ Object
-
.up?(url_or_domain_name) ⇒ Boolean
and follow a few redirects if necessary.
- .valid_cert?(domain_name_or_url) ⇒ Boolean
Class Method Details
.connection(follow: false) ⇒ Object
56 57 58 59 60 61 62 63 |
# File 'lib/rspec/webservice_matchers/util.rb', line 56 def self.connection(follow: false) Faraday.new do |c| c.[:timeout] = TIMEOUT_IN_SECONDS c.[:open_timeout] = OPEN_TIMEOUT_IN_SECONDS c.use(FaradayMiddleware::FollowRedirects, limit: 4) if follow c.adapter :excon end end |
.error_message(errors) ⇒ Object
12 13 14 15 16 17 18 19 |
# File 'lib/rspec/webservice_matchers/util.rb', line 12 def self.(errors) return errors. if errors.respond_to?(:message) errors .map(&:to_s) .join('; ') .capitalize end |
.head(url_or_domain_name, follow: false) ⇒ Object
25 26 27 28 29 |
# File 'lib/rspec/webservice_matchers/util.rb', line 25 def self.head(url_or_domain_name, follow: false) url = make_url(url_or_domain_name) response = recheck_on_timeout { connection(follow: follow).head(url) } [response.status, response.headers] end |
.make_url(url_or_domain_name) ⇒ Object
Ensure that the given string is a URL, making it into one if necessary.
67 68 69 70 71 72 73 |
# File 'lib/rspec/webservice_matchers/util.rb', line 67 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 ⇒ Object
81 82 83 84 85 |
# File 'lib/rspec/webservice_matchers/util.rb', line 81 def self.recheck_on_timeout yield rescue Faraday::Error::TimeoutError yield end |
.remove_protocol(domain_name_or_url) ⇒ Object
Normalize the input: remove ‘http(s)://’ if it’s there
76 77 78 79 |
# File 'lib/rspec/webservice_matchers/util.rb', line 76 def self.remove_protocol(domain_name_or_url) %r{^https?://(?<name>.+)$} =~ domain_name_or_url name || domain_name_or_url end |
.status(url_or_domain_name, follow: false) ⇒ Object
21 22 23 |
# File 'lib/rspec/webservice_matchers/util.rb', line 21 def self.status(url_or_domain_name, follow: false) head(url_or_domain_name, follow: follow)[0] end |
.try_ssl_connection(domain_name_or_url) ⇒ Object
48 49 50 51 52 |
# File 'lib/rspec/webservice_matchers/util.rb', line 48 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) ⇒ Boolean
and follow a few redirects if necessary.
33 34 35 36 37 38 |
# File 'lib/rspec/webservice_matchers/util.rb', line 33 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) ⇒ Boolean
40 41 42 43 44 45 46 |
# File 'lib/rspec/webservice_matchers/util.rb', line 40 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 |