Permalink
Browse files

If query is set via WebMock, then validate it with WebMock, ignoring …

…the query part of the Adressable::Template
  • Loading branch information...
1 parent e027ec1 commit aa86b972d73799807b6151ab9de7cf81d3ca290a @maxlinc maxlinc committed Nov 20, 2013
Showing with 41 additions and 4 deletions.
  1. +10 −2 lib/webmock/request_pattern.rb
  2. +31 −2 spec/unit/request_pattern_spec.rb
@@ -128,11 +128,19 @@ def to_s
class URIAddressablePattern < URIPattern
def matches?(uri)
- WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| @pattern.match(u) }
+ if @query_params.nil?
+ # Let Addressable check the whole URI
+ WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| @pattern.match(u) }
+ 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) } &&
+ (@query_params.nil? || @query_params == WebMock::Util::QueryMapper.query_to_values(uri.query))
+ end
end
def add_query_params(query_params)
- raise NotImplementedError, "Query params must be specified in the pattern when using Addressable::Template"
+ warn "WebMock warning: ignoring query params in RFC 6570 template and checking them with WebMock"
+ super(query_params)
end
def to_s
@@ -208,8 +208,37 @@ def match(request_signature)
describe "when uri is described as Addressable::Template" do
it "should raise error if query params are specified" do
- expect{WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), :query => {"a" => ["b", "c"]})}.
- to raise_error(NotImplementedError, "Query params must be specified in the pattern when using Addressable::Template")
+ WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), :query => {"a" => ["b", "c"]}).
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
+ end
+
+ it "should match request query params if params don't match" do
+ WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"), :query => {"x" => ["b", "c"]}).
+ should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
+ end
+
+ it "should match when query params are declared as HashIncluding matcher matching params" do
+ WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
+ :query => WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]})).
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
+ end
+
+ it "should not match when query params are declared as HashIncluding matcher not matching params" do
+ WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
+ :query => WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]})).
+ should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
+ end
+
+ it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
+ WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
+ :query => RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]})).
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
+ end
+
+ it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
+ WebMock::RequestPattern.new(:get, Addressable::Template.new("www.example.com"),
+ :query => RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]})).
+ should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
end
end

0 comments on commit aa86b97

Please sign in to comment.