diff --git a/lib/active_resource/formats/json_format.rb b/lib/active_resource/formats/json_format.rb index 67795f95e3..7898e67335 100644 --- a/lib/active_resource/formats/json_format.rb +++ b/lib/active_resource/formats/json_format.rb @@ -24,5 +24,7 @@ def decode(json) Formats.remove_root(ActiveSupport::JSON.decode(json)) end end + + JSONFormat = JsonFormat end end diff --git a/lib/active_resource/formats/xml_format.rb b/lib/active_resource/formats/xml_format.rb index 5fbf967e08..daac245d44 100644 --- a/lib/active_resource/formats/xml_format.rb +++ b/lib/active_resource/formats/xml_format.rb @@ -23,5 +23,7 @@ def decode(xml) Formats.remove_root(Hash.from_xml(xml)) end end + + XMLFormat = XmlFormat end end diff --git a/test/cases/formats_test.rb b/test/cases/formats_test.rb new file mode 100644 index 0000000000..7d11c5d8af --- /dev/null +++ b/test/cases/formats_test.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require "abstract_unit" + +class FormatsTest < ActiveSupport::TestCase + def test_json_format_uses_camelcase + assert_equal ActiveResource::Formats::JsonFormat, ActiveResource::Formats[:json] + end + + def test_xml_format_uses_camelcase + assert_equal ActiveResource::Formats::XmlFormat, ActiveResource::Formats[:xml] + end + + def test_custom_format_uses_camelcase + klass = Class.new + ActiveResource::Formats.const_set(:MsgpackFormat, klass) + + assert_equal klass, ActiveResource::Formats[:msgpack] + ensure + ActiveResource::Formats.send(:remove_const, :MsgpackFormat) + end + + def test_unknown_format_raises_not_found_error + assert_raises NameError, match: "uninitialized constant ActiveResource::Formats::MsgpackFormat" do + ActiveResource::Formats[:msgpack] + end + end + + def test_json_format_uses_acronym_inflections + ActiveSupport::Inflector.inflections { |inflect| inflect.acronym "JSON" } + + assert_equal ActiveResource::Formats::JsonFormat, ActiveResource::Formats[:json] + ensure + ActiveSupport::Inflector.inflections.clear :acronyms + end + + def test_xml_format_uses_acronym_inflections + ActiveSupport::Inflector.inflections { |inflect| inflect.acronym "XML" } + + assert_equal ActiveResource::Formats::XmlFormat, ActiveResource::Formats[:xml] + ensure + ActiveSupport::Inflector.inflections.clear :acronyms + end +end