Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Added info about declaring query params inside stubs as a hash

  • Loading branch information...
commit 316263c8dfd2cb173e288af644afb16f3eee3543 1 parent 6e67ced
Bartosz Blimke authored
View
6 README.md
@@ -138,7 +138,13 @@ You can also use WebMock without RSpec or Test::Unit support:
stub_request(:any, /.*example.*/)
Net::HTTP.get('www.example.com', '/') # ===> Success
+
+### Matching query params using hash
+ stub_http_request(:get, "www.example.com").with(:query => {"a" => ["b", "c"]})
+
+ RestClient.get("http://www.example.com/?a[]=b&a[]=c") # ===> Success
+
### Stubbing with custom response
stub_request(:any, "www.example.com").to_return(:body => "abc", :status => 200, :headers => { 'Content-Length' => 3 } )
View
16 lib/webmock/request_pattern.rb
@@ -38,6 +38,7 @@ def to_s
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
end
@@ -67,7 +68,8 @@ def matches?(uri)
##TODO : do I need to normalize again??
uri === @pattern
elsif @pattern.is_a?(Regexp)
- WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| u.match(@pattern) }
+ WebMock::Util::URI.variations_of_uri_as_strings(uri).any? { |u| u.match(@pattern) } &&
+ (@query_params.nil? || @query_params == uri.query_values)
else
false
end
@@ -76,6 +78,18 @@ def matches?(uri)
def to_s
WebMock::Util::URI.strip_default_port_from_uri_string(@pattern.to_s)
end
+
+ def add_query_params(query_params)
+ if @pattern.is_a?(Addressable::URI)
+ if !query_params.is_a?(Hash)
+ query_params = Addressable::URI.parse('?' + query_params).query_values
+ end
+ @pattern.query_values = (@pattern.query_values || {}).merge(query_params)
+ else
+ @query_params = query_params.is_a?(Hash) ? query_params : Addressable::URI.parse('?' + query_params).query_values
+ end
+ end
+
end
class BodyPattern
View
52 spec/request_pattern_spec.rb
@@ -119,6 +119,40 @@ def match(request_signature)
end
+ describe "when matching requests on query params" do
+
+ it "should match request query params even if uri is declared as regexp" do
+ RequestPattern.new(:get, /.*example.*/, :query => {"a" => ["b", "c"]}).
+ should match(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
+ RequestPattern.new(:get, /.*example.*/, :query => {"x" => ["b", "c"]}).
+ should_not match(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
+ RequestPattern.new(:get, "www.example.com", :query => {"a" => ["b", "c"]}).
+ should match(RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
+ end
+
+ it "should not match for query params are different than the declared in hash" do
+ RequestPattern.new(:get, "www.example.com", :query => {"a" => ["b", "c"]}).
+ should_not match(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
+ RequestPattern.new(:get, "www.example.com", :query => "a[]=b&a[]=c").
+ should match(RequestSignature.new(:get, "www.example.com?a[]=b&a[]=c"))
+ end
+
+ it "should match for query params are the same as declared both in query option or url" do
+ RequestPattern.new(:get, "www.example.com/?x=3", :query => "a[]=b&a[]=c").
+ should match(RequestSignature.new(:get, "www.example.com/?x=3&a[]=b&a[]=c"))
+ end
+
+ end
+
describe "when matching requests with body" do
it "should match if request body and body pattern are the same" do
@@ -179,12 +213,12 @@ def match(request_signature)
RequestPattern.new(:post, 'www.example.com', :body => body_hash).
should_not match(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
RequestPattern.new(:post, 'www.example.com', :body => body_hash).
should_not match(RequestSignature.new(:post, "www.example.com", :body => 'foo bar'))
end
-
+
end
describe "for request with json body and content type is set to json" do
@@ -199,17 +233,17 @@ def match(request_signature)
should match(RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/json'},
:body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}"))
end
-
+
it "should not match when body is not json" do
RequestPattern.new(:post, 'www.example.com', :body => body_hash).
- should_not match(RequestSignature.new(:post, "www.example.com",
- :headers => {:content_type => 'application/json'}, :body => "foo bar"))
+ should_not match(RequestSignature.new(:post, "www.example.com",
+ :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
RequestPattern.new(:post, 'www.example.com', :body => body_hash).
should match(RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/xml'},
@@ -221,12 +255,12 @@ def match(request_signature)
should match(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
-
+
it "should not match when body is not xml" do
RequestPattern.new(:post, 'www.example.com', :body => body_hash).
- should_not match(RequestSignature.new(:post, "www.example.com",
+ should_not match(RequestSignature.new(:post, "www.example.com",
:headers => {:content_type => 'application/xml'}, :body => "foo bar"))
- end
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.