2019-07-25 01:27:42 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-08-30 00:43:16 -04:00
|
|
|
# Assert that this value is a valid URL of at least one type.
|
|
|
|
#
|
|
|
|
# By default, this checks that the URL is either a HTTP or HTTPS URI,
|
|
|
|
# but you can check other URI schemes by passing the type, eg:
|
|
|
|
#
|
|
|
|
# ```
|
|
|
|
# expect(value).to be_url(URI::FTP)
|
|
|
|
# ```
|
|
|
|
#
|
|
|
|
# Pass an empty array of types if you want to match any URI scheme (be
|
|
|
|
# aware that this might not do what you think it does! `foo` is a valid
|
|
|
|
# URI, for instance).
|
|
|
|
RSpec::Matchers.define :be_url do |types = [URI::HTTP, URI::HTTPS]|
|
2016-11-16 17:10:27 -05:00
|
|
|
match do |actual|
|
2019-08-30 00:43:16 -04:00
|
|
|
next false unless actual.present?
|
|
|
|
|
|
|
|
uri = URI.parse(actual)
|
|
|
|
Array.wrap(types).any? { |t| uri.is_a?(t) }
|
|
|
|
rescue URI::InvalidURIError
|
|
|
|
false
|
2016-11-16 17:10:27 -05:00
|
|
|
end
|
|
|
|
end
|
2019-08-06 22:42:20 -04:00
|
|
|
|
|
|
|
# looks better when used like:
|
|
|
|
# expect(thing).to receive(:method).with(a_valid_url)
|
|
|
|
RSpec::Matchers.alias_matcher :a_valid_url, :be_url
|
2019-08-30 00:43:16 -04:00
|
|
|
RSpec::Matchers.alias_matcher :be_http_url, :be_url
|