From 2bf4453f6a26bd9300c17e611cf54d24eb65a956 Mon Sep 17 00:00:00 2001 From: Kazato Sugimoto Date: Wed, 16 May 2018 16:22:36 +0900 Subject: [PATCH] Fix InvalidURIError with `ip:port` uri --- lib/webmock/request_pattern.rb | 15 +++++++++++++-- spec/unit/request_pattern_spec.rb | 6 ++++++ 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/lib/webmock/request_pattern.rb b/lib/webmock/request_pattern.rb index c7c4aa6d7..0109c21b1 100644 --- a/lib/webmock/request_pattern.rb +++ b/lib/webmock/request_pattern.rb @@ -155,10 +155,10 @@ class URIAddressablePattern < URIPattern def matches?(uri) if @query_params.nil? # Let Addressable check the whole URI - WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| @pattern.match(u) } + matches_with_variations?(uri) else # WebMock checks the query, Addressable checks everything else - WebMock::Util::URI.variations_of_uri_as_strings(uri.omit(:query)).any? { |u| @pattern.match(u) } && + matches_with_variations?(uri.omit(:query)) && @query_params == WebMock::Util::QueryMapper.query_to_values(uri.query) end end @@ -177,6 +177,17 @@ def to_s str += " with variables #{@pattern.variables.inspect}" if @pattern.variables str end + + private + + def matches_with_variations?(uri) + normalized_template = Addressable::Template.new(WebMock::Util::URI.heuristic_parse(@pattern.pattern)) + + uri_variations_with_scheme = WebMock::Util::URI.variations_of_uri_as_strings(uri) + .map {|u| WebMock::Util::URI.heuristic_parse(u) } + + uri_variations_with_scheme.any? { |u| normalized_template.match(u) } + end end class URIStringPattern < URIPattern diff --git a/spec/unit/request_pattern_spec.rb b/spec/unit/request_pattern_spec.rb index 8cfb1b03a..8813235b1 100644 --- a/spec/unit/request_pattern_spec.rb +++ b/spec/unit/request_pattern_spec.rb @@ -121,6 +121,12 @@ def match(request_signature) to match(WebMock::RequestSignature.new(:get, "www.example.com")) end + it "should match if Addressable::Template pattern that has ip address host matches request uri" do + signature = WebMock::RequestSignature.new(:get, "127.0.0.1:3000/1234") + uri = Addressable::Template.new("127.0.0.1:3000/{id}") + 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"))