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

Revert inversion of pattern check in URIPattern constructor #904

Merged
merged 1 commit into from Sep 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 6 additions & 4 deletions lib/webmock/request_pattern.rb
Expand Up @@ -109,11 +109,13 @@ class URIPattern
include RSpecMatcherDetector

def initialize(pattern)
@pattern = case pattern
when String
WebMock::Util::URI.normalize_uri(pattern)
else
@pattern = if pattern.is_a?(Addressable::URI) \
|| pattern.is_a?(Addressable::Template)
pattern
elsif pattern.respond_to?(:call)
pattern
else
WebMock::Util::URI.normalize_uri(pattern)
end
@query_params = nil
end
Expand Down
47 changes: 44 additions & 3 deletions spec/unit/request_pattern_spec.rb
Expand Up @@ -111,6 +111,11 @@ def match(request_signature)
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
end

it "should match if uri matches requesst uri as URI object" do
expect(WebMock::RequestPattern.new(:get, URI.parse("www.example.com"))).
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
end

it "should match if uri proc pattern returning true" do
expect(WebMock::RequestPattern.new(:get, ->(uri) { true })).
to match(WebMock::RequestSignature.new(:get, "www.example.com"))
Expand Down Expand Up @@ -233,7 +238,7 @@ def match(request_signature)
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
end

it "should match request query params if params don't match" do
it "should not match request query params if params don't match" do
expect(WebMock::RequestPattern.new(:get, /.*example.*/, query: {"x" => ["b", "c"]})).
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
end
Expand Down Expand Up @@ -263,13 +268,49 @@ def match(request_signature)
end
end

describe "when uri is described as URI" do
it "should match request query params" do
expect(WebMock::RequestPattern.new(:get, 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, 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, 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, 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, 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, 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 a proc" do
it "should match request query params" do
expect(WebMock::RequestPattern.new(:get, ->(uri) { true }, query: {"a" => ["b", "c"]})).
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
end

it "should match request query params if params don't match" do
it "should not match request query params if params don't match" do
expect(WebMock::RequestPattern.new(:get, ->(uri) { true }, query: {"x" => ["b", "c"]})).
not_to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
end
Expand Down Expand Up @@ -305,7 +346,7 @@ def match(request_signature)
to match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
end

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