From 2caf4d5a9337591c2728c7db826526febc5b2158 Mon Sep 17 00:00:00 2001 From: David Heinemeier Hansson Date: Sun, 17 Sep 2006 16:20:32 +0000 Subject: Added proper getters and setters for content type and charset [DHH] Added utf-8 as the default charset for all renders. You can change this default using ActionController::Base.default_charset=(encoding) [DHH] git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@5129 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- .../test/controller/action_pack_assertions_test.rb | 6 +- actionpack/test/controller/content_type_test.rb | 99 ++++++++++++++++++++++ actionpack/test/controller/new_render_test.rb | 9 +- 3 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 actionpack/test/controller/content_type_test.rb (limited to 'actionpack/test/controller') diff --git a/actionpack/test/controller/action_pack_assertions_test.rb b/actionpack/test/controller/action_pack_assertions_test.rb index a1eea400f2..6e1ccba50e 100644 --- a/actionpack/test/controller/action_pack_assertions_test.rb +++ b/actionpack/test/controller/action_pack_assertions_test.rb @@ -568,18 +568,18 @@ class ActionPackHeaderTest < Test::Unit::TestCase def test_rendering_xml_sets_content_type assert_deprecated(/render/) { process :hello_xml_world } - assert_equal('application/xml', @controller.headers['Content-Type']) + assert_equal('application/xml; charset=utf-8', @controller.headers['Content-Type']) end def test_rendering_xml_respects_content_type @response.headers['Content-Type'] = 'application/pdf' assert_deprecated(/render/) { process :hello_xml_world } - assert_equal('application/pdf', @controller.headers['Content-Type']) + assert_equal('application/pdf; charset=utf-8', @controller.headers['Content-Type']) end def test_render_text_with_custom_content_type get :render_text_with_custom_content_type - assert_equal 'application/rss+xml', @response.headers['Content-Type'] + assert_equal 'application/rss+xml; charset=utf-8', @response.headers['Content-Type'] end end diff --git a/actionpack/test/controller/content_type_test.rb b/actionpack/test/controller/content_type_test.rb new file mode 100644 index 0000000000..71563ba9a6 --- /dev/null +++ b/actionpack/test/controller/content_type_test.rb @@ -0,0 +1,99 @@ +require File.dirname(__FILE__) + '/../abstract_unit' + +class ContentTypeController < ActionController::Base + def render_content_type_from_body + response.content_type = Mime::RSS + render :text => "hello world!" + end + + def render_defaults + render :text => "hello world!" + end + + def render_content_type_from_render + render :text => "hello world!", :content_type => Mime::RSS + end + + def render_charset_from_body + response.charset = "utf-16" + render :text => "hello world!" + end + + def render_default_for_rhtml + end + + def render_default_for_rxml + end + + def render_change_for_rxml + response.content_type = Mime::HTML + render :action => "render_default_for_rxml" + end + + def rescue_action(e) raise end +end + +ContentTypeController.template_root = File.dirname(__FILE__) + "/../fixtures/" + +class ContentTypeTest < Test::Unit::TestCase + def setup + @controller = ContentTypeController.new + + # enable a logger so that (e.g.) the benchmarking stuff runs, so we can get + # a more accurate simulation of what happens in "real life". + @controller.logger = Logger.new(nil) + + @request = ActionController::TestRequest.new + @response = ActionController::TestResponse.new + end + + def test_render_defaults + get :render_defaults + assert_equal "utf-8", @response.charset + assert_equal Mime::HTML, @response.content_type + end + + def test_render_changed_charset_default + ContentTypeController.default_charset = "utf-16" + get :render_defaults + assert_equal "utf-16", @response.charset + assert_equal Mime::HTML, @response.content_type + ContentTypeController.default_charset = "utf-8" + end + + def test_content_type_from_body + get :render_content_type_from_body + assert_equal "application/rss+xml", @response.content_type + assert_equal "utf-8", @response.charset + end + + def test_content_type_from_render + get :render_content_type_from_render + assert_equal "application/rss+xml", @response.content_type + assert_equal "utf-8", @response.charset + end + + def test_charset_from_body + get :render_charset_from_body + assert_equal "utf-16", @response.charset + assert_equal Mime::HTML, @response.content_type + end + + def test_default_for_rhtml + get :render_default_for_rhtml + assert_equal Mime::HTML, @response.content_type + assert_equal "utf-8", @response.charset + end + + def test_default_for_rxml + get :render_default_for_rxml + assert_equal Mime::XML, @response.content_type + assert_equal "utf-8", @response.charset + end + + def test_change_for_rxml + get :render_change_for_rxml + assert_equal Mime::HTML, @response.content_type + assert_equal "utf-8", @response.charset + end +end \ No newline at end of file diff --git a/actionpack/test/controller/new_render_test.rb b/actionpack/test/controller/new_render_test.rb index e06b657b07..8af6440c56 100644 --- a/actionpack/test/controller/new_render_test.rb +++ b/actionpack/test/controller/new_render_test.rb @@ -263,6 +263,11 @@ class NewRenderTestController < ActionController::Base render :action => "content_for", :layout => "yield" end + def render_content_type_from_body + response.content_type = Mime::RSS + render :text => "hello world!" + end + def rescue_action(e) raise end private @@ -572,14 +577,14 @@ EOS def test_update_page get :update_page assert_template nil - assert_equal 'text/javascript; charset=UTF-8', @response.headers['Content-Type'] + assert_equal 'text/javascript; charset=utf-8', @response.headers['Content-Type'] assert_equal 2, @response.body.split($/).length end def test_update_page_with_instance_variables get :update_page_with_instance_variables assert_template nil - assert_equal 'text/javascript; charset=UTF-8', @response.headers['Content-Type'] + assert_equal 'text/javascript; charset=utf-8', @response.headers['Content-Type'] assert_match /balance/, @response.body assert_match /\$37/, @response.body end -- cgit v1.2.3