From 14f969063aee6a282f39da4a5e8fd63823704768 Mon Sep 17 00:00:00 2001 From: Vesa Laakso Date: Wed, 2 Jan 2019 15:33:06 +0200 Subject: [PATCH 1/5] Add test for using Addressable::Template for host URI host part --- spec/unit/request_pattern_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/unit/request_pattern_spec.rb b/spec/unit/request_pattern_spec.rb index b3b5edd3c..f62a0b637 100644 --- a/spec/unit/request_pattern_spec.rb +++ b/spec/unit/request_pattern_spec.rb @@ -132,6 +132,12 @@ def match(request_signature) expect(WebMock::RequestPattern.new(:get, uri)).to match(signature) end + it "should match if Addressable::Template pattern host matches request uri" do + signature = WebMock::RequestSignature.new(:get, "www.example.com") + uri = Addressable::Template.new("{subdomain}.example.com") + expect(WebMock::RequestPattern.new(:get, uri)).to match(signature) + end + it "should match for uris with same parameters as pattern" do expect(WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2")). to match(WebMock::RequestSignature.new(:get, "www.example.com?a=1&b=2")) From 4d81e69680a7f027a70e7c20acb8cc8a1d150662 Mon Sep 17 00:00:00 2001 From: Vesa Laakso Date: Mon, 9 Sep 2019 11:23:09 +0300 Subject: [PATCH 2/5] Avoid re-evaluating URIs This allows using Addressable::Template directly and not crash when a template is used for the URI host part --- lib/webmock/request_pattern.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/webmock/request_pattern.rb b/lib/webmock/request_pattern.rb index 0555200c8..301a9f45e 100644 --- a/lib/webmock/request_pattern.rb +++ b/lib/webmock/request_pattern.rb @@ -181,7 +181,12 @@ def to_s private def matches_with_variations?(uri) - normalized_template = Addressable::Template.new(WebMock::Util::URI.heuristic_parse(@pattern.pattern)) + normalized_template = + if uri.is_a?(Addressable::URI) + Addressable::Template.new(uri) + else + Addressable::Template.new(WebMock::Util::URI.heuristic_parse(@pattern.pattern)) + end WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| normalized_template.match(u) } end From b846f551d8be62d240db2e4091044ba671de047b Mon Sep 17 00:00:00 2001 From: Vesa Laakso Date: Mon, 9 Sep 2019 11:32:55 +0300 Subject: [PATCH 3/5] Simplify matching to not do heuristic parsing on pattern --- lib/webmock/request_pattern.rb | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) diff --git a/lib/webmock/request_pattern.rb b/lib/webmock/request_pattern.rb index 301a9f45e..393684d59 100644 --- a/lib/webmock/request_pattern.rb +++ b/lib/webmock/request_pattern.rb @@ -181,14 +181,8 @@ def to_s private def matches_with_variations?(uri) - normalized_template = - if uri.is_a?(Addressable::URI) - Addressable::Template.new(uri) - else - Addressable::Template.new(WebMock::Util::URI.heuristic_parse(@pattern.pattern)) - end - - WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| normalized_template.match(u) } + template = Addressable::Template.new(uri) + WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| template.match(u) } end end From 70f4a2453e74ef725b5b37b7b3a5f1bddefd9ade Mon Sep 17 00:00:00 2001 From: Vesa Laakso <482561+valscion@users.noreply.github.com> Date: Mon, 9 Mar 2020 12:10:47 +0200 Subject: [PATCH 4/5] Add negative match assertion spec --- spec/unit/request_pattern_spec.rb | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/spec/unit/request_pattern_spec.rb b/spec/unit/request_pattern_spec.rb index f62a0b637..0e59664ef 100644 --- a/spec/unit/request_pattern_spec.rb +++ b/spec/unit/request_pattern_spec.rb @@ -138,6 +138,12 @@ def match(request_signature) expect(WebMock::RequestPattern.new(:get, uri)).to match(signature) end + it "should not match if Addressable::Template pattern host does not match request uri" do + signature = WebMock::RequestSignature.new(:get, "www.bad-example.com") + uri = Addressable::Template.new("{subdomain}.example.com") + expect(WebMock::RequestPattern.new(:get, uri)).not_to match(signature) + end + it "should match for uris with same parameters as pattern" do expect(WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2")). to match(WebMock::RequestSignature.new(:get, "www.example.com?a=1&b=2")) From 77e8543b334d9b0fa09597c6de8ba3b4e9feee0d Mon Sep 17 00:00:00 2001 From: Vesa Laakso <482561+valscion@users.noreply.github.com> Date: Mon, 9 Mar 2020 12:47:35 +0200 Subject: [PATCH 5/5] Fix matching when URI can't be normalized --- lib/webmock/request_pattern.rb | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/lib/webmock/request_pattern.rb b/lib/webmock/request_pattern.rb index 393684d59..4dd0e46e3 100644 --- a/lib/webmock/request_pattern.rb +++ b/lib/webmock/request_pattern.rb @@ -181,7 +181,12 @@ def to_s private def matches_with_variations?(uri) - template = Addressable::Template.new(uri) + template = + begin + Addressable::Template.new(WebMock::Util::URI.heuristic_parse(@pattern.pattern)) + rescue Addressable::URI::InvalidURIError + Addressable::Template.new(@pattern.pattern) + end WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| template.match(u) } end end