Skip to content
This repository

Allowing regex to be used for hash matching #81

Closed
wants to merge 1 commit into from

2 participants

Oleg James Conroy-Finn
Oleg
GBH commented March 27, 2011

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'

James Conroy-Finn
Collaborator
jcf commented May 18, 2011

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.

Oleg
GBH commented May 18, 2011

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

James Conroy-Finn
Collaborator
jcf commented May 18, 2011
Oleg
GBH commented May 18, 2011

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

James Conroy-Finn
Collaborator
jcf commented May 18, 2011

Applied to master in @70667d0. Thanks Oleg.

James Conroy-Finn jcf closed this May 18, 2011
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Showing 1 unique commit by 1 author.

Mar 27, 2011
Oleg allowing regex to be used in hash pattern matching ce5db2b
This page is out of date. Refresh to see the latest.
18  lib/webmock/request_pattern.rb
@@ -143,11 +143,11 @@ def matches?(body, content_type = "")
143 143
 
144 144
         case BODY_FORMATS[content_type]
145 145
         when :json then
146  
-          Crack::JSON.parse(body) == @pattern
  146
+          matching_hashes?(Crack::JSON.parse(body), @pattern)
147 147
         when :xml then
148  
-          Crack::XML.parse(body) == @pattern
  148
+          matching_hashes?(Crack::XML.parse(body), @pattern)
149 149
         else
150  
-          Addressable::URI.parse('?' + body).query_values == @pattern
  150
+          matching_hashes?(Addressable::URI.parse('?' + body).query_values, @pattern)
151 151
         end
152 152
       else
153 153
         empty_string?(@pattern) && empty_string?(body) ||
@@ -161,6 +161,18 @@ def to_s
161 161
     end
162 162
 
163 163
     private
  164
+    
  165
+    def matching_hashes?(h1, h2)
  166
+      return false if h1.size != h2.size
  167
+      h1.each do |k, v|
  168
+        if v.is_a?(Hash) && h2[k].is_a?(Hash)
  169
+          return false unless matching_hashes?(v, h2[k])
  170
+        else
  171
+          return false unless h2[k] === v
  172
+        end
  173
+      end
  174
+      true
  175
+    end
164 176
 
165 177
     def empty_string?(string)
166 178
       string.nil? || string == ""
7  spec/request_pattern_spec.rb
@@ -145,7 +145,7 @@ def match(request_signature)
145 145
        WebMock::RequestPattern.new(:get, "www.example.com", :query => {"a" => ["b", "c"]}).
146 146
           should_not match(WebMock::RequestSignature.new(:get, "www.example.com?x[]=b&a[]=c"))
147 147
       end
148  
-
  148
+      
149 149
       it "should match for query params are the same as declared as string" do
150 150
        WebMock::RequestPattern.new(:get, "www.example.com", :query => "a[]=b&a[]=c").
151 151
           should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
@@ -223,6 +223,11 @@ def match(request_signature)
223 223
            WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
224 224
               should_not match(WebMock::RequestSignature.new(:post, "www.example.com", :body => 'foo bar'))
225 225
           end
  226
+          
  227
+          it "should match when hash contains regex values" do
  228
+           WebMock::RequestPattern.new(:post, "www.example.com", :body => {:a => /^\w{5}$/, :b => {:c => /^\d{3}$/}}).
  229
+              should match(WebMock::RequestSignature.new(:post, "www.example.com", :body => 'a=abcde&b[c]=123'))
  230
+          end
226 231
 
227 232
         end
228 233
 
Commit_comment_tip

Tip: You can add notes to lines in a file. Hover to the left of a line to make a note

Something went wrong with that request. Please try again.