From b8c2f3c7692867dfd444d60efb18570ad0fba236 Mon Sep 17 00:00:00 2001 From: Yves Senn Date: Sun, 30 Dec 2012 22:49:46 +0100 Subject: [PATCH] charset should not be appended for `head` responses 1) Failure: test_head_created_with_image_png_content_type(RenderTest) [test/controller/render_test.rb:1238]: Expected: "image/png" Actual: "image/png; charset=utf-8" --- actionpack/CHANGELOG.md | 6 ++++++ actionpack/lib/action_controller/metal/head.rb | 1 + actionpack/lib/action_dispatch/http/response.rb | 8 ++++++-- actionpack/test/controller/render_test.rb | 13 ++++++++++++- 4 files changed, 25 insertions(+), 3 deletions(-) diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index 57b8b5dfc9..d2380c0881 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,5 +1,11 @@ ## Rails 4.0.0 (unreleased) ## +* Do not append `charset=` parameter when `head` is called with a + `:content_type` option. + Fix #8661. + + *Yves Senn* + * Added `Mime::NullType` class. This allows to use html?, xml?, json?..etc when the `format` of `request` is unknown, without raise an exception. diff --git a/actionpack/lib/action_controller/metal/head.rb b/actionpack/lib/action_controller/metal/head.rb index bbace49fd9..8237db15ca 100644 --- a/actionpack/lib/action_controller/metal/head.rb +++ b/actionpack/lib/action_controller/metal/head.rb @@ -31,6 +31,7 @@ module ActionController if include_content?(self.status) self.content_type = content_type || (Mime[formats.first] if formats) + self.response.charset = false if self.response self.response_body = " " else headers.delete('Content-Type') diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb index 0f808ac9cf..91cf4784db 100644 --- a/actionpack/lib/action_dispatch/http/response.rb +++ b/actionpack/lib/action_dispatch/http/response.rb @@ -260,14 +260,18 @@ module ActionDispatch # :nodoc: return if headers[CONTENT_TYPE].present? @content_type ||= Mime::HTML - @charset ||= self.class.default_charset + @charset ||= self.class.default_charset unless @charset == false type = @content_type.to_s.dup - type << "; charset=#{@charset}" unless @sending_file + type << "; charset=#{@charset}" if append_charset? headers[CONTENT_TYPE] = type end + def append_charset? + !@sending_file && @charset != false + end + def rack_response(status, header) assign_default_content_type_and_charset!(header) handle_conditional_get! diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 7640bc12a2..b5b5eadd7b 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -531,6 +531,10 @@ class TestController < ActionController::Base head :created, :content_type => "application/json" end + def head_ok_with_image_png_content_type + head :ok, :content_type => "image/png" + end + def head_with_location_header head :location => "/foo" end @@ -1224,10 +1228,17 @@ class RenderTest < ActionController::TestCase def test_head_created_with_application_json_content_type post :head_created_with_application_json_content_type assert_blank @response.body - assert_equal "application/json", @response.content_type + assert_equal "application/json", @response.header["Content-Type"] assert_response :created end + def test_head_ok_with_image_png_content_type + post :head_ok_with_image_png_content_type + assert_blank @response.body + assert_equal "image/png", @response.header["Content-Type"] + assert_response :ok + end + def test_head_with_location_header get :head_with_location_header assert_blank @response.body