Skip to content

Commit

Permalink
Added info about declaring query params inside stubs as a hash
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartosz Blimke committed Jun 19, 2010
1 parent 6e67ced commit 316263c
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 10 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -138,7 +138,13 @@ You can also use WebMock without RSpec or Test::Unit support:
stub_request(:any, /.*example.*/) stub_request(:any, /.*example.*/)


Net::HTTP.get('www.example.com', '/') # ===> Success 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 ### Stubbing with custom response


stub_request(:any, "www.example.com").to_return(:body => "abc", :status => 200, :headers => { 'Content-Length' => 3 } ) stub_request(:any, "www.example.com").to_return(:body => "abc", :status => 200, :headers => { 'Content-Length' => 3 } )
Expand Down
16 changes: 15 additions & 1 deletion lib/webmock/request_pattern.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ def to_s
def assign_options(options) def assign_options(options)
@body_pattern = BodyPattern.new(options[:body]) if options.has_key?(:body) @body_pattern = BodyPattern.new(options[:body]) if options.has_key?(:body)
@headers_pattern = HeadersPattern.new(options[:headers]) if options.has_key?(:headers) @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


end end
Expand Down Expand Up @@ -67,7 +68,8 @@ def matches?(uri)
##TODO : do I need to normalize again?? ##TODO : do I need to normalize again??
uri === @pattern uri === @pattern
elsif @pattern.is_a?(Regexp) 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 else
false false
end end
Expand All @@ -76,6 +78,18 @@ def matches?(uri)
def to_s def to_s
WebMock::Util::URI.strip_default_port_from_uri_string(@pattern.to_s) WebMock::Util::URI.strip_default_port_from_uri_string(@pattern.to_s)
end 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 end


class BodyPattern class BodyPattern
Expand Down
52 changes: 43 additions & 9 deletions spec/request_pattern_spec.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -119,6 +119,40 @@ def match(request_signature)


end 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 describe "when matching requests with body" do


it "should match if request body and body pattern are the same" do it "should match if request body and body pattern are the same" do
Expand Down Expand Up @@ -179,12 +213,12 @@ def match(request_signature)
RequestPattern.new(:post, 'www.example.com', :body => body_hash). 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')) should_not match(RequestSignature.new(:post, "www.example.com", :body => 'c[d][]=f&a=1&c[d][]=e'))
end end

it "should not match when body is not url encoded" do it "should not match when body is not url encoded" do
RequestPattern.new(:post, 'www.example.com', :body => body_hash). RequestPattern.new(:post, 'www.example.com', :body => body_hash).
should_not match(RequestSignature.new(:post, "www.example.com", :body => 'foo bar')) should_not match(RequestSignature.new(:post, "www.example.com", :body => 'foo bar'))
end end

end end


describe "for request with json body and content type is set to json" do describe "for request with json body and content type is set to json" do
Expand All @@ -199,17 +233,17 @@ def match(request_signature)
should match(RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/json'}, should match(RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/json'},
:body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}")) :body => "{\"a\":\"1\",\"b\":\"five\",\"c\":{\"d\":[\"e\",\"f\"]}}"))
end end

it "should not match when body is not json" do it "should not match when body is not json" do
RequestPattern.new(:post, 'www.example.com', :body => body_hash). 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/json'}, :body => "foo bar")) :headers => {:content_type => 'application/json'}, :body => "foo bar"))
end end
end end


describe "for request with xml body and content type is set to xml" do 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']}}} } let(:body_hash) { {"opt" => {:a => '1', :b => 'five', 'c' => {'d' => ['e', 'f']}}} }

it "should match when hash matches body" do it "should match when hash matches body" do
RequestPattern.new(:post, 'www.example.com', :body => body_hash). RequestPattern.new(:post, 'www.example.com', :body => body_hash).
should match(RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/xml'}, should match(RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/xml'},
Expand All @@ -221,12 +255,12 @@ def match(request_signature)
should match(RequestSignature.new(:post, "www.example.com", :headers => {:content_type => 'application/xml'}, 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")) :body => "<opt b=\"five\" a=\"1\">\n <c>\n <d>e</d>\n <d>f</d>\n </c>\n</opt>\n"))
end end

it "should not match when body is not xml" do it "should not match when body is not xml" do
RequestPattern.new(:post, 'www.example.com', :body => body_hash). 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")) :headers => {:content_type => 'application/xml'}, :body => "foo bar"))
end end
end end
end end
end end
Expand Down

0 comments on commit 316263c

Please sign in to comment.