Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Allowing regex to be used for hash matching #81

Closed
wants to merge 1 commit into from

2 participants

@GBH

Something I'm testing generates a random value that is being used in the request. Can't really intercept it or capture. So I added ability to do matching using regex:

assert_requested :post, "http://www.example.com", :body => {:a => \^\w{5}$\, :b => {:c => \^\d{3}$\}}

I changed how hashes are being compared to use ===
So 'abc' === 'abc' works same as \^\w{3}$\ === 'abc'

@jcf
Collaborator

I like this bit of functionality and think it could be a nice feature to add to WebMock's tool belt.

What are your thoughts on renaming some of the locals for the sake of readability? I've pushed a commit (@017e7ac) to bblimke/gbh-matching_hashes with my idea. I've also added some yardoc to the method.

Let me know your thoughts.

@GBH

I think this is great. Hopefully it will be merged in eventually.

@jcf
Collaborator
@GBH

As long as functionality is there, you can change whatever makes sense. Thanks!

@jcf
Collaborator

Applied to master in @70667d0. Thanks Oleg.

@jcf jcf closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 27, 2011
  1. @GBH
This page is out of date. Refresh to see the latest.
Showing with 21 additions and 4 deletions.
  1. +15 −3 lib/webmock/request_pattern.rb
  2. +6 −1 spec/request_pattern_spec.rb
View
18 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) ||
@@ -161,6 +161,18 @@ def to_s
end
private
+
+ def matching_hashes?(h1, h2)
+ return false if h1.size != h2.size
+ h1.each do |k, v|
+ if v.is_a?(Hash) && h2[k].is_a?(Hash)
+ return false unless matching_hashes?(v, h2[k])
+ else
+ return false unless h2[k] === v
+ end
+ end
+ true
+ end
def empty_string?(string)
string.nil? || string == ""
View
7 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"))
@@ -223,6 +223,11 @@ def match(request_signature)
WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
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
end
Something went wrong with that request. Please try again.