aboutsummaryrefslogtreecommitdiffstats
path: root/activeresource/test/cases/http_mock_test.rb
diff options
context:
space:
mode:
Diffstat (limited to 'activeresource/test/cases/http_mock_test.rb')
-rw-r--r--activeresource/test/cases/http_mock_test.rb130
1 files changed, 130 insertions, 0 deletions
diff --git a/activeresource/test/cases/http_mock_test.rb b/activeresource/test/cases/http_mock_test.rb
index 5e032d03f1..43cf5f5ef0 100644
--- a/activeresource/test/cases/http_mock_test.rb
+++ b/activeresource/test/cases/http_mock_test.rb
@@ -59,6 +59,136 @@ class HttpMockTest < ActiveSupport::TestCase
assert_equal "XML", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body
assert_equal "Json", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body
end
+
+ test "raises InvalidRequestError if no response found for the #{method} request" do
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/xml"}, "XML")
+ end
+
+ assert_raise(::ActiveResource::InvalidRequestError) do
+ request(method, "/people/1", FORMAT_HEADER[method] => "application/json")
+ end
+ end
+
+ end
+
+ test "allows you to send in pairs directly to the respond_to method" do
+ matz = { :id => 1, :name => "Matz" }.to_xml(:root => "person")
+
+ create_matz = ActiveResource::Request.new(:post, '/people.xml', matz, {})
+ created_response = ActiveResource::Response.new("", 201, {"Location" => "/people/1.xml"})
+ get_matz = ActiveResource::Request.new(:get, '/people/1.xml', nil)
+ ok_response = ActiveResource::Response.new(matz, 200, {})
+
+ pairs = {create_matz => created_response, get_matz => ok_response}
+
+ ActiveResource::HttpMock.respond_to(pairs)
+ assert_equal 2, ActiveResource::HttpMock.responses.length
+ assert_equal "", ActiveResource::HttpMock.responses.assoc(create_matz)[1].body
+ assert_equal matz, ActiveResource::HttpMock.responses.assoc(get_matz)[1].body
+ end
+
+ test "resets all mocked responses on each call to respond_to with a block by default" do
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.send(:get, "/people/1", {}, "XML1")
+ end
+ assert_equal 1, ActiveResource::HttpMock.responses.length
+
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.send(:get, "/people/2", {}, "XML2")
+ end
+ assert_equal 1, ActiveResource::HttpMock.responses.length
+ end
+
+ test "resets all mocked responses on each call to respond_to by passing pairs by default" 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.xml', nil)
+ ok_response = ActiveResource::Response.new(matz, 200, {})
+ ActiveResource::HttpMock.respond_to({get_matz => ok_response})
+
+ assert_equal 1, ActiveResource::HttpMock.responses.length
+ end
+
+ test "allows you to add new responses to the existing responses by calling a block" do
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.send(:get, "/people/1", {}, "XML1")
+ end
+ assert_equal 1, ActiveResource::HttpMock.responses.length
+
+ ActiveResource::HttpMock.respond_to(false) do |mock|
+ mock.send(:get, "/people/2", {}, "XML2")
+ end
+ assert_equal 2, ActiveResource::HttpMock.responses.length
+ end
+
+ test "allows you to add new responses to the existing responses by passing pairs" 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.xml', nil)
+ ok_response = ActiveResource::Response.new(matz, 200, {})
+ ActiveResource::HttpMock.respond_to({get_matz => ok_response}, false)
+
+ assert_equal 2, ActiveResource::HttpMock.responses.length
+ end
+
+ test "allows you to replace the existing reponse with the same request by calling a block" do
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.send(:get, "/people/1", {}, "XML1")
+ end
+ assert_equal 1, ActiveResource::HttpMock.responses.length
+
+ ActiveResource::HttpMock.respond_to(false) do |mock|
+ mock.send(:get, "/people/1", {}, "XML2")
+ end
+ assert_equal 1, ActiveResource::HttpMock.responses.length
+ end
+
+ test "allows you to replace the existing reponse with the same request by passing pairs" 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
+
+ test "do not replace the response with the same path but different method by calling a block" do
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.send(:get, "/people/1", {}, "XML1")
+ end
+ assert_equal 1, ActiveResource::HttpMock.responses.length
+
+ ActiveResource::HttpMock.respond_to(false) do |mock|
+ mock.send(:put, "/people/1", {}, "XML2")
+ end
+ assert_equal 2, ActiveResource::HttpMock.responses.length
+ end
+
+ test "do not replace the response with the same path but different method by passing pairs" do
+ ActiveResource::HttpMock.respond_to do |mock|
+ mock.send(:get, "/people/1", {}, "XML1")
+ end
+ assert_equal 1, ActiveResource::HttpMock.responses.length
+
+ put_matz = ActiveResource::Request.new(:put, '/people/1', nil)
+ ok_response = ActiveResource::Response.new("", 200, {})
+
+ ActiveResource::HttpMock.respond_to({put_matz => ok_response}, false)
+ assert_equal 2, ActiveResource::HttpMock.responses.length
end
def request(method, path, headers = {}, body = nil)