Skip to content

Commit

Permalink
Merge pull request #797 from valscion/template-in-host-part
Browse files Browse the repository at this point in the history
Fix regression: Allow using Addressable::Template for URI host part
  • Loading branch information
bblimke authored Mar 11, 2020
2 parents a9cf1b7 + 77e8543 commit 0fec93b
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 3 deletions.
10 changes: 7 additions & 3 deletions lib/webmock/request_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -181,9 +181,13 @@ 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 =
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

Expand Down
12 changes: 12 additions & 0 deletions spec/unit/request_pattern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -132,6 +132,18 @@ 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 not match if Addressable::Template pattern host does not match request uri" do
signature = WebMock::RequestSignature.new(:get, "www.bad-example.com")
uri = Addressable::Template.new("{subdomain}.example.com")
expect(WebMock::RequestPattern.new(:get, uri)).not_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 0fec93b

Please sign in to comment.