From ea5f509643d6d9c468a9b26f6c45bd4e40fd67cf Mon Sep 17 00:00:00 2001 From: "yuuji.yaginuma" Date: Wed, 17 Apr 2019 15:37:16 +0900 Subject: Change `ActionDispatch::Response#content_type` returning Content-Type header as it is MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Since #35709, `Response#conten_type` returns only MIME type correctly. It is a documented behavior that this method only returns MIME type, so this change seems appropriate. https://github.com/rails/rails/blob/39de7fac0507070e3c5f8b33fbad6fced84d97ed/actionpack/lib/action_dispatch/http/response.rb#L245-L249 But unfortunately, some users expect this method to return all Content-Type that does not contain charset. This seems to be breaking changes. We can change this behavior with the deprecate cycle. But, in that case, a method needs that include Content-Type with additional parameters. And that method name is probably the `content_type` seems to properly. So I changed the new behavior to more appropriate `media_type` method. And `Response#content_type` changed (as the method name) to return Content-Type header as it is. Fixes #35709. [Rafael Mendonça França & Yuuji Yaginuma ] --- guides/source/6_0_release_notes.md | 4 ++++ guides/source/testing.md | 2 +- guides/source/upgrading_ruby_on_rails.md | 22 ++++++++++++++++++++++ 3 files changed, 27 insertions(+), 1 deletion(-) (limited to 'guides') diff --git a/guides/source/6_0_release_notes.md b/guides/source/6_0_release_notes.md index fa45e7240d..e421ae1ac7 100644 --- a/guides/source/6_0_release_notes.md +++ b/guides/source/6_0_release_notes.md @@ -215,6 +215,10 @@ Please refer to the [Changelog][action-pack] for detailed changes. ### Notable changes +* Change `ActionDispatch::Response#content_type` returning Content-Type + header as it is. + ([Pull Request](https://github.com/rails/rails/pull/36034)) + * Raise an `ArgumentError` if a resource param contains a colon. ([Pull Request](https://github.com/rails/rails/pull/35236)) diff --git a/guides/source/testing.md b/guides/source/testing.md index 9540bb2af5..41bc54b924 100644 --- a/guides/source/testing.md +++ b/guides/source/testing.md @@ -1144,7 +1144,7 @@ test "ajax request" do get article_url(article), xhr: true assert_equal 'hello world', @response.body - assert_equal "text/javascript", @response.content_type + assert_equal "text/javascript", @response.media_type end ``` diff --git a/guides/source/upgrading_ruby_on_rails.md b/guides/source/upgrading_ruby_on_rails.md index b8a5c39f39..1110592d5e 100644 --- a/guides/source/upgrading_ruby_on_rails.md +++ b/guides/source/upgrading_ruby_on_rails.md @@ -134,6 +134,28 @@ Action Cable JavaScript API: + ActionCable.logger.enabled = false ``` +### `ActionDispatch::Response#content_type` now returned Content-Type header as it is. + +Previously, `ActionDispatch::Response#content_type` returned value does NOT contain charset part. +This behavior changed to returned Content-Type header containing charset part as it is. + +If you want just MIME type, please use `ActionDispatch::Response#media_type` instead. + +Before: + +```ruby +resp = ActionDispatch::Response.new(200, "Content-Type" => "text/csv; header=present; charset=utf-16") +resp.content_type #=> "text/csv; header=present" +``` + +After: + +```ruby +resp = ActionDispatch::Response.new(200, "Content-Type" => "text/csv; header=present; charset=utf-16") +resp.content_type #=> "text/csv; header=present; charset=utf-16" +resp.media_type #=> "text/csv" +``` + ### Autoloading The default configuration for Rails 6 -- cgit v1.2.3