From bafd698acb160fd241efe6f027b83676b9e80d1f Mon Sep 17 00:00:00 2001 From: Jeremy Kemper Date: Sun, 9 Dec 2007 22:11:37 +0000 Subject: render :xml and :json preserve custom content types. Closes #10388. git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@8342 5ecf4fe2-1ee6-0310-87b1-e25e094e27de --- actionpack/CHANGELOG | 2 ++ actionpack/lib/action_controller/base.rb | 4 ++-- actionpack/test/controller/render_test.rb | 40 +++++++++++++++++++++++-------- 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/actionpack/CHANGELOG b/actionpack/CHANGELOG index e0b667969c..fd15879424 100644 --- a/actionpack/CHANGELOG +++ b/actionpack/CHANGELOG @@ -1,5 +1,7 @@ *SVN* +* render :xml and :json preserve custom content types. #10388 [jmettraux, Chu Yeow] + * Refactor Action View template handlers. #10437 [Josh Peek] * Fix DoubleRenderError message and leave out mention of returning false from filters. Closes #10380 [fcheung] diff --git a/actionpack/lib/action_controller/base.rb b/actionpack/lib/action_controller/base.rb index b5c4780f52..f1d416d664 100755 --- a/actionpack/lib/action_controller/base.rb +++ b/actionpack/lib/action_controller/base.rb @@ -873,13 +873,13 @@ module ActionController #:nodoc: end elsif xml = options[:xml] - response.content_type = Mime::XML + response.content_type ||= Mime::XML render_for_text(xml.respond_to?(:to_xml) ? xml.to_xml : xml, options[:status]) elsif json = options[:json] json = json.to_json unless json.is_a?(String) json = "#{options[:callback]}(#{json})" unless options[:callback].blank? - response.content_type = Mime::JSON + response.content_type ||= Mime::JSON render_for_text(json, options[:status]) elsif partial = options[:partial] diff --git a/actionpack/test/controller/render_test.rb b/actionpack/test/controller/render_test.rb index 7765c62a5e..d3a9ec195c 100644 --- a/actionpack/test/controller/render_test.rb +++ b/actionpack/test/controller/render_test.rb @@ -44,6 +44,10 @@ class TestController < ActionController::Base render :json => {:hello => 'world'}.to_json, :callback => 'alert' end + def render_json_with_custom_content_type + render :json => {:hello => 'world'}.to_json, :content_type => 'text/javascript' + end + def render_symbol_json render :json => {:hello => 'world'}.to_json end @@ -65,6 +69,10 @@ class TestController < ActionController::Base render :template => "test/hello" end + def render_xml_with_custom_content_type + render :xml => "", :content_type => "application/atomsvc+xml" + end + def heading head :ok end @@ -199,56 +207,62 @@ class RenderTest < Test::Unit::TestCase assert_template "test/hello_world" end - def test_do_with_render + def test_render get :render_hello_world assert_template "test/hello_world" end - def test_do_with_render_from_variable + def test_render_from_variable get :render_hello_world_from_variable assert_equal "hello david", @response.body end - def test_do_with_render_action + def test_render_action get :render_action_hello_world assert_template "test/hello_world" end - def test_do_with_render_action_with_symbol + def test_render_action_with_symbol get :render_action_hello_world_with_symbol assert_template "test/hello_world" end - def test_do_with_render_text + def test_render_text get :render_text_hello_world assert_equal "hello world", @response.body end - def test_do_with_render_json + def test_render_json get :render_json_hello_world assert_equal '{"hello": "world"}', @response.body assert_equal 'application/json', @response.content_type end - def test_do_with_render_json_with_callback + def test_render_json_with_callback get :render_json_hello_world_with_callback assert_equal 'alert({"hello": "world"})', @response.body assert_equal 'application/json', @response.content_type end - def test_do_with_render_symbol_json + def test_render_json_with_custom_content_type + get :render_json_with_custom_content_type + assert_equal '{"hello": "world"}', @response.body + assert_equal 'text/javascript', @response.content_type + end + + def test_render_symbol_json get :render_symbol_json assert_equal '{"hello": "world"}', @response.body assert_equal 'application/json', @response.content_type end - def test_do_with_render_custom_code + def test_render_custom_code get :render_custom_code assert_response 404 assert_equal 'hello world', @response.body end - def test_do_with_render_nothing_with_appendix + def test_render_nothing_with_appendix get :render_nothing_with_appendix assert_response 200 assert_equal 'appended', @response.body @@ -269,6 +283,7 @@ class RenderTest < Test::Unit::TestCase def test_render_xml get :render_xml_hello assert_equal "\n

Hello David

\n

This is grand!

\n\n", @response.body + assert_equal "application/xml", @response.content_type end def test_render_xml_with_default @@ -435,6 +450,11 @@ class RenderTest < Test::Unit::TestCase assert_equal %(Element.replace("foo", "partial html");), @response.body end + def test_should_render_xml_but_keep_custom_content_type + get :render_xml_with_custom_content_type + assert_equal "application/atomsvc+xml", @response.content_type + end + protected def etag_for(text) -- cgit v1.2.3