Change `ActionDispatch::Response#content_type` to return the full Content-Type header

And deprecate the config to keep the previous behavior.
This commit is contained in:
Rafael Mendonça França 2020-10-28 19:09:38 +00:00
parent 62bda903be
commit 64efe502f3
No known key found for this signature in database
GPG Key ID: FC23B6D0F1EEE948
12 changed files with 26 additions and 121 deletions

View File

@ -1,3 +1,11 @@
* Deprecate `config.action_dispatch.return_only_media_type_on_content_type`.
*Rafael Mendonça França*
* Change `ActionDispatch::Response#content_type` to return the full Content-Type header.
*Rafael Mendonça França*
* Remove deprecated `ActionDispatch::Http::ParameterFilter`.
*Rafael Mendonça França*

View File

@ -85,7 +85,18 @@ module ActionDispatch # :nodoc:
cattr_accessor :default_charset, default: "utf-8"
cattr_accessor :default_headers
cattr_accessor :return_only_media_type_on_content_type, default: false
def self.return_only_media_type_on_content_type=(*)
ActiveSupport::Deprecation.warn(
".return_only_media_type_on_content_type= is dreprecated with no replacement and will be removed in 6.2."
)
end
def self.return_only_media_type_on_content_type
ActiveSupport::Deprecation.warn(
".return_only_media_type_on_content_type is dreprecated with no replacement and will be removed in 6.2."
)
end
include Rack::Response::Helpers
# Aliasing these off because AD::Http::Cache::Response defines them.
@ -243,17 +254,7 @@ module ActionDispatch # :nodoc:
# Content type of response.
def content_type
if self.class.return_only_media_type_on_content_type
ActiveSupport::Deprecation.warn(
"Rails 6.1 will return Content-Type header without modification." \
" If you want just the MIME type, please use `#media_type` instead."
)
content_type = super
content_type ? content_type.split(/;\s*charset=/)[0].presence : content_type
else
super.presence
end
super.presence
end
# Media type of response.

View File

@ -23,7 +23,6 @@ module ActionDispatch
config.action_dispatch.use_authenticated_cookie_encryption = false
config.action_dispatch.use_cookies_with_metadata = false
config.action_dispatch.perform_deep_munge = true
config.action_dispatch.return_only_media_type_on_content_type = true
config.action_dispatch.default_headers = {
"X-Frame-Options" => "SAMEORIGIN",
@ -43,10 +42,10 @@ module ActionDispatch
ActionDispatch::Http::URL.tld_length = app.config.action_dispatch.tld_length
ActionDispatch::Request.ignore_accept_header = app.config.action_dispatch.ignore_accept_header
ActionDispatch::Request::Utils.perform_deep_munge = app.config.action_dispatch.perform_deep_munge
ActiveSupport.on_load(:action_dispatch_response) do
self.default_charset = app.config.action_dispatch.default_charset || app.config.encoding
self.default_headers = app.config.action_dispatch.default_headers
self.return_only_media_type_on_content_type = app.config.action_dispatch.return_only_media_type_on_content_type
end
ActionDispatch::ExceptionWrapper.rescue_responses.merge!(config.action_dispatch.rescue_responses)

View File

@ -32,13 +32,4 @@ 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

View File

@ -133,22 +133,4 @@ 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

View File

@ -98,13 +98,4 @@ 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

View File

@ -616,15 +616,6 @@ 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" } }

View File

@ -604,33 +604,4 @@ class ResponseIntegrationTest < ActionDispatch::IntegrationTest
assert_equal("text/csv; header=present", @response.media_type)
assert_equal("utf-16", @response.charset)
end
test "`content type` returns header that excludes `charset` when specified `return_only_media_type_on_content_type`" do
original = ActionDispatch::Response.return_only_media_type_on_content_type
ActionDispatch::Response.return_only_media_type_on_content_type = true
@app = lambda { |env|
if env["PATH_INFO"] == "/with_parameters"
[200, { "Content-Type" => "text/csv; header=present; charset=utf-16" }, [""]]
else
[200, { "Content-Type" => "text/csv; charset=utf-16" }, [""]]
end
}
get "/"
assert_response :success
assert_deprecated do
assert_equal("text/csv", @response.content_type)
end
get "/with_parameters"
assert_response :success
assert_deprecated do
assert_equal("text/csv; header=present", @response.content_type)
end
ensure
ActionDispatch::Response.return_only_media_type_on_content_type = original
end
end

View File

@ -61,8 +61,12 @@ Please refer to the [Changelog][action-pack] for detailed changes.
### Deprecations
* Deprecate `config.action_dispatch.return_only_media_type_on_content_type`.
### Notable changes
* Change `ActionDispatch::Response#content_type` to return the full Content-Type header.
Action View
-----------

View File

@ -625,10 +625,6 @@ Defaults to `'signed cookie'`.
Any exceptions that are not configured will be mapped to 500 Internal Server Error.
* `config.action_dispatch.return_only_media_type_on_content_type` change the
return value of `ActionDispatch::Response#content_type` to the Content-Type
header without modification. Defaults to `false`.
* `config.action_dispatch.cookies_same_site_protection` configures the default
value of the `SameSite` attribute when setting cookies. When set to `nil`, the
`SameSite` attribute is not added. To allow the value of the `SameSite` attribute
@ -1024,7 +1020,6 @@ text/javascript image/svg+xml application/postscript application/x-shockwave-fla
- `config.autoloader`: `:zeitwerk`
- `config.action_view.default_enforce_utf8`: `false`
- `config.action_dispatch.use_cookies_with_metadata`: `true`
- `config.action_dispatch.return_only_media_type_on_content_type`: `false`
- `config.action_mailer.delivery_job`: `"ActionMailer::MailDeliveryJob"`
- `config.active_storage.queues.analysis`: `:active_storage_analysis`
- `config.active_storage.queues.purge`: `:active_storage_purge`

View File

@ -136,7 +136,6 @@ module Rails
if respond_to?(:action_dispatch)
action_dispatch.use_cookies_with_metadata = true
action_dispatch.return_only_media_type_on_content_type = false
end
if respond_to?(:action_mailer)

View File

@ -2453,33 +2453,6 @@ module ApplicationTests
assert_nil ActiveStorage.queues[:purge]
end
test "ActionDispatch::Response.return_only_media_type_on_content_type is false by default" do
app "development"
assert_equal false, ActionDispatch::Response.return_only_media_type_on_content_type
end
test "ActionDispatch::Response.return_only_media_type_on_content_type is true in the 5.x defaults" do
remove_from_config '.*config\.load_defaults.*\n'
add_to_config 'config.load_defaults "5.2"'
app "development"
assert_equal true, ActionDispatch::Response.return_only_media_type_on_content_type
end
test "ActionDispatch::Response.return_only_media_type_on_content_type can be configured in the new framework defaults" do
remove_from_config '.*config\.load_defaults.*\n'
app_file "config/initializers/new_framework_defaults_6_0.rb", <<-RUBY
Rails.application.config.action_dispatch.return_only_media_type_on_content_type = false
RUBY
app "development"
assert_equal false, ActionDispatch::Response.return_only_media_type_on_content_type
end
test "ActionMailbox.logger is Rails.logger by default" do
app "development"