Permalink
Browse files

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
  • Loading branch information...
1 parent 3fab196 commit bafd698acb160fd241efe6f027b83676b9e80d1f @jeremy jeremy committed Dec 9, 2007
Showing with 34 additions and 12 deletions.
  1. +2 −0 actionpack/CHANGELOG
  2. +2 −2 actionpack/lib/action_controller/base.rb
  3. +30 −10 actionpack/test/controller/render_test.rb
View
@@ -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]
@@ -873,13 +873,13 @@ def render(options = nil, &block) #:doc:
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]
@@ -44,6 +44,10 @@ def render_json_hello_world_with_callback
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 @@ def render_xml_hello
render :template => "test/hello"
end
+ def render_xml_with_custom_content_type
+ render :xml => "<blah/>", :content_type => "application/atomsvc+xml"
+ end
+
def heading
head :ok
end
@@ -199,56 +207,62 @@ def test_simple_show
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 @@ def test_private_methods
def test_render_xml
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 "application/xml", @response.content_type
end
def test_render_xml_with_default
@@ -435,6 +450,11 @@ def test_should_render_with_alternate_default_render
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)

0 comments on commit bafd698

Please sign in to comment.