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

Changeset 5755

Show
Ignore:
Timestamp:
12/19/06 20:25:52 (2 years ago)
Author:
bitsweat
Message:

Only cache GET requests with a 200 OK response. Closes #6514, #6743.

Files:

Legend:

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

    r5748 r5755  
    11*SVN* 
     2 
     3* Only cache GET requests with a 200 OK response.  #6514, #6743 [RSL, anamba] 
    24 
    35* Add a 'referer' attribute to TestRequest. [Jamis Buck] 
  • trunk/actionpack/lib/action_controller/caching.rb

    r5544 r5755  
    136136      private 
    137137        def caching_allowed 
    138           !request.post? && response.headers['Status'] && response.headers['Status'].to_i < 400 
     138          request.get? && response.headers['Status'].to_i == 200 
    139139        end 
    140140    end 
  • trunk/actionpack/test/controller/caching_test.rb

    r5289 r5755  
    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 
     10 
     11class PageCachingTestController < ActionController::Base 
     12  caches_page :ok, :no_content, :found, :not_found 
     13 
     14  def ok 
     15    head :ok 
     16  end 
     17 
     18  def no_content 
     19    head :no_content 
     20  end 
     21 
     22  def found 
     23    redirect_to :action => 'ok' 
     24  end 
     25 
     26  def not_found 
     27    head :not_found 
     28  end 
     29end 
    930 
    1031class PageCachingTest < Test::Unit::TestCase 
     
    1536      map.connect ':controller/:action/:id' 
    1637    end 
    17      
     38 
    1839    @request = ActionController::TestRequest.new 
     40    @request.host = 'hostname.com' 
     41 
     42    @response   = ActionController::TestResponse.new 
     43    @controller = PageCachingTestController.new 
     44 
    1945    @params = {:controller => 'posts', :action => 'index', :only_path => true, :skip_relative_url_root => true} 
    2046    @rewriter = ActionController::UrlRewriter.new(@request, @params) 
    21   end  
     47 
     48    FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) 
     49    FileUtils.mkdir_p(FILE_STORE_PATH) 
     50  end 
     51 
     52  def teardown 
     53    FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) 
     54  end 
    2255 
    2356  def test_page_caching_resources_saves_to_correct_path_with_extension_even_if_default_route 
     
    2760    assert_equal '/', @rewriter.rewrite(@params) 
    2861  end 
     62 
     63  def test_should_cache_get_with_ok_status 
     64    get :ok 
     65    assert_response :ok 
     66    assert_page_cached :ok, "get with ok status should have been cached" 
     67  end 
     68 
     69  [:ok, :no_content, :found, :not_found].each do |status| 
     70    [:get, :post, :put, :delete].each do |method| 
     71      unless method == :get and status == :ok 
     72        define_method "test_shouldnt_cache_#{method}_with_#{status}_status" do 
     73          @request.env['REQUEST_METHOD'] = method.to_s.upcase 
     74          process status 
     75          assert_response status 
     76          assert_page_not_cached status, "#{method} with #{status} status shouldn't have been cached" 
     77        end 
     78      end 
     79    end 
     80  end 
     81 
     82  private 
     83    def assert_page_cached(action, message = "#{action} should have been cached") 
     84      assert page_cached?(action), message 
     85    end 
     86 
     87    def assert_page_not_cached(action, message = "#{action} shouldn't have been cached") 
     88      assert !page_cached?(action), message 
     89    end 
     90 
     91    def page_cached?(action) 
     92      File.exist? "#{FILE_STORE_PATH}/page_caching_test/#{action}.html" 
     93    end 
    2994end 
    3095 
    3196class ActionCachingTestController < ActionController::Base 
    3297  caches_action :index 
    33   
     98 
    3499  def index 
    35100    @cache_this = Time.now.to_f.to_s 
    36101    render :text => @cache_this 
    37102  end 
    38    
     103 
    39104  def expire 
    40105    expire_action :controller => 'action_caching_test', :action => 'index' 
    41106    render :nothing => true 
    42107  end 
    43    
     108 
    44109end 
    45110 
     
    47112  attr_accessor :mock_url_for 
    48113  attr_accessor :mock_path 
    49    
     114 
    50115  def initialize 
    51116    yield self if block_given? 
    52117  end 
    53    
     118 
    54119  def url_for(*args) 
    55120    @mock_url_for 
    56121  end 
    57    
     122 
    58123  def request 
    59124    mocked_path = @mock_path 
     
    72137    @mock_controller = ActionCachingMockController.new 
    73138  end 
    74    
     139 
    75140  def teardown 
    76141    FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) 
    77142  end 
    78    
     143 
    79144  def test_simple_action_cache 
    80145    get :index 
     
    82147    assert_equal cached_time, @response.body 
    83148    reset! 
    84      
     149 
    85150    get :index 
    86151    assert_equal cached_time, @response.body 
    87152  end 
    88    
     153 
    89154  def test_cache_expiration 
    90155    get :index 
    91156    cached_time = content_to_cache 
    92157    reset! 
    93          
     158 
    94159    get :index 
    95160    assert_equal cached_time, @response.body 
     
    98163    get :expire 
    99164    reset! 
    100      
     165 
    101166    get :index 
    102167    new_cached_time = content_to_cache 
    103168    assert_not_equal cached_time, @response.body 
    104169    reset! 
    105      
     170 
    106171    get :index 
    107172    assert_response :success 
    108173    assert_equal new_cached_time, @response.body 
    109174  end 
    110    
     175 
    111176  def test_cache_is_scoped_by_subdomain 
    112177    @request.host = 'jamis.hostname.com' 
    113178    get :index 
    114179    jamis_cache = content_to_cache 
    115      
     180 
    116181    @request.host = 'david.hostname.com' 
    117182    get :index 
    118183    david_cache = content_to_cache 
    119184    assert_not_equal jamis_cache, @response.body 
    120      
     185 
    121186    @request.host = 'jamis.hostname.com' 
    122187    get :index 
    123188    assert_equal jamis_cache, @response.body 
    124      
     189 
    125190    @request.host = 'david.hostname.com' 
    126191    get :index 
    127192    assert_equal david_cache, @response.body 
    128193  end 
    129    
     194 
    130195  def test_xml_version_of_resource_is_treated_as_different_cache 
    131196    @mock_controller.mock_url_for = 'http://example.org/posts/' 
     
    135200    assert_equal 'example.org/posts/index.xml', path_object.path 
    136201  end 
    137    
     202 
    138203  def test_empty_path_is_normalized 
    139204    @mock_controller.mock_url_for = 'http://example.org/' 
     
    142207    assert_equal 'example.org/index', @path_class.path_for(@mock_controller) 
    143208  end 
    144    
     209 
    145210  def test_file_extensions 
    146     get :index, :id => 'kitten.jpg'  
    147211    get :index, :id => 'kitten.jpg' 
     212    get :index, :id => 'kitten.jpg' 
    148213 
    149214    assert_response :success 
    150215  end 
    151    
     216 
    152217  private 
    153    
    154218    def content_to_cache 
    155219      assigns(:cache_this) 
    156220    end 
    157      
     221 
    158222    def reset! 
    159223      @request    = ActionController::TestRequest.new 
     
    162226      @request.host = 'hostname.com' 
    163227    end 
    164    
    165 end 
     228end