From 5a8a550a45c5ca7abc9785ed180d5f46189c9958 Mon Sep 17 00:00:00 2001
From: Yehuda Katz + Carl Lerche <ykatz+clerche@engineyard.com>
Date: Mon, 15 Jun 2009 11:21:08 -0700
Subject: Finish making things pass with updated internal content_type
 semantics

---
 .../lib/action_controller/base/mime_responds.rb    |  2 +-
 actionpack/lib/action_controller/base/streaming.rb |  9 ++--
 .../action_controller/new_base/rack_convenience.rb |  2 +-
 .../lib/action_controller/testing/process.rb       |  2 +
 actionpack/lib/action_dispatch/http/response.rb    | 56 +++++-----------------
 actionpack/test/controller/content_type_test.rb    |  4 +-
 actionpack/test/controller/send_file_test.rb       | 10 ++--
 7 files changed, 26 insertions(+), 59 deletions(-)

(limited to 'actionpack')

diff --git a/actionpack/lib/action_controller/base/mime_responds.rb b/actionpack/lib/action_controller/base/mime_responds.rb
index 3c17dda1a1..5c7218691e 100644
--- a/actionpack/lib/action_controller/base/mime_responds.rb
+++ b/actionpack/lib/action_controller/base/mime_responds.rb
@@ -125,7 +125,7 @@ module ActionController #:nodoc:
           end
 
           @controller.template.formats = [mime_type.to_sym]
-          @response.content_type = mime_type.to_s
+          @response.content_type = mime_type
 
           block_given? ? block.call : @controller.send(:render, :action => @controller.action_name)
         end
diff --git a/actionpack/lib/action_controller/base/streaming.rb b/actionpack/lib/action_controller/base/streaming.rb
index 73d4bde6c1..5c72fc9ad9 100644
--- a/actionpack/lib/action_controller/base/streaming.rb
+++ b/actionpack/lib/action_controller/base/streaming.rb
@@ -162,15 +162,16 @@ module ActionController #:nodoc:
         disposition <<= %(; filename="#{options[:filename]}") if options[:filename]
 
         content_type = options[:type]
+
         if content_type.is_a?(Symbol)
-          raise ArgumentError, "Unknown MIME type #{options[:type]}" unless Mime::EXTENSION_LOOKUP.has_key?(content_type.to_s)
-          content_type = Mime::Type.lookup_by_extension(content_type.to_s)
+          raise ArgumentError, "Unknown MIME type #{options[:type]}" unless Mime::EXTENSION_LOOKUP.key?(content_type.to_s)
+          self.content_type = Mime::Type.lookup_by_extension(content_type.to_s)
+        else
+          self.content_type = content_type
         end
-        content_type = content_type.to_s.strip # fixes a problem with extra '\r' with some browsers
 
         headers.merge!(
           'Content-Length'            => options[:length],
-          'Content-Type'              => content_type,
           'Content-Disposition'       => disposition,
           'Content-Transfer-Encoding' => 'binary'
         )
diff --git a/actionpack/lib/action_controller/new_base/rack_convenience.rb b/actionpack/lib/action_controller/new_base/rack_convenience.rb
index 5dfa7d12f3..805157b0e3 100644
--- a/actionpack/lib/action_controller/new_base/rack_convenience.rb
+++ b/actionpack/lib/action_controller/new_base/rack_convenience.rb
@@ -3,7 +3,7 @@ module ActionController
     extend ActiveSupport::Concern
 
     included do
-      delegate :headers, :status=, :location=,
+      delegate :headers, :status=, :location=, :content_type=,
                :status, :location, :content_type, :to => "@_response"
       attr_internal :request, :response
     end
diff --git a/actionpack/lib/action_controller/testing/process.rb b/actionpack/lib/action_controller/testing/process.rb
index 9647f8ce45..ea5752d4f5 100644
--- a/actionpack/lib/action_controller/testing/process.rb
+++ b/actionpack/lib/action_controller/testing/process.rb
@@ -56,6 +56,8 @@ module ActionController #:nodoc:
       @block = nil
       @length = 0
       @body = []
+      @charset = nil
+      @content_type = nil
 
       @request = @template = nil
     end
diff --git a/actionpack/lib/action_dispatch/http/response.rb b/actionpack/lib/action_dispatch/http/response.rb
index b9db7a4508..e58b4b5e19 100644
--- a/actionpack/lib/action_dispatch/http/response.rb
+++ b/actionpack/lib/action_dispatch/http/response.rb
@@ -67,12 +67,7 @@ module ActionDispatch # :nodoc:
     end
 
     def body=(body)
