Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Support Addressable::URI in request patterns #1073

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 3 additions & 3 deletions lib/webmock/request_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,10 @@ def create_uri_pattern(uri)
URICallablePattern.new(uri)
elsif uri.is_a?(::URI::Generic)
URIStringPattern.new(uri.to_s)
elsif uri.is_a?(String)
URIStringPattern.new(uri)
elsif uri.respond_to?(:to_str)
URIStringPattern.new(uri.to_str)
else
raise ArgumentError.new("URI should be a String, Regexp, Addressable::Template or a callable object. Got: #{uri.class}")
raise ArgumentError.new("URI should be a String, Regexp, Addressable::Template, a callable object, or respond to #to_str. Got: #{uri.class}")
end
end
end
Expand Down
36 changes: 36 additions & 0 deletions spec/unit/request_pattern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -376,6 +376,42 @@ def match(request_signature)
end
end

describe "when uri is described as Addressable::URI" do
it "should match request query params" do
expect(WebMock::RequestPattern.new(:get, Addressable::URI.parse("www.example.com"), query: {"a" => ["b", "c"]})).
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
end

it "should not match request query params if params don't match" do
expect(WebMock::RequestPattern.new(:get, Addressable::URI.parse("www.example.com"), query: {"x" => ["b", "c"]})).
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
end

it "should match when query params are declared as HashIncluding matcher matching params" do
expect(WebMock::RequestPattern.new(:get, Addressable::URI.parse("www.example.com"),
query: WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
end

it "should not match when query params are declared as HashIncluding matcher not matching params" do
expect(WebMock::RequestPattern.new(:get, Addressable::URI.parse("www.example.com"),
query: WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]}))).
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
end

it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
expect(WebMock::RequestPattern.new(:get, Addressable::URI.parse("www.example.com"),
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]}))).
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
end

it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
expect(WebMock::RequestPattern.new(:get, Addressable::URI.parse("www.example.com"),
query: RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]}))).
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
end
end

describe "when uri is described as string" do
it "should match when query params are the same as declared in hash" do
expect(WebMock::RequestPattern.new(:get, "www.example.com", query: {"a" => ["b", "c"]})).
Expand Down
Loading