1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Make sure ActiveResource::Errors#from_json doesn't pass nil to #from_array [#3650 state:commited]

Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
David Trasbo 2010-06-26 19:16:17 +02:00 committed by José Valim
parent 7eb5766bd1
commit df083b482d
2 changed files with 13 additions and 2 deletions

View file

@ -27,7 +27,7 @@ module ActiveResource
# Grabs errors from a json response.
def from_json(json, save_cache = false)
array = ActiveSupport::JSON.decode(json)['errors'] rescue []
array = Array.wrap(ActiveSupport::JSON.decode(json)['errors']) rescue []
from_array array, save_cache
end

View file

@ -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