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
Conversation
This comment has been minimized.
This comment has been minimized.
Hi there
|
@valscion I haven't had a chance to investigate that yet. If it's something that was supported and now is broken then I guess we should bring the support back. I can have a look when I get some time, unless someone finds the fix before me. |
@valscion have you found a solution to this problem, we've just upgraded webmock and been hit by this? |
Nope, we're stuck in webmock 3.4.1 |
This allows using Addressable::Template directly and not crash when a template is used for the URI host part
Seems like all specs pass when I merely remove the The spec added in #758 that caused this regression still passes with this change, so this PR should be good to go? Care to check it out @bblimke when you've got time? 💞 |
lib/webmock/request_pattern.rb
Outdated
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) |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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
Sorry, I haven't had the time to do the requested changes in this PR. I can however confirm that the issue as described in this PR is still a problem in v3.8.0 and prevents us from upgrading from v3.4.1 😓 |
Hi @bblimke, I now added the negative assertion spec case (and you're right, it did fail as expected as the code was not correct). I went with what I presume was "silently rescue InvalidURiError and try again with not normalized version of the pattern" as you commented in #797 (comment) The tests do pass now. Is there anything left to address here? Would love to get us to upgrade Ruby to 2.6 but as we're stuck in webmock 3.4.1 we're not yet able to upgrade to Ruby 2.6 as it needs a newer version of webmock 😅 EDIT: We were able to monkeypatch this change in to get to webmock v3.8.2, so there's no rush on our side, but of course it would be nice if we didn't have to have the following monkeypatch applied: # TODO: Remove this warning and monkeypatch once https://github.com/bblimke/webmock/pull/797 ships
if WebMock::VERSION != '3.8.2'
STDERR.puts "!!! " + ("NOTE " * 15) + "!!!"
STDERR.puts "!!! " + ("NOTE " * 15) + "!!!"
STDERR.puts "!!! " + ("NOTE " * 15) + "!!!"
STDERR.puts
STDERR.puts "--------->>>> Check if webmock monkeypatch still applies in spec/support/webmock.rb <<<<---------"
STDERR.puts "--------->>>> Is https://github.com/bblimke/webmock/pull/797 merged and in a new release? <<<<---------"
STDERR.puts
STDERR.puts "!!! " + ("NOTE " * 15) + "!!!"
STDERR.puts "!!! " + ("NOTE " * 15) + "!!!"
STDERR.puts "!!! " + ("NOTE " * 15) + "!!!"
end
module WebMock
class URIAddressablePattern
# https://github.com/bblimke/webmock/blob/77e8543b334d9b0fa09597c6de8ba3b4e9feee0d/lib/webmock/request_pattern.rb#L183-L191
private def matches_with_variations?(uri)
template =
begin
Addressable::Template.new(WebMock::Util::URI.heuristic_parse(@pattern.pattern))
rescue Addressable::URI::InvalidURIError
Addressable::Template.new(@pattern.pattern)
end
WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| template.match(u) }
end
end
end |
Thank you @valscion ! |
It's now released as 3.8.3 |
Nice, thank you! |
Ever since webmock v3.4.2, we've had a few failing specs that have blocked us from upgrading webmock. All of them are a variant of
Addressable::URI::InvalidURIError
error with the messageInvalid character in host
The full error looks like this:
and the spec scenario looks like this:
At first we thought that our spec was somehow broken. I looked into this more closely today, and I think it might be a bug in webmock, possibly caused by #758
I wasn't able to figure out another way to specify that we're not interested in the lowest sub-domain in the URL (the
{data_center}
part in my example). It seems like using a template right afterhttps://
part should work, as peraddressable
docs, but it doesn't seem to work anymore.This PR contains a failing test to show the error, and a commit that fixes the failure.
The change avoids going through
Addressable::URI.heuristic_parse
when the input is already anAddressable::URI
instance. This avoid the error raised fromheuristic_parse
to be triggered.