Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix invalid scheme error with Addressable::Template #758

Merged
merged 3 commits into from
Jun 3, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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