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

Ticket #6743: cache_only_for_200_OK.diff

File cache_only_for_200_OK.diff, 3.0 kB (added by anamba, 2 years ago)
  • test/controller/caching_test.rb

    old new  
    55# Don't change '/../temp/' cavalierly or you might hoze something you don't want hozed 
    66FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR) 
    77ActionController::Base.perform_caching = true 
     8ActionController::Base.page_cache_directory = FILE_STORE_PATH 
    89ActionController::Base.fragment_cache_store = :file_store, FILE_STORE_PATH 
    910 
     11class PageCachingTestController < ActionController::Base 
     12  caches_page :index, :test404, :test302 
     13   
     14  def index 
     15    render :text => "all's well" 
     16  end 
     17   
     18  def test404 
     19    render :text => "oopsie!", :status => 404 
     20  end 
     21   
     22  def test302 
     23    redirect_to :action => 'index' 
     24  end 
     25end 
     26 
    1027class PageCachingTest < Test::Unit::TestCase 
    1128  def setup 
    1229    ActionController::Routing::Routes.draw do |map| 
     
    1835    @request = ActionController::TestRequest.new 
    1936    @params = {:controller => 'posts', :action => 'index', :only_path => true, :skip_relative_url_root => true} 
    2037    @rewriter = ActionController::UrlRewriter.new(@request, @params) 
    21   end  
    22  
     38     
     39    FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) 
     40    FileUtils.mkdir_p(FILE_STORE_PATH) 
     41  end 
     42   
     43  def teardown 
     44    FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) 
     45  end 
     46   
    2347  def test_page_caching_resources_saves_to_correct_path_with_extension_even_if_default_route 
    2448    @params[:format] = 'rss' 
    2549    assert_equal '/posts.rss', @rewriter.rewrite(@params) 
    2650    @params[:format] = nil 
    2751    assert_equal '/', @rewriter.rewrite(@params) 
    2852  end 
     53   
     54  def test_page_caching_only_for_200_OK 
     55    reset! 
     56    get :index 
     57    assert_equal 200, @response.headers['Status'].to_i 
     58    assert File.exists?(File.join(FILE_STORE_PATH, 'page_caching_test.html')) 
     59     
     60    reset! 
     61    get :test404 
     62    assert_equal 404, @response.headers['Status'].to_i 
     63    assert !File.exists?(File.join(FILE_STORE_PATH, 'page_caching_test', 'test404.html')) 
     64     
     65    reset! 
     66    get :test302 
     67    assert_equal 302, @response.headers['Status'].to_i 
     68    assert !File.exists?(File.join(FILE_STORE_PATH, 'page_caching_test', 'test302.html')) 
     69  end 
     70   
     71  private 
     72   
     73  def reset! 
     74    @request    = ActionController::TestRequest.new 
     75    @response   = ActionController::TestResponse.new 
     76    @controller = PageCachingTestController.new 
     77    @request.host = 'hostname.com' 
     78  end 
    2979end 
    3080 
    3181class ActionCachingTestController < ActionController::Base 
  • lib/action_controller/caching.rb

    old new  
    135135 
    136136      private 
    137137        def caching_allowed 
    138           !request.post? && response.headers['Status'] && response.headers['Status'].to_i < 400 
     138          !request.post? && response.headers['Status'] && response.headers['Status'].to_i == 200 
    139139        end 
    140140    end 
    141141