Skip to content

Commit

Permalink
Merge pull request #758 from uiureo/fix-489
Browse files Browse the repository at this point in the history
Fix invalid scheme error with Addressable::Template
  • Loading branch information
bblimke committed Jun 3, 2018
2 parents 11feb72 + 1962400 commit 20e8d38
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 4 deletions.
13 changes: 11 additions & 2 deletions lib/webmock/request_pattern.rb
Original file line number Diff line number Diff line change
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,15 @@ 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))

WebMock::Util::URI.variations_of_uri_as_strings(uri, only_with_scheme: true)
.any? { |u| normalized_template.match(u) }
end
end

class URIStringPattern < URIPattern
Expand Down
4 changes: 2 additions & 2 deletions lib/webmock/util/uri.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ def self.normalize_uri(uri)
NORMALIZED_URIS[uri].dup
end

def self.variations_of_uri_as_strings(uri_object)
def self.variations_of_uri_as_strings(uri_object, only_with_scheme: false)
normalized_uri = normalize_uri(uri_object.dup).freeze
uris = [ normalized_uri ]

Expand All @@ -47,7 +47,7 @@ def self.variations_of_uri_as_strings(uri_object)
uris = uris_with_inferred_port_and_without(uris)
end

if normalized_uri.scheme == "http"
if normalized_uri.scheme == "http" && !only_with_scheme
uris = uris_with_scheme_and_without(uris)
end

Expand Down
6 changes: 6 additions & 0 deletions spec/unit/request_pattern_spec.rb
Original file line number Diff line number Diff line change
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 20e8d38

Please sign in to comment.