From 7cf445d3bdc466f26b4a1929822ccd34daac19a7 Mon Sep 17 00:00:00 2001 From: Eugene Kenny Date: Sun, 4 Aug 2019 00:35:49 +0100 Subject: Use media_type instead of content_type internally These calls to `content_type` were triggering the deprecation from c631e8d011a7cf3e7ade4e9e8db56d2b89bd530c in upgraded applications. We can use `media_type` in all of these cases to avoid the deprecation. --- actionpack/lib/action_controller/metal/renderers.rb | 6 +++--- .../metal/request_forgery_protection.rb | 2 +- actionpack/test/controller/render_js_test.rb | 9 +++++++++ actionpack/test/controller/render_json_test.rb | 18 ++++++++++++++++++ actionpack/test/controller/render_xml_test.rb | 9 +++++++++ .../test/controller/request_forgery_protection_test.rb | 9 +++++++++ 6 files changed, 49 insertions(+), 4 deletions(-) diff --git a/actionpack/lib/action_controller/metal/renderers.rb b/actionpack/lib/action_controller/metal/renderers.rb index a251c29d23..660aef4106 100644 --- a/actionpack/lib/action_controller/metal/renderers.rb +++ b/actionpack/lib/action_controller/metal/renderers.rb @@ -163,18 +163,18 @@ module ActionController "/**/#{options[:callback]}(#{json})" else - self.content_type ||= Mime[:json] + self.content_type = Mime[:json] if media_type.nil? json end end add :js do |js, options| - self.content_type ||= Mime[:js] + self.content_type = Mime[:js] if media_type.nil? js.respond_to?(:to_js) ? js.to_js(options) : js end add :xml do |xml, options| - self.content_type ||= Mime[:xml] + self.content_type = Mime[:xml] if media_type.nil? xml.respond_to?(:to_xml) ? xml.to_xml(options) : xml end end diff --git a/actionpack/lib/action_controller/metal/request_forgery_protection.rb b/actionpack/lib/action_controller/metal/request_forgery_protection.rb index e923afb17c..31df6cea0f 100644 --- a/actionpack/lib/action_controller/metal/request_forgery_protection.rb +++ b/actionpack/lib/action_controller/metal/request_forgery_protection.rb @@ -280,7 +280,7 @@ module ActionController #:nodoc: # Check for cross-origin JavaScript responses. def non_xhr_javascript_response? # :doc: - %r(\A(?:text|application)/javascript).match?(content_type) && !request.xhr? + %r(\A(?:text|application)/javascript).match?(media_type) && !request.xhr? end AUTHENTICITY_TOKEN_LENGTH = 32 diff --git a/actionpack/test/controller/render_js_test.rb b/actionpack/test/controller/render_js_test.rb index da8f6e8062..02bc0c6ade 100644 --- a/actionpack/test/controller/render_js_test.rb +++ b/actionpack/test/controller/render_js_test.rb @@ -32,4 +32,13 @@ class RenderJSTest < ActionController::TestCase get :show_partial, format: "js", xhr: true assert_equal "partial js", @response.body end + + def test_should_not_trigger_content_type_deprecation + original = ActionDispatch::Response.return_only_media_type_on_content_type + ActionDispatch::Response.return_only_media_type_on_content_type = true + + assert_not_deprecated { get :render_vanilla_js_hello, xhr: true } + ensure + ActionDispatch::Response.return_only_media_type_on_content_type = original + end end diff --git a/actionpack/test/controller/render_json_test.rb b/actionpack/test/controller/render_json_test.rb index 82c6aaafe5..0045d2be34 100644 --- a/actionpack/test/controller/render_json_test.rb +++ b/actionpack/test/controller/render_json_test.rb @@ -133,4 +133,22 @@ class RenderJsonTest < ActionController::TestCase get :render_json_without_options assert_equal '{"a":"b"}', @response.body end + + def test_should_not_trigger_content_type_deprecation + original = ActionDispatch::Response.return_only_media_type_on_content_type + ActionDispatch::Response.return_only_media_type_on_content_type = true + + assert_not_deprecated { get :render_json_hello_world } + ensure + ActionDispatch::Response.return_only_media_type_on_content_type = original + end + + def test_should_not_trigger_content_type_deprecation_with_callback + original = ActionDispatch::Response.return_only_media_type_on_content_type + ActionDispatch::Response.return_only_media_type_on_content_type = true + + assert_not_deprecated { get :render_json_hello_world_with_callback, xhr: true } + ensure + ActionDispatch::Response.return_only_media_type_on_content_type = original + end end diff --git a/actionpack/test/controller/render_xml_test.rb b/actionpack/test/controller/render_xml_test.rb index 28d8e281ab..16da41e7c7 100644 --- a/actionpack/test/controller/render_xml_test.rb +++ b/actionpack/test/controller/render_xml_test.rb @@ -98,4 +98,13 @@ class RenderXmlTest < ActionController::TestCase get :implicit_content_type, format: "atom" assert_equal Mime[:atom], @response.media_type end + + def test_should_not_trigger_content_type_deprecation + original = ActionDispatch::Response.return_only_media_type_on_content_type + ActionDispatch::Response.return_only_media_type_on_content_type = true + + assert_not_deprecated { get :render_with_to_xml } + ensure + ActionDispatch::Response.return_only_media_type_on_content_type = original + end end diff --git a/actionpack/test/controller/request_forgery_protection_test.rb b/actionpack/test/controller/request_forgery_protection_test.rb index 01250880f5..145d04f5be 100644 --- a/actionpack/test/controller/request_forgery_protection_test.rb +++ b/actionpack/test/controller/request_forgery_protection_test.rb @@ -591,6 +591,15 @@ module RequestForgeryProtectionTests end end + def test_should_not_trigger_content_type_deprecation + original = ActionDispatch::Response.return_only_media_type_on_content_type + ActionDispatch::Response.return_only_media_type_on_content_type = true + + assert_not_deprecated { get :same_origin_js, xhr: true } + ensure + ActionDispatch::Response.return_only_media_type_on_content_type = original + end + def test_should_not_raise_error_if_token_is_not_a_string assert_blocked do patch :index, params: { custom_authenticity_token: { foo: "bar" } } -- cgit v1.2.3