Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Added support for matching partial query params with "hash_including"

  • Loading branch information...
commit 2d03aca4acb3a65e976250c538393f373ffbdb9d 1 parent 1e658f8
@bblimke authored
View
10 README.md
@@ -172,6 +172,12 @@ You can also use WebMock outside a test framework:
RestClient.get("http://www.example.com/?a[]=b&a[]=c") # ===> Success
+### Matching partial query params using hash
+
+ stub_http_request(:get, "www.example.com").with(:query => hash_including({"a" => ["b", "c"]}))
+
+ RestClient.get("http://www.example.com/?a[]=b&a[]=c&x=1") # ===> Success
+
### Stubbing with custom response
stub_request(:any, "www.example.com").to_return(:body => "abc", :status => 200, :headers => { 'Content-Length' => 3 } )
@@ -405,6 +411,8 @@ This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
WebMock.should have_requested(:get, "www.example.com").with(:query => {"a" => ["b", "c"]})
+ WebMock.should have_requested(:get, "www.example.com").with(:query => hash_including({"a" => ["b", "c"]}))
+
WebMock.should have_requested(:get, "www.example.com").
with(:body => {"a" => ["b", "c"]}, :headers => {'Content-Type' => 'application/json'})
@@ -420,6 +428,8 @@ This forces WebMock Net::HTTP adapter to always connect on `Net::HTTP.start`.
a_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]}).should have_been_made
+ a_request(:get, "www.example.com").with(:query => hash_including({"a" => ["b", "c"]})).should have_been_made
+
a_request(:post, "www.example.com").
with(:body => {"a" => ["b", "c"]}, :headers => {'Content-Type' => 'application/json'}).should have_been_made
View
2  lib/webmock.rb
@@ -24,6 +24,8 @@
require 'webmock/util/hash_keys_stringifier'
require 'webmock/util/json'
+require 'webmock/matchers/hash_including_matcher'
+
require 'webmock/request_pattern'
require 'webmock/request_signature'
require 'webmock/responses_sequence'
View
8 lib/webmock/api.rb
@@ -32,6 +32,14 @@ def assert_not_requested(*args, &block)
assert_request_not_requested(*args, &block)
end
+ def hash_including(expected)
+ if defined?(RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher)
+ RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new(expected)
+ else
+ WebMock::Matchers::HashIncludingMatcher.new(expected)
+ end
+ end
+
private
def convert_uri_method_and_options_to_request_and_options(*args, &block)
View
25 lib/webmock/matchers/hash_including_matcher.rb
@@ -0,0 +1,25 @@
+module WebMock
+ module Matchers
+ #this is a based on RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher
+ #https://github.com/rspec/rspec-mocks/blob/master/lib/rspec/mocks/argument_matchers.rb
+ class HashIncludingMatcher
+ def initialize(expected)
+ @expected = expected
+ end
+
+ def ==(actual)
+ @expected.all? {|k,v| actual.has_key?(k) && v == actual[k]}
+ rescue NoMethodError
+ false
+ end
+
+ def inspect
+ "hash_including(#{@expected.inspect})"
+ end
+
+ def self.from_rspec_matcher(matcher)
+ new(matcher.instance_variable_get(:@expected))
+ end
+ end
+ end
+end
View
150 lib/webmock/request_pattern.rb
@@ -40,19 +40,19 @@ def to_s
private
- def assign_options(options)
- @body_pattern = BodyPattern.new(options[:body]) if options.has_key?(:body)
- @headers_pattern = HeadersPattern.new(options[:headers]) if options.has_key?(:headers)
- @uri_pattern.add_query_params(options[:query]) if options.has_key?(:query)
- end
+ def assign_options(options)
+ @body_pattern = BodyPattern.new(options[:body]) if options.has_key?(:body)
+ @headers_pattern = HeadersPattern.new(options[:headers]) if options.has_key?(:headers)
+ @uri_pattern.add_query_params(options[:query]) if options.has_key?(:query)
+ end
- def create_uri_pattern(uri)
- if uri.is_a?(Regexp)
- URIRegexpPattern.new(uri)
- else
- URIStringPattern.new(uri)
+ def create_uri_pattern(uri)
+ if uri.is_a?(Regexp)
+ URIRegexpPattern.new(uri)
+ else
+ URIStringPattern.new(uri)
+ end
end
- end
end
@@ -76,6 +76,25 @@ class URIPattern
def initialize(pattern)
@pattern = pattern.is_a?(Addressable::URI) ? pattern : WebMock::Util::URI.normalize_uri(pattern)
end
+
+ def add_query_params(query_params)
+ @query_params = case query_params
+ when Hash
+ query_params
+ when WebMock::Matchers::HashIncludingMatcher
+ query_params
+ when RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher
+ WebMock::Matchers::HashIncludingMatcher.from_rspec_matcher(query_params)
+ else
+ Addressable::URI.parse('?' + query_params).query_values
+ end
+ end
+
+ def to_s
+ str = @pattern.inspect
+ str += " with query params #{@query_params.inspect}" if @query_params
+ str
+ end
end
class URIRegexpPattern < URIPattern
@@ -94,33 +113,34 @@ def to_s
str += " with query params #{@query_params.inspect}" if @query_params
str
end
-
- def add_query_params(query_params)
- @query_params = query_params.is_a?(Hash) ? query_params : Addressable::URI.parse('?' + query_params).query_values
- end
-
end
class URIStringPattern < URIPattern
def matches?(uri)
if @pattern.is_a?(Addressable::URI)
- uri === @pattern
+ if @query_params
+ uri.omit(:query) === @pattern && (@query_params.nil? || @query_params == uri.query_values)
+ else
+ uri === @pattern
+ end
else
false
end
end
def add_query_params(query_params)
- if !query_params.is_a?(Hash)
- query_params = Addressable::URI.parse('?' + query_params).query_values
+ super
+ if @query_params.is_a?(Hash) || @query_params.is_a?(String)
+ @pattern.query_values = (@pattern.query_values || {}).merge(@query_params)
+ @query_params = nil
end
- @pattern.query_values = (@pattern.query_values || {}).merge(query_params)
end
def to_s
- WebMock::Util::URI.strip_default_port_from_uri_string(@pattern.to_s)
+ str = WebMock::Util::URI.strip_default_port_from_uri_string(@pattern.to_s)
+ str += " with query params #{@query_params.inspect}" if @query_params
+ str
end
-
end
@@ -171,50 +191,50 @@ 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
+ # 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
- true
- end
- def empty_string?(string)
- string.nil? || string == ""
- end
+ def empty_string?(string)
+ string.nil? || string == ""
+ end
- def normalize_hash(hash)
- Hash[WebMock::Util::HashKeysStringifier.stringify_keys!(hash).sort]
- end
+ def normalize_hash(hash)
+ Hash[WebMock::Util::HashKeysStringifier.stringify_keys!(hash).sort]
+ end
end
@@ -241,9 +261,9 @@ def to_s
private
- def empty_headers?(headers)
- headers.nil? || headers == {}
- end
+ def empty_headers?(headers)
+ headers.nil? || headers == {}
+ end
end
end
View
7 spec/acceptance/shared/request_expectations.rb
@@ -150,6 +150,13 @@
a_request(:get, "www.example.com/?x=3").with(:query => {"a" => ["b", "c"]}).should have_been_made
}.should_not raise_error
end
+
+ it "should satisfy expectation if the request was executed with only part query params declared as a hash in a query option" do
+ lambda {
+ http_request(:get, "http://www.example.com/?a[]=b&a[]=c&b=1")
+ a_request(:get, "www.example.com").with(:query => hash_including({"a" => ["b", "c"]})).should have_been_made
+ }.should_not raise_error
+ end
end
it "should fail if request was made more times than expected" do
View
5 spec/acceptance/shared/stubbing_requests.rb
@@ -32,6 +32,11 @@
stub_request(:get, "www.example.com/?x=3").with(:query => {"a" => ["b", "c"]}).to_return(:body => "abc")
http_request(:get, "http://www.example.com/?x=3&a[]=b&a[]=c").body.should == "abc"
end
+
+ it "should return stubbed response when stub expects only part of query params" do
+ stub_request(:get, "www.example.com").with(:query => hash_including({"a" => ["b", "c"]})).to_return(:body => "abc")
+ http_request(:get, "http://www.example.com/?a[]=b&a[]=c&b=1").body.should == "abc"
+ end
end
describe "based on method" do
View
251 spec/unit/request_pattern_spec.rb
@@ -2,21 +2,29 @@
describe WebMock::RequestPattern do
- it "should report string describing itself" do
- WebMock::RequestPattern.new(:get, "www.example.com",
+ describe "describing itself" do
+ it "should report string describing itself" do
+ WebMock::RequestPattern.new(:get, "www.example.com",
:body => "abc", :headers => {'A' => 'a', 'B' => 'b'}).to_s.should ==
- "GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'}"
- end
+ "GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'}"
+ end
- it "should report string describing itself with block" do
- WebMock::RequestPattern.new(:get, "www.example.com",
+ it "should report string describing itself with block" do
+ WebMock::RequestPattern.new(:get, "www.example.com",
:body => "abc", :headers => {'A' => 'a', 'B' => 'b'}).with {|req| true}.to_s.should ==
- "GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'} with given block"
- end
+ "GET http://www.example.com/ with body \"abc\" with headers {'A'=>'a', 'B'=>'b'} with given block"
+ end
+
+ it "should report string describing itself with query params" do
+ WebMock::RequestPattern.new(:get, /.*example.*/, :query => {'a' => ['b', 'c']}).to_s.should ==
+ "GET /.*example.*/ with query params {\"a\"=>[\"b\", \"c\"]}"
+ end
- it "should report string describing itself with query params" do
- WebMock::RequestPattern.new(:get, /.*example.*/, :query => {'a' => ['b', 'c']}).to_s.should ==
- "GET /.*example.*/ with query params {\"a\"=>[\"b\", \"c\"]}"
+ it "should report string describing itself with query params as hash including maycher" do
+ WebMock::RequestPattern.new(:get, /.*example.*/,
+ :query => WebMock::Matchers::HashIncludingMatcher.new({'a' => ['b', 'c']})).to_s.should ==
+ "GET /.*example.*/ with query params hash_including({\"a\"=>[\"b\", \"c\"]})"
+ end
end
describe "with" do
@@ -46,79 +54,79 @@ def match(request_signature)
describe "when matching" do
it "should match if uri matches and method matches" do
- WebMock::RequestPattern.new(:get, "www.example.com").
+ WebMock::RequestPattern.new(:get, "www.example.com").
should match(WebMock::RequestSignature.new(:get, "www.example.com"))
end
it "should match if uri matches and method pattern is any" do
- WebMock::RequestPattern.new(:any, "www.example.com").
+ WebMock::RequestPattern.new(:any, "www.example.com").
should match(WebMock::RequestSignature.new(:get, "www.example.com"))
end
it "should not match if request has different method" do
- WebMock::RequestPattern.new(:post, "www.example.com").
+ WebMock::RequestPattern.new(:post, "www.example.com").
should_not match(WebMock::RequestSignature.new(:get, "www.example.com"))
end
it "should match if uri matches request uri" do
- WebMock::RequestPattern.new(:get, "www.example.com").
+ WebMock::RequestPattern.new(:get, "www.example.com").
should match(WebMock::RequestSignature.new(:get, "www.example.com"))
end
it "should match if request has unescaped uri" do
- WebMock::RequestPattern.new(:get, "www.example.com/my%20path").
+ WebMock::RequestPattern.new(:get, "www.example.com/my%20path").
should match(WebMock::RequestSignature.new(:get, "www.example.com/my path"))
end
it "should match if request has escaped uri" do
- WebMock::RequestPattern.new(:get, "www.example.com/my path").
+ WebMock::RequestPattern.new(:get, "www.example.com/my path").
should match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path"))
end
it "should match if uri regexp pattern matches unescaped form of request uri" do
- WebMock::RequestPattern.new(:get, /.*my path.*/).
+ WebMock::RequestPattern.new(:get, /.*my path.*/).
should match(WebMock::RequestSignature.new(:get, "www.example.com/my%20path"))
end
it "should match if uri regexp pattern matches request uri" do
- WebMock::RequestPattern.new(:get, /.*example.*/).
+ WebMock::RequestPattern.new(:get, /.*example.*/).
should match(WebMock::RequestSignature.new(:get, "www.example.com"))
end
it "should match for uris with same parameters as pattern" do
- WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2").
+ WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2").
should match(WebMock::RequestSignature.new(:get, "www.example.com?a=1&b=2"))
end
it "should not match for uris with different parameters" do
- WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2").
+ WebMock::RequestPattern.new(:get, "www.example.com?a=1&b=2").
should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a=2&b=1"))
end
it "should match for uri parameters in different order" do
- WebMock::RequestPattern.new(:get, "www.example.com?b=2&a=1").
+ WebMock::RequestPattern.new(:get, "www.example.com?b=2&a=1").
should match(WebMock::RequestSignature.new(:get, "www.example.com?a=1&b=2"))
end
describe "when parameters are escaped" do
it "should match if uri pattern has escaped parameters and request has unescaped parameters" do
- WebMock::RequestPattern.new(:get, "www.example.com/?a=a%20b").
+ WebMock::RequestPattern.new(:get, "www.example.com/?a=a%20b").
should match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))
end
it "should match if uri pattern has unescaped parameters and request has escaped parameters" do
- WebMock::RequestPattern.new(:get, "www.example.com/?a=a b").
+ WebMock::RequestPattern.new(:get, "www.example.com/?a=a b").
should match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))
end
it "should match if uri regexp pattern matches uri with unescaped parameters and request has escaped parameters" do
- WebMock::RequestPattern.new(:get, /.*a=a b.*/).
+ WebMock::RequestPattern.new(:get, /.*a=a b.*/).
should match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a%20b"))
end
it "should match if uri regexp pattern matches uri with escaped parameters and request has unescaped parameters" do
- WebMock::RequestPattern.new(:get, /.*a=a%20b.*/).
+ WebMock::RequestPattern.new(:get, /.*a=a%20b.*/).
should match(WebMock::RequestSignature.new(:get, "www.example.com/?a=a b"))
end
@@ -126,77 +134,128 @@ def match(request_signature)
describe "when matching requests on query params" do
- it "should match request query params even if uri is declared as regexp" do
- WebMock::RequestPattern.new(:get, /.*example.*/, :query => {"a" => ["b", "c"]}).
- should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
- end
+ describe "when uri is described as regexp" do
+ it "should match request query params" do
+ WebMock::RequestPattern.new(:get, /.*example.*/, :query => {"a" => ["b", "c"]}).
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
+ end
- it "should match request query params if uri is declared as regexp but params don't match" do
- WebMock::RequestPattern.new(:get, /.*example.*/, :query => {"x" => ["b", "c"]}).
- should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
- end
+ it "should match request query params if params don't match" do
+ WebMock::RequestPattern.new(:get, /.*example.*/, :query => {"x" => ["b", "c"]}).
+ should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
+ end
- it "should match for query params are the same as declared in hash" do
- WebMock::RequestPattern.new(:get, "www.example.com", :query => {"a" => ["b", "c"]}).
- should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
- end
+ it "should match when query params are declared as HashIncluding matcher matching params" do
+ WebMock::RequestPattern.new(:get, /.*example.*/,
+ :query => WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]})).
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
+ end
- it "should not match for query params are different than the declared in hash" do
- 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 not match when query params are declared as HashIncluding matcher not matching params" do
+ WebMock::RequestPattern.new(:get, /.*example.*/,
+ :query => WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]})).
+ should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
+ 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"))
- end
+ it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
+ WebMock::RequestPattern.new(:get, /.*example.*/,
+ :query => RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]})).
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
+ end
- it "should match for query params are the same as declared both in query option or url" do
- WebMock::RequestPattern.new(:get, "www.example.com/?x=3", :query => "a[]=b&a[]=c").
- should match(WebMock::RequestSignature.new(:get, "www.example.com/?x=3&a[]=b&a[]=c"))
+ it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
+ WebMock::RequestPattern.new(:get, /.*example.*/,
+ :query => RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]})).
+ should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
+ end
end
+ describe "when uri is described as string" do
+ it "should match when query params are the same as declared in hash" do
+ WebMock::RequestPattern.new(:get, "www.example.com", :query => {"a" => ["b", "c"]}).
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
+ end
+
+ it "should not match when query params are different than the declared in hash" do
+ 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 when 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"))
+ end
+
+ it "should match when query params are the same as declared both in query option or url" do
+ WebMock::RequestPattern.new(:get, "www.example.com/?x=3", :query => "a[]=b&a[]=c").
+ should match(WebMock::RequestSignature.new(:get, "www.example.com/?x=3&a[]=b&a[]=c"))
+ end
+
+ it "should match when query params are declared as HashIncluding matcher matching params" do
+ WebMock::RequestPattern.new(:get, "www.example.com",
+ :query => WebMock::Matchers::HashIncludingMatcher.new({"a" => ["b", "c"]})).
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
+ end
+
+ it "should not match when query params are declared as HashIncluding matcher not matching params" do
+ WebMock::RequestPattern.new(:get, "www.example.com",
+ :query => WebMock::Matchers::HashIncludingMatcher.new({"x" => ["b", "c"]})).
+ should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
+ end
+
+ it "should match when query params are declared as RSpec HashIncluding matcher matching params" do
+ WebMock::RequestPattern.new(:get, "www.example.com",
+ :query => RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "c"]})).
+ should match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
+ end
+
+ it "should not match when query params are declared as RSpec HashIncluding matcher not matching params" do
+ WebMock::RequestPattern.new(:get, "www.example.com",
+ :query => RSpec::Mocks::ArgumentMatchers::HashIncludingMatcher.new({"a" => ["b", "d"]})).
+ should_not match(WebMock::RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c&b=1"))
+ end
+ end
end
describe "when matching requests with body" do
it "should match if request body and body pattern are the same" do
- WebMock::RequestPattern.new(:get, "www.example.com", :body => "abc").
+ WebMock::RequestPattern.new(:get, "www.example.com", :body => "abc").
should match(WebMock::RequestSignature.new(:get, "www.example.com", :body => "abc"))
end
it "should match if request body matches regexp" do
- WebMock::RequestPattern.new(:get, "www.example.com", :body => /^abc$/).
+ WebMock::RequestPattern.new(:get, "www.example.com", :body => /^abc$/).
should match(WebMock::RequestSignature.new(:get, "www.example.com", :body => "abc"))
end
it "should not match if body pattern is different than request body" do
- WebMock::RequestPattern.new(:get, "www.example.com", :body => "def").
+ WebMock::RequestPattern.new(:get, "www.example.com", :body => "def").
should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :body => "abc"))
end
it "should not match if request body doesn't match regexp pattern" do
- WebMock::RequestPattern.new(:get, "www.example.com", :body => /^abc$/).
+ WebMock::RequestPattern.new(:get, "www.example.com", :body => /^abc$/).
should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :body => "xabc"))
end
it "should match if pattern doesn't have specified body" do
- WebMock::RequestPattern.new(:get, "www.example.com").
+ WebMock::RequestPattern.new(:get, "www.example.com").
should match(WebMock::RequestSignature.new(:get, "www.example.com", :body => "abc"))
end
it "should not match if pattern has body specified as nil but request body is not empty" do
- WebMock::RequestPattern.new(:get, "www.example.com", :body => nil).
+ WebMock::RequestPattern.new(:get, "www.example.com", :body => nil).
should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :body => "abc"))
end
it "should not match if pattern has empty body but request body is not empty" do
- WebMock::RequestPattern.new(:get, "www.example.com", :body => "").
+ WebMock::RequestPattern.new(:get, "www.example.com", :body => "").
should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :body => "abc"))
end
it "should not match if pattern has body specified but request has no body" do
- WebMock::RequestPattern.new(:get, "www.example.com", :body => "abc").
+ WebMock::RequestPattern.new(:get, "www.example.com", :body => "abc").
should_not match(WebMock::RequestSignature.new(:get, "www.example.com"))
end
@@ -205,76 +264,76 @@ def match(request_signature)
describe "for request with url encoded body" do
it "should match when hash matches body" do
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
should match(WebMock::RequestSignature.new(:post, "www.example.com", :body => 'a=1&c[d][]=e&c[d][]=f&b=five'))
end
it "should match when hash matches body in different order of params" do
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
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
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
+ 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
it "should not match when body is not url encoded" do
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
+ 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}$/}}).
+ 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}$/}}).
+ 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
it "should match when hash matches body" do
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
should match(WebMock::RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/json'},
- :body => "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}"))
- end
+ :body => "{\"a\":\"1\",\"c\":{\"d\":[\"e\",\"f\"]},\"b\":\"five\"}"))
+ end
it "should match if hash matches body in different form" do
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
should match(WebMock::RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/json'},
- :body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}"))
- end
+ :body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}"))
+ end
it "should not match when body is not json" do
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
should_not match(WebMock::RequestSignature.new(:post, "www.example.com",
- :headers => {:content_type => 'application/json'}, :body => "foo bar"))
- end
+ :headers => {:content_type => 'application/json'}, :body => "foo bar"))
+ end
end
describe "for request with xml body and content type is set to xml" do
let(:body_hash) { {"opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}}} }
it "should match when hash matches body" do
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
should match(WebMock::RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/xml'},
- :body => "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
- end
+ :body => "<opt a=\"1\" b=\"five\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
+ end
it "should match if hash matches body in different form" do
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
should match(WebMock::RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/xml'},
- :body => "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
- end
+ :body => "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
+ end
it "should not match when body is not xml" do
- WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
+ WebMock::RequestPattern.new(:post, 'www.example.com', :body => body_hash).
should_not match(WebMock::RequestSignature.new(:post, "www.example.com",
- :headers => {:content_type => 'application/xml'}, :body => "foo bar"))
- end
+ :headers => {:content_type => 'application/xml'}, :body => "foo bar"))
+ end
end
end
end
@@ -282,78 +341,78 @@ def match(request_signature)
it "should match if pattern and request have the same headers" do
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
should match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
end
it "should match if pattern headers values are regexps matching request header values" do
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => %r{^image/jpeg$}}).
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => %r{^image/jpeg$}}).
should match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
end
it "should not match if pattern has different value of header than request" do
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/png'}).
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/png'}).
should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
end
it "should not match if pattern header value regexp doesn't match request header value" do
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => %r{^image\/jpeg$}}).
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => %r{^image\/jpeg$}}).
should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpegx'}))
end
it "should match if request has more headers than request pattern" do
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}).
should match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}))
end
it "should not match if request has less headers than the request pattern" do
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}).
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}).
should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'Content-Type' => 'image/jpeg'}))
end
it "should match even is header keys are declared in different form" do
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'ContentLength' => '8888', 'Content-type' => 'image/png'}).
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'ContentLength' => '8888', 'Content-type' => 'image/png'}).
should match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {:ContentLength => 8888, 'content_type' => 'image/png'}))
end
it "should match is pattern doesn't have specified headers" do
- WebMock::RequestPattern.new(:get, "www.example.com").
+ WebMock::RequestPattern.new(:get, "www.example.com").
should match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}))
end
it "should not match if pattern has nil headers but request has headers" do
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => nil).
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => nil).
should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}))
end
it "should not match if pattern has empty headers but request has headers" do
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {}).
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {}).
should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {'A' => 'a'}))
end
it "should not match if pattern has specified headers but request has nil headers" do
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'A'=>'a'}).
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'A'=>'a'}).
should_not match(WebMock::RequestSignature.new(:get, "www.example.com"))
end
it "should not match if pattern has specified headers but request has empty headers" do
- WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'A'=>'a'}).
+ WebMock::RequestPattern.new(:get, "www.example.com", :headers => {'A'=>'a'}).
should_not match(WebMock::RequestSignature.new(:get, "www.example.com", :headers => {}))
end
it "should match if block given in pattern evaluates request to true" do
- WebMock::RequestPattern.new(:get, "www.example.com").with { |request| true }.
+ WebMock::RequestPattern.new(:get, "www.example.com").with { |request| true }.
should match(WebMock::RequestSignature.new(:get, "www.example.com"))
end
it "should not match if block given in pattrn evaluates request to false" do
- WebMock::RequestPattern.new(:get, "www.example.com").with { |request| false }.
+ WebMock::RequestPattern.new(:get, "www.example.com").with { |request| false }.
should_not match(WebMock::RequestSignature.new(:get, "www.example.com"))
end
it "should yield block with request signature" do
signature = WebMock::RequestSignature.new(:get, "www.example.com")
- WebMock::RequestPattern.new(:get, "www.example.com").with { |request| request == signature }.
+ WebMock::RequestPattern.new(:get, "www.example.com").with { |request| request == signature }.
should match(signature)
end
Please sign in to comment.
Something went wrong with that request. Please try again.