Ruby on Rails | Screencasts | Download | Documentation | Weblog | Community | Source

Changeset 8342

Show
Ignore:
Timestamp:
12/09/07 22:11:37 (7 months ago)
Author:
bitsweat
Message:

render :xml and :json preserve custom content types. Closes #10388.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/actionpack/CHANGELOG

    r8341 r8342  
    11*SVN* 
     2 
     3* render :xml and :json preserve custom content types.  #10388 [jmettraux, Chu Yeow] 
    24 
    35* Refactor Action View template handlers.  #10437 [Josh Peek] 
  • trunk/actionpack/lib/action_controller/base.rb

    r8335 r8342  
    874874 
    875875          elsif xml = options[:xml] 
    876             response.content_type = Mime::XML 
     876            response.content_type ||= Mime::XML 
    877877            render_for_text(xml.respond_to?(:to_xml) ? xml.to_xml : xml, options[:status]) 
    878878 
     
    880880            json = json.to_json unless json.is_a?(String) 
    881881            json = "#{options[:callback]}(#{json})" unless options[:callback].blank? 
    882             response.content_type = Mime::JSON 
     882            response.content_type ||= Mime::JSON 
    883883            render_for_text(json, options[:status]) 
    884884 
  • trunk/actionpack/test/controller/render_test.rb

    r8011 r8342  
    4545  end 
    4646 
     47  def render_json_with_custom_content_type 
     48    render :json => {:hello => 'world'}.to_json, :content_type => 'text/javascript' 
     49  end 
     50 
    4751  def render_symbol_json 
    4852    render :json => {:hello => 'world'}.to_json 
     
    6468    @name = "David" 
    6569    render :template => "test/hello" 
     70  end 
     71 
     72  def render_xml_with_custom_content_type 
     73    render :xml => "<blah/>", :content_type => "application/atomsvc+xml" 
    6674  end 
    6775 
     
    200208  end 
    201209 
    202   def test_do_with_render 
     210  def test_render 
    203211    get :render_hello_world 
    204212    assert_template "test/hello_world" 
    205213  end 
    206214 
    207   def test_do_with_render_from_variable 
     215  def test_render_from_variable 
    208216    get :render_hello_world_from_variable 
    209217    assert_equal "hello david", @response.body 
    210218  end 
    211219 
    212   def test_do_with_render_action 
     220  def test_render_action 
    213221    get :render_action_hello_world 
    214222    assert_template "test/hello_world" 
    215223  end 
    216224 
    217   def test_do_with_render_action_with_symbol 
     225  def test_render_action_with_symbol 
    218226    get :render_action_hello_world_with_symbol 
    219227    assert_template "test/hello_world" 
    220228  end 
    221229 
    222   def test_do_with_render_text 
     230  def test_render_text 
    223231    get :render_text_hello_world 
    224232    assert_equal "hello world", @response.body 
    225233  end 
    226234 
    227   def test_do_with_render_json 
     235  def test_render_json 
    228236    get :render_json_hello_world 
    229237    assert_equal '{"hello": "world"}', @response.body 
     
    231239  end 
    232240 
    233   def test_do_with_render_json_with_callback 
     241  def test_render_json_with_callback 
    234242    get :render_json_hello_world_with_callback 
    235243    assert_equal 'alert({"hello": "world"})', @response.body 
     
    237245  end 
    238246 
    239   def test_do_with_render_symbol_json 
     247  def test_render_json_with_custom_content_type 
     248    get :render_json_with_custom_content_type 
     249    assert_equal '{"hello": "world"}', @response.body 
     250    assert_equal 'text/javascript', @response.content_type 
     251  end 
     252 
     253  def test_render_symbol_json 
    240254    get :render_symbol_json 
    241255    assert_equal '{"hello": "world"}', @response.body 
     
    243257  end 
    244258 
    245   def test_do_with_render_custom_code 
     259  def test_render_custom_code 
    246260    get :render_custom_code 
    247261    assert_response 404 
     
    249263  end 
    250264 
    251   def test_do_with_render_nothing_with_appendix 
     265  def test_render_nothing_with_appendix 
    252266    get :render_nothing_with_appendix 
    253267    assert_response 200 
     
    270284    get :render_xml_hello 
    271285    assert_equal "<html>\n  <p>Hello David</p>\n<p>This is grand!</p>\n</html>\n", @response.body 
     286    assert_equal "application/xml", @response.content_type 
    272287  end 
    273288 
     
    436451  end 
    437452 
     453  def test_should_render_xml_but_keep_custom_content_type 
     454    get :render_xml_with_custom_content_type 
     455    assert_equal "application/atomsvc+xml", @response.content_type 
     456  end 
     457 
    438458  protected 
    439459