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

Ticket #10974: head_should_return_empty_body.diff

File head_should_return_empty_body.diff, 5.1 kB (added by wzph, 10 months ago)

HEAD should return a 200 by default, and an empty body.

  • test/controller/render_test.rb

    old new  
    7272  def render_nothing_with_appendix 
    7373    render :text => "appended" 
    7474  end 
     75 
     76  def render_nothing 
     77    render :nothing => true 
     78  end 
    7579   
    7680  def render_invalid_args 
    7781    render("test/hello") 
     
    9599    render :text => line 
    96100  end 
    97101 
     102  def render_empty_body 
     103    render :empty_body => true 
     104  end 
     105 
    98106  def heading 
    99107    head :ok 
    100108  end 
    101109 
     110  def heading_with_301 
     111    head 301 
     112  end 
     113 
    102114  def greeting 
    103115    # let's just rely on the template 
    104116  end 
     
    312324    assert_response 200 
    313325    assert_equal 'appended', @response.body 
    314326  end 
    315    
     327 
     328  def test_render_empty_body 
     329    get :render_empty_body 
     330    assert_response 200 
     331    assert_equal '', @response.body 
     332  end 
     333 
     334  def test_head_responses_should_have_empty_bodies 
     335    head :heading 
     336    assert_equal '', @response.body 
     337  end 
     338 
     339  def test_head_responses_should_return_200s_by_default 
     340    head :heading 
     341    assert_response 200 
     342  end 
     343 
     344  def test_head_responses_can_have_other_status_codes 
     345    head :heading_with_301 
     346    assert_response 301 
     347  end 
     348 
    316349  def test_attempt_to_render_with_invalid_arguments 
    317350    assert_raises(ActionController::RenderError) { get :render_invalid_args } 
    318351  end 
  • test/controller/rescue_test.rb

    old new  
    198198    end 
    199199 
    200200    assert_response :internal_server_error 
    201     assert_equal ' ', @response.body 
     201    assert_equal '', @response.body 
    202202  end 
    203203 
    204204  def test_rescue_unknown_action_in_public_with_error_file 
     
    221221    end 
    222222 
    223223    assert_response :not_found 
    224     assert_equal ' ', @response.body 
     224    assert_equal '', @response.body 
    225225  end 
    226226 
    227227  def test_rescue_missing_template_in_public 
  • test/controller/filters_test.rb

    old new  
    823823  def test_first_filter_in_multiple_before_filter_chain_halts 
    824824    controller = ::FilterTest::TestMultipleFiltersController.new 
    825825    response = test_process(controller, 'fail_1') 
    826     assert_equal ' ', response.body 
     826    assert_equal '', response.body 
    827827    assert_equal 1, controller.instance_variable_get(:@try) 
    828828    assert controller.instance_variable_get(:@before_filter_chain_aborted) 
    829829  end 
     
    831831  def test_second_filter_in_multiple_before_filter_chain_halts 
    832832    controller = ::FilterTest::TestMultipleFiltersController.new 
    833833    response = test_process(controller, 'fail_2') 
    834     assert_equal ' ', response.body 
     834    assert_equal '', response.body 
    835835    assert_equal 2, controller.instance_variable_get(:@try) 
    836836    assert controller.instance_variable_get(:@before_filter_chain_aborted) 
    837837  end 
     
    839839  def test_last_filter_in_multiple_before_filter_chain_halts 
    840840    controller = ::FilterTest::TestMultipleFiltersController.new 
    841841    response = test_process(controller, 'fail_3') 
    842     assert_equal ' ', response.body 
     842    assert_equal '', response.body 
    843843    assert_equal 3, controller.instance_variable_get(:@try) 
    844844    assert controller.instance_variable_get(:@before_filter_chain_aborted) 
    845845  end 
  • lib/action_controller/base.rb

    old new  
    913913          elsif options[:nothing] 
    914914            # Safari doesn't pass the headers of the return if the response is zero length 
    915915            render_for_text(" ", options[:status]) 
    916  
     916          elsif options[:empty_body] 
     917            # Absolutely nothing in the body--sorry, Safari! 
     918            # To be safe, this should probably only be used for 
     919            # HEAD requests. 
     920            render_for_text(nil, options[:status]) 
    917921          else 
    918922            render_for_file(default_template_name, options[:status], true) 
    919923          end 
     
    954958        options.each do |key, value| 
    955959          headers[key.to_s.dasherize.split(/-/).map { |v| v.capitalize }.join("-")] = value.to_s 
    956960        end 
    957  
    958         render :nothing => true, :status => status 
     961         
     962        render :empty_body => true, :status => status 
    959963      end 
    960964 
    961965 
  • lib/action_controller/layout.rb

    old new  
    273273 
    274274      def candidate_for_layout?(options) 
    275275        (options.has_key?(:layout) && options[:layout] != false) ||  
    276           options.values_at(:text, :xml, :json, :file, :inline, :partial, :nothing).compact.empty? && 
     276          options.values_at(:text, :xml, :json, :file, :inline, :partial, :nothing, :empty_body).compact.empty? && 
    277277          !template_exempt_from_layout?(options[:template] || default_template_name(options[:action])) 
    278278      end 
    279279