Skip to content

Commit

Permalink
Fix InvalidURIError with ip:port uri
Browse files Browse the repository at this point in the history
  • Loading branch information
uiur committed May 19, 2018
1 parent ca54cf8 commit 2bf4453
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 2 deletions.
15 changes: 13 additions & 2 deletions lib/webmock/request_pattern.rb
Expand Up @@ -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
Expand All @@ -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
Expand Down
6 changes: 6 additions & 0 deletions spec/unit/request_pattern_spec.rb
Expand Up @@ -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"))
Expand Down

0 comments on commit 2bf4453

Please sign in to comment.