Skip to content

Commit

Permalink
Add support for matching parameters using regular expressions
Browse files Browse the repository at this point in the history
  • Loading branch information
Oleg authored and James Conroy-Finn committed May 18, 2011
1 parent f4d1afe commit 70667d0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 4 deletions.
43 changes: 40 additions & 3 deletions lib/webmock/request_pattern.rb
Expand Up @@ -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) ||
Expand All @@ -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
Expand Down
11 changes: 10 additions & 1 deletion spec/request_pattern_spec.rb
Expand Up @@ -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"))
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 70667d0

Please sign in to comment.