From becfdfda9d5c472de5fd53c23314a9757a042dfb Mon Sep 17 00:00:00 2001 From: Ryan Kerr Date: Sun, 13 Sep 2020 17:08:32 -0400 Subject: [PATCH] Revert inversion of pattern check in URIPattern constructor. Fixes #903. This reinstates `WebMock::Util::URI.normalize_uri` as the default fallback behavior of the constructor, specifically whitelisting callable objects, in addition to the classes that were previously whitelisted, to pass through unchanged. --- lib/webmock/request_pattern.rb | 10 ++++--- spec/unit/request_pattern_spec.rb | 47 +++++++++++++++++++++++++++++-- 2 files changed, 50 insertions(+), 7 deletions(-) diff --git a/lib/webmock/request_pattern.rb b/lib/webmock/request_pattern.rb index b0f187e25..54f88a7fb 100644 --- a/lib/webmock/request_pattern.rb +++ b/lib/webmock/request_pattern.rb @@ -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 diff --git a/spec/unit/request_pattern_spec.rb b/spec/unit/request_pattern_spec.rb index 767ceb5c6..ae2502edc 100644 --- a/spec/unit/request_pattern_spec.rb +++ b/spec/unit/request_pattern_spec.rb @@ -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")) @@ -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 @@ -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 @@ -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