Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Matching partial body params #77

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion lib/webmock/request_pattern.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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) ||
Expand All @@ -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 == ""
Expand Down
7 changes: 6 additions & 1 deletion spec/request_pattern_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down