Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

body params as hash matches if the match pattern is a subset of the body

hash params
  • Loading branch information...
commit f8c47ab8d818e15c3505ef43d3077aca36d8828f 1 parent cd68c86
karmajunkie authored
Showing with 11 additions and 2 deletions.
  1. +5 −1 lib/webmock/request_pattern.rb
  2. +6 −1 spec/request_pattern_spec.rb
View
6 lib/webmock/request_pattern.rb
@@ -147,7 +147,7 @@ def matches?(body, content_type = "")
when :xml then
Crack::XML.parse(body) == @pattern
else
- Addressable::URI.parse('?' + body).query_values == @pattern
+ hash_contains?(Addressable::URI.parse('?' + body).query_values, @pattern)
end
else
empty_string?(@pattern) && empty_string?(body) ||
@@ -161,6 +161,10 @@ def to_s
end
private
+
+ def hash_contains?(haystack, needle)
+ needle.select{|k, v| haystack[k] != v}.empty?
+ end
def empty_string?(string)
string.nil? || string == ""
View
7 spec/request_pattern_spec.rb
@@ -214,7 +214,12 @@ def match(request_signature)
should match(WebMock::RequestSignature.new(:post, "www.example.com", :body => 'a=1&c[d][]=e&b=five&c[d][]=f'))
end
- it "should not match when hash doesn't match url encoded body" do
+ it "should match if body is a superset of pattern hash" do
+ WebMock::RequestPattern.new(:post, "www.example.com", :body => {:a => "1"}).
+ should match(WebMock::RequestSignature.new(:post, "www.example.com", :body => 'a=1&c[d][]=e&c[d][]=f&b=five'))
+ end
+
+ it "should not match when hash is not completely contained by url encoded body" do
WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
should_not match(WebMock::RequestSignature.new(:post, "www.example.com", :body => 'c[d][]=f&a=1&c[d][]=e'))
end
Please sign in to comment.
Something went wrong with that request. Please try again.