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 regression: Allow using Addressable::Template for URI host part #797

Merged
merged 6 commits into from Mar 11, 2020
Merged
Show file tree
Hide file tree
Changes from 3 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
5 changes: 2 additions & 3 deletions lib/webmock/request_pattern.rb
Expand Up @@ -181,9 +181,8 @@ def to_s
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).any? { |u| normalized_template.match(u) }
template = Addressable::Template.new(uri)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@valscion I believe that with this change you are ignoring @pattern.pattern and soimply matching the passed uri with the pattern generated with the same uri, which obviously matches :) feel free to add a spec with webmock pattern that doesn't match your url (e.g different) domain and this code will still match.

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps we should silently rescue InvalidURiError and try again with not normalized version of the pattern?
@rafaelfranca since you are also dealing with similar issue, any thoughts on that?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh true -- seems like we indeed need some negative assertion specs instead of only positive cases here ☺️. I was quite surprised that none of the existing specs failed with this change here

WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| template.match(u) }
end
end

Expand Down
6 changes: 6 additions & 0 deletions spec/unit/request_pattern_spec.rb
Expand Up @@ -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"))
Expand Down