-      @body =
-        if body.respond_to?(:to_str)
-          [body]
-        else
-          body
-        end
+      @body = body.respond_to?(:to_str) ? [body] : body
     end
 
     def body_parts
@@ -96,36 +91,7 @@ module ActionDispatch # :nodoc:
     # If a character set has been defined for this response (see charset=) then
     # the character set information will also be included in the content type
     # information.
-    def content_type=(mime_type)
-      self.headers["Content-Type"] =
-        if mime_type =~ /charset/ || (c = charset).nil?
-          mime_type.to_s
-        else
-          "#{mime_type}; charset=#{c}"
-        end
-    end
-
-    # Returns the response's content MIME type, or nil if content type has been set.
-    def content_type
-      content_type = String(headers["Content-Type"] || headers["type"]).split(";")[0]
-      content_type.blank? ? nil : content_type
-    end
-
-    # Set the charset of the Content-Type header. Set to nil to remove it.
-    # If no content type is set, it defaults to HTML.
-    def charset=(charset)
-      headers["Content-Type"] =
-        if charset
-          "#{content_type || Mime::HTML}; charset=#{charset}"
-        else
-          content_type || Mime::HTML.to_s
-        end
-    end
-
-    def charset
-      charset = String(headers["Content-Type"] || headers["type"]).split(";")[1]
-      charset.blank? ? nil : charset.strip.split("=")[1]
-    end
+    attr_accessor :charset, :content_type
 
     def last_modified
       if last = headers['Last-Modified']
@@ -162,15 +128,15 @@ module ActionDispatch # :nodoc:
     end
 
     def assign_default_content_type_and_charset!
-      if type = headers['Content-Type'] || headers['type']
-        unless type =~ /charset=/ || sending_file?
-          headers['Content-Type'] = "#{type}; charset=#{default_charset}"
-        end
-      else
-        type = Mime::HTML.to_s
-        type += "; charset=#{default_charset}" unless sending_file?
-        headers['Content-Type'] = type
-      end
+      return if !headers["Content-Type"].blank?
+
+      @content_type ||= Mime::HTML
+      @charset      ||= default_charset
+
+      type = @content_type.to_s.dup
+      type << "; charset=#{@charset}" unless sending_file?
+
+      headers["Content-Type"] = type
     end
 
     def prepare!
diff --git a/actionpack/test/controller/content_type_test.rb b/actionpack/test/controller/content_type_test.rb
index d622ac1e85..511788aec8 100644
--- a/actionpack/test/controller/content_type_test.rb
+++ b/actionpack/test/controller/content_type_test.rb
@@ -83,14 +83,14 @@ class ContentTypeTest < ActionController::TestCase
   # :ported:
   def test_content_type_from_body
     get :render_content_type_from_body
-    assert_equal "application/rss+xml", @response.content_type
+    assert_equal Mime::RSS, @response.content_type
     assert_equal "utf-8", @response.charset
   end
 
   # :ported:
   def test_content_type_from_render
     get :render_content_type_from_render
-    assert_equal "application/rss+xml", @response.content_type
+    assert_equal Mime::RSS, @response.content_type
     assert_equal "utf-8", @response.charset
   end
 
diff --git a/actionpack/test/controller/send_file_test.rb b/actionpack/test/controller/send_file_test.rb
index 0bc0eb2df6..4134da3b9e 100644
--- a/actionpack/test/controller/send_file_test.rb
+++ b/actionpack/test/controller/send_file_test.rb
@@ -91,10 +91,10 @@ class SendFileTest < ActionController::TestCase
 
   def test_headers_after_send_shouldnt_include_charset
     response = process('data')
-    assert_equal "application/octet-stream", response.content_type
+    assert_equal "application/octet-stream", response.headers["Content-Type"]
 
     response = process('file')
-    assert_equal "application/octet-stream", response.content_type
+    assert_equal "application/octet-stream", response.headers["Content-Type"]
   end
 
   # Test that send_file_headers! is setting the correct HTTP headers.
@@ -116,7 +116,7 @@ class SendFileTest < ActionController::TestCase
 
     h = @controller.headers
     assert_equal 1, h['Content-Length']
-    assert_equal 'image/png', h['Content-Type']
+    assert_equal 'image/png', @controller.content_type
     assert_equal 'disposition; filename="filename"', h['Content-Disposition']
     assert_equal 'binary', h['Content-Transfer-Encoding']
 
@@ -136,9 +136,7 @@ class SendFileTest < ActionController::TestCase
     @controller.headers = {}
     @controller.send(:send_file_headers!, options)
 
-    headers = @controller.headers
-
-    assert_equal 'image/png', headers['Content-Type']
+    assert_equal 'image/png', @controller.content_type
   end
   
 
-- 
cgit v1.2.3