From c6f121eee937f9460bdbdaeea60320bc02ddacec Mon Sep 17 00:00:00 2001 From: Jatinder Singh Date: Wed, 27 Jan 2010 15:40:46 -0800 Subject: [PATCH] Use format of ARes rather than content-type of remote errors to load errors. --- activeresource/lib/active_resource/validations.rb | 6 +++--- activeresource/test/base_errors_test.rb | 13 +++++++++++++ 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/activeresource/lib/active_resource/validations.rb b/activeresource/lib/active_resource/validations.rb index 56621dd..f5aa24f 100644 --- a/activeresource/lib/active_resource/validations.rb +++ b/activeresource/lib/active_resource/validations.rb @@ -259,10 +259,10 @@ module ActiveResource save_without_validation true rescue ResourceInvalid => error - case error.response['Content-Type'] - when /xml/ + case self.class.format + when ActiveResource::Formats[:xml] errors.from_xml(error.response.body) - when /json/ + when ActiveResource::Formats[:json] errors.from_json(error.response.body) end false diff --git a/activeresource/test/base_errors_test.rb b/activeresource/test/base_errors_test.rb index 21e0b39..7183d7a 100644 --- a/activeresource/test/base_errors_test.rb +++ b/activeresource/test/base_errors_test.rb @@ -71,6 +71,19 @@ class BaseErrorsTest < Test::Unit::TestCase end end + def test_should_mark_as_invalid_when_content_type_is_unavailable_in_response_header + ActiveResource::HttpMock.respond_to do |mock| + mock.post "/people.xml", {}, %q(Age can't be blankName can't be blankName must start with a letterPerson quota full for today.), 422, {} + mock.post "/people.json", {}, %q({"errors":["Age can't be blank","Name can't be blank","Name must start with a letter","Person quota full for today."]}), 422, {} + end + + [ :json, :xml ].each do |format| + invalid_user_using_format(format) do + assert !@person.valid? + end + end + end + private def invalid_user_using_format(mime_type_reference) previous_format = Person.format -- 1.6.1.3