1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

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
This commit is contained in:
Jeremy Kemper 2007-12-09 22:11:37 +00:00
parent 3fab196da3
commit bafd698acb
3 changed files with 34 additions and 12 deletions

View file

@ -1,5 +1,7 @@
*SVN* *SVN*
* render :xml and :json preserve custom content types. #10388 [jmettraux, Chu Yeow]
* Refactor Action View template handlers. #10437 [Josh Peek] * Refactor Action View template handlers. #10437 [Josh Peek]
* Fix DoubleRenderError message and leave out mention of returning false from filters. Closes #10380 [fcheung] * Fix DoubleRenderError message and leave out mention of returning false from filters. Closes #10380 [fcheung]

View file

@ -873,13 +873,13 @@ module ActionController #:nodoc:
end end
elsif xml = options[:xml] 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]) render_for_text(xml.respond_to?(:to_xml) ? xml.to_xml : xml, options[:status])
elsif json = options[:json] elsif json = options[:json]
json = json.to_json unless json.is_a?(String) json = json.to_json unless json.is_a?(String)
json = "#{options[:callback]}(#{json})" unless options[:callback].blank? json = "#{options[:callback]}(#{json})" unless options[:callback].blank?
response.content_type = Mime::JSON response.content_type ||= Mime::JSON
render_for_text(json, options[:status]) render_for_text(json, options[:status])
elsif partial = options[:partial] elsif partial = options[:partial]

View file

@ -44,6 +44,10 @@ class TestController < ActionController::Base
render :json => {:hello => 'world'}.to_json, :callback => 'alert' render :json => {:hello => 'world'}.to_json, :callback => 'alert'
end end
def render_json_with_custom_content_type
render :json => {:hello => 'world'}.to_json, :content_type => 'text/javascript'
end
def render_symbol_json def render_symbol_json
render :json => {:hello => 'world'}.to_json render :json => {:hello => 'world'}.to_json
end end
@ -65,6 +69,10 @@ class TestController < ActionController::Base
render :template => "test/hello" render :template => "test/hello"
end end
def render_xml_with_custom_content_type
render :xml => "<blah/>", :content_type => "application/atomsvc+xml"
end
def heading def heading
head :ok head :ok
end end
@ -199,56 +207,62 @@ class RenderTest < Test::Unit::TestCase
assert_template "test/hello_world" assert_template "test/hello_world"
end end
def test_do_with_render def test_render
get :render_hello_world get :render_hello_world
assert_template "test/hello_world" assert_template "test/hello_world"
end end
def test_do_with_render_from_variable def test_render_from_variable
get :render_hello_world_from_variable get :render_hello_world_from_variable
assert_equal "hello david", @response.body assert_equal "hello david", @response.body
end end
def test_do_with_render_action def test_render_action
get :render_action_hello_world get :render_action_hello_world
assert_template "test/hello_world" assert_template "test/hello_world"
end end
def test_do_with_render_action_with_symbol def test_render_action_with_symbol
get :render_action_hello_world_with_symbol get :render_action_hello_world_with_symbol
assert_template "test/hello_world" assert_template "test/hello_world"
end end
def test_do_with_render_text def test_render_text
get :render_text_hello_world get :render_text_hello_world
assert_equal "hello world", @response.body assert_equal "hello world", @response.body
end end
def test_do_with_render_json def test_render_json
get :render_json_hello_world get :render_json_hello_world
assert_equal '{"hello": "world"}', @response.body assert_equal '{"hello": "world"}', @response.body
assert_equal 'application/json', @response.content_type assert_equal 'application/json', @response.content_type
end end
def test_do_with_render_json_with_callback def test_render_json_with_callback
get :render_json_hello_world_with_callback get :render_json_hello_world_with_callback
assert_equal 'alert({"hello": "world"})', @response.body assert_equal 'alert({"hello": "world"})', @response.body
assert_equal 'application/json', @response.content_type assert_equal 'application/json', @response.content_type
end 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 get :render_symbol_json
assert_equal '{"hello": "world"}', @response.body assert_equal '{"hello": "world"}', @response.body
assert_equal 'application/json', @response.content_type assert_equal 'application/json', @response.content_type
end end
def test_do_with_render_custom_code def test_render_custom_code
get :render_custom_code get :render_custom_code
assert_response 404 assert_response 404
assert_equal 'hello world', @response.body assert_equal 'hello world', @response.body
end end
def test_do_with_render_nothing_with_appendix def test_render_nothing_with_appendix
get :render_nothing_with_appendix get :render_nothing_with_appendix
assert_response 200 assert_response 200
assert_equal 'appended', @response.body assert_equal 'appended', @response.body
@ -269,6 +283,7 @@ class RenderTest < Test::Unit::TestCase
def test_render_xml def test_render_xml
get :render_xml_hello get :render_xml_hello
assert_equal "<html>\n <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body assert_equal "<html>\n <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body
assert_equal "application/xml", @response.content_type
end end
def test_render_xml_with_default def test_render_xml_with_default
@ -435,6 +450,11 @@ class RenderTest < Test::Unit::TestCase
assert_equal %(Element.replace("foo", "partial html");), @response.body assert_equal %(Element.replace("foo", "partial html");), @response.body
end 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 protected
def etag_for(text) def etag_for(text)