提交 b9f8501f 编写于 作者: G Gaston Ramos 提交者: José Valim

- Fix ActiveResource::HttpMock.respond_to replace the response

  if it has the same request
Signed-off-by: NJosé Valim <jose.valim@gmail.com>
上级 243513f4
...@@ -100,11 +100,11 @@ def responses ...@@ -100,11 +100,11 @@ def responses
# Accepts a block which declares a set of requests and responses for the HttpMock to respond to in # Accepts a block which declares a set of requests and responses for the HttpMock to respond to in
# the following format: # the following format:
# #
# mock.http_method(path, request_headers = {}, body = nil, status = 200, response_headers = {}) # mock.http_method(path, request_headers = {}, body = nil, status = 200, response_headers = {})
# #
# === Example # === Example
# #
# @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person") # @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
# ActiveResource::HttpMock.respond_to do |mock| # ActiveResource::HttpMock.respond_to do |mock|
# mock.post "/people.xml", {}, @matz, 201, "Location" => "/people/1.xml" # mock.post "/people.xml", {}, @matz, 201, "Location" => "/people/1.xml"
...@@ -112,58 +112,58 @@ def responses ...@@ -112,58 +112,58 @@ def responses
# mock.put "/people/1.xml", {}, nil, 204 # mock.put "/people/1.xml", {}, nil, 204
# mock.delete "/people/1.xml", {}, nil, 200 # mock.delete "/people/1.xml", {}, nil, 200
# end # end
# #
# Alternatively, accepts a hash of <tt>{Request => Response}</tt> pairs allowing you to generate # Alternatively, accepts a hash of <tt>{Request => Response}</tt> pairs allowing you to generate
# these the following format: # these the following format:
# #
# ActiveResource::Request.new(method, path, body, request_headers) # ActiveResource::Request.new(method, path, body, request_headers)
# ActiveResource::Response.new(body, status, response_headers) # ActiveResource::Response.new(body, status, response_headers)
# #
# === Example # === Example
# #
# Request.new(:#{method}, path, nil, request_headers) # Request.new(:#{method}, path, nil, request_headers)
# #
# @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person") # @matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
# #
# create_matz = ActiveResource::Request.new(:post, '/people.xml', @matz, {}) # create_matz = ActiveResource::Request.new(:post, '/people.xml', @matz, {})
# created_response = ActiveResource::Response.new("", 201, {"Location" => "/people/1.xml"}) # created_response = ActiveResource::Response.new("", 201, {"Location" => "/people/1.xml"})
# get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil) # get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
# ok_response = ActiveResource::Response.new("", 200, {}) # ok_response = ActiveResource::Response.new("", 200, {})
# #
# pairs = {create_matz => created_response, get_matz => ok_response} # pairs = {create_matz => created_response, get_matz => ok_response}
# #
# ActiveResource::HttpMock.respond_to(pairs) # ActiveResource::HttpMock.respond_to(pairs)
# #
# Note, by default, every time you call +respond_to+, any previous request and response pairs stored # Note, by default, every time you call +respond_to+, any previous request and response pairs stored
# in HttpMock will be deleted giving you a clean slate to work on. # in HttpMock will be deleted giving you a clean slate to work on.
# #
# If you want to override this behaviour, pass in +false+ as the last argument to +respond_to+ # If you want to override this behaviour, pass in +false+ as the last argument to +respond_to+
# #
# === Example # === Example
# #
# ActiveResource::HttpMock.respond_to do |mock| # ActiveResource::HttpMock.respond_to do |mock|
# mock.send(:get, "/people/1", {}, "XML1") # mock.send(:get, "/people/1", {}, "XML1")
# end # end
# ActiveResource::HttpMock.responses.length #=> 1 # ActiveResource::HttpMock.responses.length #=> 1
# #
# ActiveResource::HttpMock.respond_to(false) do |mock| # ActiveResource::HttpMock.respond_to(false) do |mock|
# mock.send(:get, "/people/2", {}, "XML2") # mock.send(:get, "/people/2", {}, "XML2")
# end # end
# ActiveResource::HttpMock.responses.length #=> 2 # ActiveResource::HttpMock.responses.length #=> 2
# #
# This also works with passing in generated pairs of requests and responses, again, just pass in false # This also works with passing in generated pairs of requests and responses, again, just pass in false
# as the last argument: # as the last argument:
# #
# === Example # === Example
# #
# ActiveResource::HttpMock.respond_to do |mock| # ActiveResource::HttpMock.respond_to do |mock|
# mock.send(:get, "/people/1", {}, "XML1") # mock.send(:get, "/people/1", {}, "XML1")
# end # end
# ActiveResource::HttpMock.responses.length #=> 1 # ActiveResource::HttpMock.responses.length #=> 1
# #
# get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil) # get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
# ok_response = ActiveResource::Response.new("", 200, {}) # ok_response = ActiveResource::Response.new("", 200, {})
# #
# pairs = {get_matz => ok_response} # pairs = {get_matz => ok_response}
# #
# ActiveResource::HttpMock.respond_to(pairs, false) # ActiveResource::HttpMock.respond_to(pairs, false)
...@@ -171,6 +171,8 @@ def responses ...@@ -171,6 +171,8 @@ def responses
def respond_to(*args) #:yields: mock def respond_to(*args) #:yields: mock
pairs = args.first || {} pairs = args.first || {}
reset! if args.last.class != FalseClass reset! if args.last.class != FalseClass
delete_responses_to_replace pairs.to_a
responses.concat pairs.to_a responses.concat pairs.to_a
if block_given? if block_given?
yield Responder.new(responses) yield Responder.new(responses)
...@@ -179,6 +181,13 @@ def respond_to(*args) #:yields: mock ...@@ -179,6 +181,13 @@ def respond_to(*args) #:yields: mock
end end
end end
def delete_responses_to_replace(new_responses)
new_responses.each{|nr|
request_to_remove = nr[0]
@@responses = responses.delete_if{|r| r[0] == request_to_remove}
}
end
# Deletes all logged requests and responses. # Deletes all logged requests and responses.
def reset! def reset!
requests.clear requests.clear
......
...@@ -69,19 +69,19 @@ class HttpMockTest < ActiveSupport::TestCase ...@@ -69,19 +69,19 @@ class HttpMockTest < ActiveSupport::TestCase
request(method, "/people/1", FORMAT_HEADER[method] => "application/json") request(method, "/people/1", FORMAT_HEADER[method] => "application/json")
end end
end end
end end
test "allows you to send in pairs directly to the respond_to method" do test "allows you to send in pairs directly to the respond_to method" do
matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person") matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
create_matz = ActiveResource::Request.new(:post, '/people.xml', matz, {}) create_matz = ActiveResource::Request.new(:post, '/people.xml', matz, {})
created_response = ActiveResource::Response.new("", 201, {"Location" => "/people/1.xml"}) created_response = ActiveResource::Response.new("", 201, {"Location" => "/people/1.xml"})
get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil) get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
ok_response = ActiveResource::Response.new(matz, 200, {}) ok_response = ActiveResource::Response.new(matz, 200, {})
pairs = {create_matz => created_response, get_matz => ok_response} pairs = {create_matz => created_response, get_matz => ok_response}
ActiveResource::HttpMock.respond_to(pairs) ActiveResource::HttpMock.respond_to(pairs)
assert_equal 2, ActiveResource::HttpMock.responses.length assert_equal 2, ActiveResource::HttpMock.responses.length
assert_equal "", ActiveResource::HttpMock.responses.assoc(create_matz)[1].body assert_equal "", ActiveResource::HttpMock.responses.assoc(create_matz)[1].body
...@@ -140,6 +140,21 @@ class HttpMockTest < ActiveSupport::TestCase ...@@ -140,6 +140,21 @@ class HttpMockTest < ActiveSupport::TestCase
assert_equal 2, ActiveResource::HttpMock.responses.length assert_equal 2, ActiveResource::HttpMock.responses.length
end end
test "allows you to add replace the existing reponese with the same path" do
ActiveResource::HttpMock.respond_to do |mock|
mock.send(:get, "/people/1", {}, "XML1")
end
assert_equal 1, ActiveResource::HttpMock.responses.length
matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
get_matz = ActiveResource::Request.new(:get, '/people/1', nil)
ok_response = ActiveResource::Response.new(matz, 200, {})
ActiveResource::HttpMock.respond_to({get_matz => ok_response}, false)
assert_equal 1, ActiveResource::HttpMock.responses.length
end
def request(method, path, headers = {}, body = nil) def request(method, path, headers = {}, body = nil)
if [:put, :post].include? method if [:put, :post].include? method
@http.send(method, path, body, headers) @http.send(method, path, body, headers)
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册