Permalink
Browse files

Ability to register and expect requests with empty body.

* Specified empty body and nil body are treated as equal
* If user registers request or expects request with body specified as empty,
it will only match requests with empty body.
  • Loading branch information...
1 parent feb8ba5 commit 4306efacaaf4239f9294c143f0aa0c1c36f205e7 Bartosz Blimke committed Nov 30, 2009
View
@@ -180,10 +180,10 @@ If you want to reset all current stubs and history of requests use `WebMock.rese
An executed request matches stubbed request if it passes following criteria:
- Request URI matches stubbed request URI string or Regexp pattern<br/>
+ When request URI matches stubbed request URI string or Regexp pattern<br/>
And request method is the same as stubbed request method or stubbed request method is :any<br/>
- And request body is the same as stubbed request body or stubbed request body is not set (is nil)<br/>
- And request headers match stubbed request headers, or stubbed request headers match a subset of request headers, or stubbed request headers are not set
+ And request body is the same as stubbed request body or stubbed request body is not specified<br/>
+ And request headers match stubbed request headers, or stubbed request headers match a subset of request headers, or stubbed request headers are not specified
## Precedence of stubs
@@ -17,10 +17,7 @@ def twice
end
def with(options)
- @request_execution_verifier.request_profile.body =
- options[:body] if options.has_key?(:body)
- @request_execution_verifier.request_profile.headers =
- options[:headers] if options.has_key?(:headers)
+ @request_execution_verifier.request_profile.with(options)
self
end
@@ -75,7 +75,7 @@ def request_with_webmock(request, body = nil, &block)
headers = Hash[*request.to_hash.map {|k,v| [k, v.flatten]}.flatten]
headers.reject! {|k,v| k =~ /[Aa]ccept/ && v = '*/*'} #removing header added by Net::HTTP
- request_signature = WebMock::RequestSignature.new(method, uri, body, headers)
+ request_signature = WebMock::RequestSignature.new(method, uri, :body => body, :headers => headers)
WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)
@@ -2,26 +2,56 @@ module WebMock
class RequestProfile < Struct.new(:method, :uri, :body, :headers)
- def initialize(method, uri, body = nil, headers = nil)
- super
- self.uri = WebMock::Util::URI.normalize_uri(self.uri) unless self.uri.is_a?(Addressable::URI)
- self.headers = WebMock::Util::Headers.normalize_headers(self.headers)
+ def initialize(method, uri, options = {})
+ self.method = method
+ self.uri = uri.is_a?(Addressable::URI) ? uri : WebMock::Util::URI.normalize_uri(uri)
+ assign_options(options)
end
def with(options)
- self.body = options[:body] if options.has_key?(:body)
- self.headers = WebMock::Util::Headers.normalize_headers(options[:headers]) if options.has_key?(:headers)
+ assign_options(options)
self
end
def to_s
string = "#{self.method.to_s.upcase} #{WebMock::Util::URI.strip_default_port_from_uri_string(self.uri.to_s)}"
- string << " with body '#{body}'" if body
+ string << " with body '#{body.to_s}'" if body && !body.is_empty?
if headers && !headers.empty?
string << " with headers #{WebMock::Util::Headers.normalize_headers(headers).inspect.gsub("\"","'")}"
end
string
end
+
+ private
+
+ def assign_options(options)
+ self.body = Body.new(options[:body]) if options.has_key?(:body)
+ self.headers = WebMock::Util::Headers.normalize_headers(options[:headers]) if options.has_key?(:headers)
+ end
+
+
+ class Body
+
+ attr_reader :data
+
+ def initialize(data)
+ @data = data
+ end
+
+ def ==(other)
+ other.is_a?(Body) &&
+ (other.is_empty? && self.is_empty? || other.data == self.data)
+ end
+
+ def is_empty?
+ @data.nil? || @data == ""
+ end
+
+ def to_s
+ @data
+ end
+
+ end
end
@@ -9,8 +9,7 @@ def initialize(method, uri)
end
def with(params)
- @request_profile.body = params[:body]
- @request_profile.headers = Util::Headers.normalize_headers(params[:headers])
+ @request_profile.with(params)
self
end
View
@@ -13,13 +13,13 @@ def request(method, uri)
def assert_requested(method, uri, options = {})
expected_times_executed = options.delete(:times) || 1
- request = RequestProfile.new(method, uri, options[:body], options[:headers])
+ request = RequestProfile.new(method, uri, options)
verifier = RequestExecutionVerifier.new(request, expected_times_executed)
assertion_failure(verifier.failure_message) unless verifier.matches?
end
def assert_not_requested(method, uri, options = {})
- request = RequestProfile.new(method, uri, options[:body], options[:headers])
+ request = RequestProfile.new(method, uri, options)
verifier = RequestExecutionVerifier.new(request, options.delete(:times))
assertion_failure(verifier.negative_failure_message) unless verifier.does_not_match?
end
@@ -19,17 +19,19 @@
it "should have assigned normalized headers" do
WebMock::Util::Headers.should_receive(:normalize_headers).with('A' => 'a').and_return('B' => 'b')
- RequestProfile.new(:get, "www.google.com", nil, 'A' => 'a').headers.should == {'B' => 'b'}
+ RequestProfile.new(:get, "www.google.com", :headers => {'A' => 'a'}).headers.should == {'B' => 'b'}
end
it "should have assigned body" do
- RequestProfile.new(:get, "www.google.com", "abc").body.should == "abc"
+ RequestProfile.new(:get, "www.google.com", :body => "abc").
+ body.should == RequestProfile::Body.new("abc")
end
end
it "should report string describing itself" do
- RequestProfile.new(:get, "www.google.com", "abc", {'A' => 'a', 'B' => 'b'}).to_s.should ==
+ RequestProfile.new(:get, "www.google.com",
+ :body => "abc", :headers => {'A' => 'a', 'B' => 'b'}).to_s.should ==
"GET http://www.google.com/ with body 'abc' with headers {'A'=>'a', 'B'=>'b'}"
end
@@ -41,7 +43,12 @@
it "should assign body to request profile" do
@request_profile.with(:body => "abc")
- @request_profile.body.should == "abc"
+ @request_profile.body.should == RequestProfile::Body.new("abc")
+ end
+
+ it "should have the same body" do
+ @request_profile.with(:body => "abc")
+ @request_profile.body.should == RequestProfile::Body.new("abc")
end
it "should assign normalized headers to request profile" do
@@ -86,68 +86,83 @@
it "should match for same bodies" do
- RequestSignature.new(:get, "www.google.com", "abc").
- should match(RequestProfile.new(:get, "www.google.com", "abc"))
+ RequestSignature.new(:get, "www.google.com", :body => "abc").
+ should match(RequestProfile.new(:get, "www.google.com", :body => "abc"))
end
it "should not match for different bodies" do
- RequestSignature.new(:get, "www.google.com", "abc").
- should_not match(RequestProfile.new(:get, "www.google.com", "def"))
+ RequestSignature.new(:get, "www.google.com", :body => "abc").
+ should_not match(RequestProfile.new(:get, "www.google.com", :body => "def"))
end
- it "should match is other has nil body" do
- RequestSignature.new(:get, "www.google.com", "abc").
- should match(RequestProfile.new(:get, "www.google.com", nil))
+ it "should match if other has not specified body" do
+ RequestSignature.new(:get, "www.google.com", :body => "abc").
+ should match(RequestProfile.new(:get, "www.google.com"))
+ end
+
+ it "should not match if other has nil body" do
+ RequestSignature.new(:get, "www.google.com", :body => "abc").
+ should_not match(RequestProfile.new(:get, "www.google.com", :body => nil))
end
it "should not match if other has empty body" do
- RequestSignature.new(:get, "www.google.com", "abc").
- should_not match(RequestProfile.new(:get, "www.google.com", ""))
+ RequestSignature.new(:get, "www.google.com", :body => "abc").
+ should_not match(RequestProfile.new(:get, "www.google.com", :body => ""))
+ end
+
+ it "should not match if other has body" do
+ RequestSignature.new(:get, "www.google.com").
+ should_not match(RequestProfile.new(:get, "www.google.com", :body => "abc"))
end
it "should match for same headers" do
- RequestSignature.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg').
- should match(RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg'))
+ RequestSignature.new(:get, "www.google.com", :headers => {'Content-Type' => 'image/jpeg'}).
+ should match(RequestProfile.new(:get, "www.google.com", :headers => {'Content-Type' => 'image/jpeg'}))
end
it "should not match for different values of the same header" do
- RequestSignature.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg').
- should_not match(RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/png'))
+ RequestSignature.new(:get, "www.google.com", :headers => {'Content-Type' => 'image/jpeg'}).
+ should_not match(RequestProfile.new(:get, "www.google.com", :headers => {'Content-Type' => 'image/png'}))
end
it "should match if request has more headers than other" do
- RequestSignature.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg', 'Content-Length' => '8888').
- should match(RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg'))
+ RequestSignature.new(:get, "www.google.com", :headers => {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}).
+ should match(RequestProfile.new(:get, "www.google.com", :headers => {'Content-Type' => 'image/jpeg'}))
end
it "should not match if request has less headers that the other and all match" do
- RequestSignature.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg').
- should_not match(RequestProfile.new(:get, "www.google.com", nil, 'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'))
+ RequestSignature.new(:get, "www.google.com", :headers => {'Content-Type' => 'image/jpeg'}).
+ should_not match(RequestProfile.new(:get, "www.google.com", :headers => {'Content-Type' => 'image/jpeg', 'Content-Length' => '8888'}))
end
it "should match even is header keys or values are in different format" do
- RequestSignature.new(:get, "www.google.com", nil, :ContentLength => 8888, 'content_type' => 'image/png').
- should match(RequestProfile.new(:get, "www.google.com", nil, 'ContentLength' => '8888', 'Content-type' => 'image/png'))
+ RequestSignature.new(:get, "www.google.com", :headers => {:ContentLength => 8888, 'content_type' => 'image/png'}).
+ should match(RequestProfile.new(:get, "www.google.com", :headers => {'ContentLength' => '8888', 'Content-type' => 'image/png'}))
+ end
+
+ it "should match is other has not specified" do
+ RequestSignature.new(:get, "www.google.com", :headers => {'A' => 'a'}).
+ should match(RequestProfile.new(:get, "www.google.com"))
end
- it "should match is other has nil headers" do
- RequestSignature.new(:get, "www.google.com", nil, 'A' => 'a').
- should match(RequestProfile.new(:get, "www.google.com", nil, nil))
+ it "should not match is other has nil headers" do
+ RequestSignature.new(:get, "www.google.com", :headers => {'A' => 'a'}).
+ should match(RequestProfile.new(:get, "www.google.com", :headers => nil))
end
it "should not match if other has empty headers" do
- RequestSignature.new(:get, "www.google.com", nil, 'A' => 'a').
- should_not match(RequestProfile.new(:get, "www.google.com", nil, {}))
+ RequestSignature.new(:get, "www.google.com", :headers => {'A' => 'a'}).
+ should_not match(RequestProfile.new(:get, "www.google.com", :headers => {}))
end
it "should not match if profile has no headers but other has headers" do
- RequestSignature.new(:get, "www.google.com", nil, nil).
- should_not match(RequestProfile.new(:get, "www.google.com", nil, {'A'=>'a'}))
+ RequestSignature.new(:get, "www.google.com").
+ should_not match(RequestProfile.new(:get, "www.google.com", :headers => {'A'=>'a'}))
end
it "should not match if profile has empty headers but other has headers" do
- RequestSignature.new(:get, "www.google.com", nil, {}).
- should_not match(RequestProfile.new(:get, "www.google.com", nil, {'A'=>'a'}))
+ RequestSignature.new(:get, "www.google.com", :headers => {}).
+ should_not match(RequestProfile.new(:get, "www.google.com", :headers => {'A'=>'a'}))
end
end
@@ -19,7 +19,7 @@
it "should assign body to request profile" do
@request_stub.with(:body => "abc")
- @request_stub.request_profile.body.should == "abc"
+ @request_stub.request_profile.body.should == RequestProfile::Body.new("abc")
end
it "should assign normalized headers to request profile" do

0 comments on commit 4306efa

Please sign in to comment.