Skip to content

Commit

Permalink
Merge pull request #904 from leboshi/master
Browse files Browse the repository at this point in the history
Revert inversion of pattern check in URIPattern constructor
  • Loading branch information
bblimke committed Sep 14, 2020
2 parents d9c4601 + becfdfd commit c4f4f1f
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 7 deletions.
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

0 comments on commit c4f4f1f

Please sign in to comment.