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 5 5 # Don't change '/../temp/' cavalierly or you might hoze something you don't want hozed 6 6 FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR) 7 7 ActionController::Base.perform_caching = true 8 ActionController::Base.page_cache_directory = FILE_STORE_PATH 8 9 ActionController::Base.fragment_cache_store = :file_store, FILE_STORE_PATH 9 10 11 class 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 25 end 26 10 27 class PageCachingTest < Test::Unit::TestCase 11 28 def setup 12 29 ActionController::Routing::Routes.draw do |map| … … 18 35 @request = ActionController::TestRequest.new 19 36 @params = {:controller => 'posts', :action => 'index', :only_path => true, :skip_relative_url_root => true} 20 37 @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 23 47 def test_page_caching_resources_saves_to_correct_path_with_extension_even_if_default_route 24 48 @params[:format] = 'rss' 25 49 assert_equal '/posts.rss', @rewriter.rewrite(@params) 26 50 @params[:format] = nil 27 51 assert_equal '/', @rewriter.rewrite(@params) 28 52 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 29 79 end 30 80 31 81 class ActionCachingTestController < ActionController::Base -
lib/action_controller/caching.rb
old new 135 135 136 136 private 137 137 def caching_allowed 138 !request.post? && response.headers['Status'] && response.headers['Status'].to_i < 400138 !request.post? && response.headers['Status'] && response.headers['Status'].to_i == 200 139 139 end 140 140 end 141 141