diff options
author | Alex Kalderimis <akalderimis@gitlab.com> | 2019-08-30 07:43:16 +0300 |
---|---|---|
committer | Thong Kuah <tkuah@gitlab.com> | 2019-08-30 07:43:16 +0300 |
commit | e3a91089b75cdadaa66288d0ff68f137893a3a45 (patch) | |
tree | 5b90b14cbb888b193c70cf1f7402937275470506 /spec/support | |
parent | 1a513aaf53d4f7b7c08aded24f1741e5906fba39 (diff) |
Allow be_url to specify the type
This allows the be_url matcher to be more specific. By default, it only
matches HTTP and HTTPS URIs.
Diffstat (limited to 'spec/support')
-rw-r--r-- | spec/support/matchers/be_url.rb | 22 |
1 files changed, 20 insertions, 2 deletions
diff --git a/spec/support/matchers/be_url.rb b/spec/support/matchers/be_url.rb index 69171f53891..388c1b384c7 100644 --- a/spec/support/matchers/be_url.rb +++ b/spec/support/matchers/be_url.rb @@ -1,11 +1,29 @@ # frozen_string_literal: true -RSpec::Matchers.define :be_url do |_| +# 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]| match do |actual| - URI.parse(actual) rescue false + next false unless actual.present? + + uri = URI.parse(actual) + Array.wrap(types).any? { |t| uri.is_a?(t) } + rescue URI::InvalidURIError + false end end # looks better when used like: # expect(thing).to receive(:method).with(a_valid_url) RSpec::Matchers.alias_matcher :a_valid_url, :be_url +RSpec::Matchers.alias_matcher :be_http_url, :be_url |