Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Add support for matching parameters using regular expressions

  • Loading branch information...
commit 70667d097a459220a2a79715320f4a5bb84270f6 1 parent f4d1afe
@GBH GBH authored jcf committed
Showing with 50 additions and 4 deletions.
  1. +40 −3 lib/webmock/request_pattern.rb
  2. +10 −1 spec/request_pattern_spec.rb
View
43 lib/webmock/request_pattern.rb
@@ -143,11 +143,11 @@ def matches?(body, content_type = "")
case BODY_FORMATS[content_type]
when :json then
- Crack::JSON.parse(body) == @pattern
+ matching_hashes?(Crack::JSON.parse(body), @pattern)
when :xml then
- Crack::XML.parse(body) == @pattern
+ matching_hashes?(Crack::XML.parse(body), @pattern)
else
- Addressable::URI.parse('?' + body).query_values == @pattern
+ matching_hashes?(Addressable::URI.parse('?' + body).query_values, @pattern)
end
else
empty_string?(@pattern) && empty_string?(body) ||
@@ -162,6 +162,43 @@ def to_s
private
+ # Compare two hashes for equality
+ #
+ # For two hashes to match they must have the same length and all
+ # values must match when compared using `#===`.
+ #
+ # The following hashes are examples of matches:
+ #
+ # {a: /\d+/} and {a: '123'}
+ #
+ # {a: '123'} and {a: '123'}
+ #
+ # {a: {b: /\d+/}} and {a: {b: '123'}}
+ #
+ # {a: {b: 'wow'}} and {a: {b: 'wow'}}
+ #
+ # @param [Hash] query_parameters typically the result of parsing
+ # JSON, XML or URL encoded parameters.
+ #
+ # @param [Hash] pattern which contains keys with a string, hash or
+ # regular expression value to use for comparison.
+ #
+ # @return [Boolean] true if the paramaters match the comparison
+ # hash, false if not.
+ def matching_hashes?(query_parameters, pattern)
+ return false unless query_parameters.size == pattern.size
+ query_parameters.each do |key, actual|
+ expected = pattern[key]
+
+ if actual.is_a?(Hash) && expected.is_a?(Hash)
+ return false unless matching_hashes?(actual, expected)
+ else
+ return false unless expected === actual
+ end
+ end
+ true
+ end
+
def empty_string?(string)
string.nil? || string == ""
end
View
11 spec/request_pattern_spec.rb
@@ -145,7 +145,7 @@ def match(request_signature)
WebMock::RequestPattern.new(:get, "www.example.com", :query => {"a" => ["b", "c"]}).
should_not match(WebMock::RequestSignature.new(:get, "www.example.com?x[]=b&a[]=c"))
end
-
+
it "should match for query params are the same as declared as string" do
WebMock::RequestPattern.new(:get, "www.example.com", :query => "a[]=b&a[]=c").
should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
@@ -224,6 +224,15 @@ def match(request_signature)
should_not match(WebMock::RequestSignature.new(:post, "www.example.com", :body => 'foo bar'))
end
+ it "should match when hash contains regex values" do
+ WebMock::RequestPattern.new(:post, "www.example.com", :body => {:a => /^\w{5}$/, :b => {:c => /^\d{3}$/}}).
+ should match(WebMock::RequestSignature.new(:post, "www.example.com", :body => 'a=abcde&b[c]=123'))
+ end
+
+ it "should not match when hash does not contains regex values" do
+ WebMock::RequestPattern.new(:post, "www.example.com", :body => {:a => /^\d+$/, :b => {:c => /^\d{3}$/}}).
+ should_not match(WebMock::RequestSignature.new(:post, "www.example.com", :body => 'a=abcde&b[c]=123'))
+ end
end
describe "for request with json body and content type is set to json" do
Please sign in to comment.
Something went wrong with that request. Please try again.