Permalink
Browse files

backport #8662, 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"
  • Loading branch information...
1 parent d38c8ca commit 55a886b6e9bae1986a357626b08522a838aedb3e @senny senny committed Dec 30, 2012
@@ -1,5 +1,12 @@
## Rails 3.2.10 (unreleased) ##
+* Do not append `charset=` parameter when `head` is called with a
+ `:content_type` option.
+ Fix #8661.
+ Backport #8662.
+
+ *Yves Senn*
+
* Clear url helper methods when routes are reloaded by removing the methods
explicitly rather than just clearing the module because it didn't work
properly and could be the source of a memory leak.
@@ -29,6 +29,7 @@ def head(status, options = {})
self.status = status
self.location = url_for(location) if location
self.content_type = content_type || (Mime[formats.first] if formats)
+ self.response.charset = false if self.response
self.response_body = " "
end
end
@@ -56,7 +56,7 @@ class Response
CONTENT_TYPE = "Content-Type".freeze
SET_COOKIE = "Set-Cookie".freeze
LOCATION = "Location".freeze
-
+
cattr_accessor(:default_charset) { "utf-8" }
include Rack::Response::Helpers
@@ -195,12 +195,16 @@ def assign_default_content_type_and_charset!
return if headers[CONTENT_TYPE].present?
@content_type ||= Mime::HTML
- @charset ||= self.class.default_charset
+ @charset ||= self.class.default_charset if !defined?(@charset) || @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
end
end
@@ -503,6 +503,10 @@ def head_created_with_application_json_content_type
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
@@ -1197,10 +1201,17 @@ def test_head_created
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

0 comments on commit 55a886b

Please sign in to comment.