diff options
Diffstat (limited to 'activeresource/test/cases')
-rw-r--r-- | activeresource/test/cases/base_errors_test.rb | 13 | ||||
-rw-r--r-- | activeresource/test/cases/http_mock_test.rb | 71 | ||||
-rw-r--r-- | activeresource/test/cases/log_subscriber_test.rb | 9 |
3 files changed, 87 insertions, 6 deletions
diff --git a/activeresource/test/cases/base_errors_test.rb b/activeresource/test/cases/base_errors_test.rb index b4fd75fba3..5063916d10 100644 --- a/activeresource/test/cases/base_errors_test.rb +++ b/activeresource/test/cases/base_errors_test.rb @@ -17,7 +17,7 @@ class BaseErrorsTest < Test::Unit::TestCase end end - def test_should_parse_xml_errors + def test_should_parse_json_and_xml_errors [ :json, :xml ].each do |format| invalid_user_using_format(format) do assert_kind_of ActiveResource::Errors, @person.errors @@ -26,6 +26,17 @@ class BaseErrorsTest < Test::Unit::TestCase end end + def test_should_parse_json_errors_when_no_errors_key + ActiveResource::HttpMock.respond_to do |mock| + mock.post "/people.json", {}, '{}', 422, {'Content-Type' => 'application/json; charset=utf-8'} + end + + invalid_user_using_format(:json) do + assert_kind_of ActiveResource::Errors, @person.errors + assert_equal 0, @person.errors.size + end + end + def test_should_parse_errors_to_individual_attributes [ :json, :xml ].each do |format| invalid_user_using_format(format) do diff --git a/activeresource/test/cases/http_mock_test.rb b/activeresource/test/cases/http_mock_test.rb new file mode 100644 index 0000000000..5e032d03f1 --- /dev/null +++ b/activeresource/test/cases/http_mock_test.rb @@ -0,0 +1,71 @@ +require 'abstract_unit' + +class HttpMockTest < ActiveSupport::TestCase + setup do + @http = ActiveResource::HttpMock.new("http://example.com") + end + + FORMAT_HEADER = ActiveResource::Connection::HTTP_FORMAT_HEADER_NAMES + + [:post, :put, :get, :delete, :head].each do |method| + test "responds to simple #{method} request" do + ActiveResource::HttpMock.respond_to do |mock| + mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/xml"}, "Response") + end + + assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body + end + + test "adds format header by default to #{method} request" do + ActiveResource::HttpMock.respond_to do |mock| + mock.send(method, "/people/1", {}, "Response") + end + + assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body + end + + test "respond only when headers match header by default to #{method} request" do + ActiveResource::HttpMock.respond_to do |mock| + mock.send(method, "/people/1", {"X-Header" => "X"}, "Response") + end + + assert_equal "Response", request(method, "/people/1", "X-Header" => "X").body + assert_raise(ActiveResource::InvalidRequestError) { request(method, "/people/1") } + end + + test "does not overwrite format header to #{method} request" do + ActiveResource::HttpMock.respond_to do |mock| + mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/json"}, "Response") + end + + assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body + end + + test "ignores format header when there is only one response to same url in a #{method} request" do + ActiveResource::HttpMock.respond_to do |mock| + mock.send(method, "/people/1", {}, "Response") + end + + assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/json").body + assert_equal "Response", request(method, "/people/1", FORMAT_HEADER[method] => "application/xml").body + end + + test "responds correctly when format header is given to #{method} request" do + ActiveResource::HttpMock.respond_to do |mock| + mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/xml"}, "XML") + mock.send(method, "/people/1", {FORMAT_HEADER[method] => "application/json"}, "Json") + end + + 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 + end + + def request(method, path, headers = {}, body = nil) + if [:put, :post].include? method + @http.send(method, path, body, headers) + else + @http.send(method, path, headers) + end + end +end diff --git a/activeresource/test/cases/log_subscriber_test.rb b/activeresource/test/cases/log_subscriber_test.rb index f0330e8f51..3cd96007db 100644 --- a/activeresource/test/cases/log_subscriber_test.rb +++ b/activeresource/test/cases/log_subscriber_test.rb @@ -1,12 +1,11 @@ require "abstract_unit" require "fixtures/person" -require "rails/log_subscriber/test_helper" -require "active_resource/railties/log_subscriber" +require "active_support/log_subscriber/test_helper" +require "active_resource/log_subscriber" require "active_support/core_ext/hash/conversions" -# TODO: This test should be part of Railties class LogSubscriberTest < ActiveSupport::TestCase - include Rails::LogSubscriber::TestHelper + include ActiveSupport::LogSubscriber::TestHelper def setup super @@ -16,7 +15,7 @@ class LogSubscriberTest < ActiveSupport::TestCase mock.get "/people/1.xml", {}, @matz end - Rails::LogSubscriber.add(:active_resource, ActiveResource::Railties::LogSubscriber.new) + ActiveResource::LogSubscriber.attach_to :active_resource end def set_logger(logger) |