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

Ticket #3335: action_caching_for_non_success_status_codes.2.diff

File action_caching_for_non_success_status_codes.2.diff, 2.8 kB (added by josh, 1 year ago)
  • actionpack/test/controller/caching_test.rb

    old new  
    121121 
    122122 
    123123class ActionCachingTestController < ActionController::Base 
    124   caches_action :index 
     124  caches_action :index, :redirected, :forbidden 
    125125  caches_action :show, :cache_path => 'http://test.host/custom/show' 
    126126  caches_action :edit, :cache_path => Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" } 
    127127 
     
    129129    @cache_this = Time.now.to_f.to_s 
    130130    render :text => @cache_this 
    131131  end 
    132    
     132 
     133  def redirected 
     134    redirect_to :action => 'index' 
     135  end 
     136 
     137  def forbidden 
     138    render :text => "Forbidden" 
     139    headers["Status"] = "403 Forbidden" 
     140  end 
     141 
    133142  alias_method :show, :index 
    134143  alias_method :edit, :index 
    135144 
     
    245254    assert_equal david_cache, @response.body 
    246255  end 
    247256 
     257  def test_redirect_is_not_cached 
     258    get :redirected 
     259    assert_response :redirect 
     260    reset! 
     261 
     262    get :redirected 
     263    assert_response :redirect 
     264  end 
     265 
     266  def test_forbidden_is_not_cached 
     267    get :forbidden 
     268    assert_response :forbidden 
     269    reset! 
     270 
     271    get :forbidden 
     272    assert_response :forbidden 
     273  end 
     274 
    248275  def test_xml_version_of_resource_is_treated_as_different_cache 
    249276    @mock_controller.mock_url_for = 'http://example.org/posts/' 
    250277    @mock_controller.mock_path    = '/posts/index.xml' 
  • actionpack/lib/action_controller/caching.rb

    old new  
    240240        end 
    241241 
    242242        def after(controller) 
    243           return if !@actions.include?(controller.action_name.intern) || controller.rendered_action_cache 
     243          return if !@actions.include?(controller.action_name.intern) || controller.rendered_action_cache || !caching_allowed(controller) 
    244244          controller.write_fragment(controller.action_cache_path.path, controller.response.body) 
    245245        end 
    246          
     246 
    247247        private 
    248            
    249248          def set_content_type!(controller, extension) 
    250249            controller.response.content_type = Mime::EXTENSION_LOOKUP[extension].to_s if extension 
    251250          end 
    252            
     251 
    253252          def path_options_for(controller, options) 
    254253            ((path_options = options[:cache_path]).respond_to?(:call) ? path_options.call(controller) : path_options) || {} 
    255254          end 
    256            
     255 
     256          def caching_allowed(controller) 
     257            controller.request.get? && controller.response.headers['Status'].to_i == 200 
     258          end 
    257259      end 
    258260       
    259261      class ActionCachePath