From b09b2a8401c18d1efff21b3919ac280470a6eb8b Mon Sep 17 00:00:00 2001 From: Denis Odorcic Date: Fri, 16 Sep 2011 11:04:12 -0400 Subject: Fix ActiveResource JSON error parser and incorrect tests --- activeresource/test/cases/base_errors_test.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) (limited to 'activeresource/test/cases/base_errors_test.rb') diff --git a/activeresource/test/cases/base_errors_test.rb b/activeresource/test/cases/base_errors_test.rb index 5063916d10..a1766cdb21 100644 --- a/activeresource/test/cases/base_errors_test.rb +++ b/activeresource/test/cases/base_errors_test.rb @@ -5,7 +5,7 @@ class BaseErrorsTest < Test::Unit::TestCase def setup 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, {'Content-Type' => 'application/xml; charset=utf-8'} - 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, {'Content-Type' => 'application/json; charset=utf-8'} + mock.post "/people.json", {}, %q({"errors":{"age":["can't be blank"],"name":["can't be blank", "must start with a letter"],"person":["quota full for today."]}}), 422, {'Content-Type' => 'application/json; charset=utf-8'} end end @@ -83,7 +83,7 @@ class BaseErrorsTest < Test::Unit::TestCase 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, {} + mock.post "/people.json", {}, %q({"errors":{"age":["can't be blank"],"name":["can't be blank", "must start with a letter"],"person":["quota full for today."]}}), 422, {} end [ :json, :xml ].each do |format| -- cgit v1.2.3 From 02859745cc0db02fcb1af27da345456a3b5e1eb2 Mon Sep 17 00:00:00 2001 From: Matt Jones Date: Sat, 4 Feb 2012 16:02:52 -0600 Subject: add handling for backwards-compatibility and update documentation --- activeresource/test/cases/base_errors_test.rb | 30 +++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'activeresource/test/cases/base_errors_test.rb') diff --git a/activeresource/test/cases/base_errors_test.rb b/activeresource/test/cases/base_errors_test.rb index 98fef5fa73..88ac2de96e 100644 --- a/activeresource/test/cases/base_errors_test.rb +++ b/activeresource/test/cases/base_errors_test.rb @@ -93,6 +93,36 @@ class BaseErrorsTest < ActiveSupport::TestCase end end + def test_should_parse_json_string_errors_with_an_errors_key + ActiveResource::HttpMock.respond_to do |mock| + 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, {'Content-Type' => 'application/json; charset=utf-8'} + end + + assert_deprecated(/as an array/) do + invalid_user_using_format(:json) do + assert @person.errors[:name].any? + assert_equal ["can't be blank"], @person.errors[:age] + assert_equal ["can't be blank", "must start with a letter"], @person.errors[:name] + assert_equal ["Person quota full for today."], @person.errors[:base] + end + end + end + + def test_should_parse_3_1_style_json_errors + ActiveResource::HttpMock.respond_to do |mock| + mock.post "/people.json", {}, %q({"age":["can't be blank"],"name":["can't be blank", "must start with a letter"],"person":["quota full for today."]}), 422, {'Content-Type' => 'application/json; charset=utf-8'} + end + + assert_deprecated(/without a root/) do + invalid_user_using_format(:json) do + assert @person.errors[:name].any? + assert_equal ["can't be blank"], @person.errors[:age] + assert_equal ["can't be blank", "must start with a letter"], @person.errors[:name] + assert_equal ["Person quota full for today."], @person.errors[:base] + end + end + end + private def invalid_user_using_format(mime_type_reference) previous_format = Person.format -- cgit v1.2.3