debian-mirror-gitlab/spec/support/matchers/be_url.rb

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

30 lines
890 B
Ruby
Raw Normal View History

2019-10-12 21:52:04 +05:30
# frozen_string_literal: true
2019-12-04 20:38:33 +05:30
# 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]|
2017-08-17 22:00:37 +05:30
match do |actual|
2019-12-04 20:38:33 +05:30
next false unless actual.present?
uri = URI.parse(actual)
Array.wrap(types).any? { |t| uri.is_a?(t) }
rescue URI::InvalidURIError
false
2017-08-17 22:00:37 +05:30
end
end
2019-10-12 21:52:04 +05:30
# looks better when used like:
# expect(thing).to receive(:method).with(a_valid_url)
RSpec::Matchers.alias_matcher :a_valid_url, :be_url
2019-12-04 20:38:33 +05:30
RSpec::Matchers.alias_matcher :be_http_url, :be_url