Changeset 5755
- Timestamp:
- 12/19/06 20:25:52 (2 years ago)
- Files:
-
- trunk/actionpack/CHANGELOG (modified) (1 diff)
- trunk/actionpack/lib/action_controller/caching.rb (modified) (1 diff)
- trunk/actionpack/test/controller/caching_test.rb (modified) (10 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/actionpack/CHANGELOG
r5748 r5755 1 1 *SVN* 2 3 * Only cache GET requests with a 200 OK response. #6514, #6743 [RSL, anamba] 2 4 3 5 * Add a 'referer' attribute to TestRequest. [Jamis Buck] trunk/actionpack/lib/action_controller/caching.rb
r5544 r5755 136 136 private 137 137 def caching_allowed 138 !request.post? && response.headers['Status'] && response.headers['Status'].to_i < 400138 request.get? && response.headers['Status'].to_i == 200 139 139 end 140 140 end trunk/actionpack/test/controller/caching_test.rb
r5289 r5755 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 10 11 class 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 29 end 9 30 10 31 class PageCachingTest < Test::Unit::TestCase … … 15 36 map.connect ':controller/:action/:id' 16 37 end 17 38 18 39 @request = ActionController::TestRequest.new 40 @request.host = 'hostname.com' 41 42 @response = ActionController::TestResponse.new 43 @controller = PageCachingTestController.new 44 19 45 @params = {:controller => 'posts', :action => 'index', :only_path => true, :skip_relative_url_root => true} 20 46 @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 22 55 23 56 def test_page_caching_resources_saves_to_correct_path_with_extension_even_if_default_route … … 27 60 assert_equal '/', @rewriter.rewrite(@params) 28 61 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 29 94 end 30 95 31 96 class ActionCachingTestController < ActionController::Base 32 97 caches_action :index 33 98 34 99 def index 35 100 @cache_this = Time.now.to_f.to_s 36 101 render :text => @cache_this 37 102 end 38 103 39 104 def expire 40 105 expire_action :controller => 'action_caching_test', :action => 'index' 41 106 render :nothing => true 42 107 end 43 108 44 109 end 45 110 … … 47 112 attr_accessor :mock_url_for 48 113 attr_accessor :mock_path 49 114 50 115 def initialize 51 116 yield self if block_given? 52 117 end 53 118 54 119 def url_for(*args) 55 120 @mock_url_for 56 121 end 57 122 58 123 def request 59 124 mocked_path = @mock_path … … 72 137 @mock_controller = ActionCachingMockController.new 73 138 end 74 139 75 140 def teardown 76 141 FileUtils.rm_rf(File.dirname(FILE_STORE_PATH)) 77 142 end 78 143 79 144 def test_simple_action_cache 80 145 get :index … … 82 147 assert_equal cached_time, @response.body 83 148 reset! 84 149 85 150 get :index 86 151 assert_equal cached_time, @response.body 87 152 end 88 153 89 154 def test_cache_expiration 90 155 get :index 91 156 cached_time = content_to_cache 92 157 reset! 93 158 94 159 get :index 95 160 assert_equal cached_time, @response.body … … 98 163 get :expire 99 164 reset! 100 165 101 166 get :index 102 167 new_cached_time = content_to_cache 103 168 assert_not_equal cached_time, @response.body 104 169 reset! 105 170 106 171 get :index 107 172 assert_response :success 108 173 assert_equal new_cached_time, @response.body 109 174 end 110 175 111 176 def test_cache_is_scoped_by_subdomain 112 177 @request.host = 'jamis.hostname.com' 113 178 get :index 114 179 jamis_cache = content_to_cache 115 180 116 181 @request.host = 'david.hostname.com' 117 182 get :index 118 183 david_cache = content_to_cache 119 184 assert_not_equal jamis_cache, @response.body 120 185 121 186 @request.host = 'jamis.hostname.com' 122 187 get :index 123 188 assert_equal jamis_cache, @response.body 124 189 125 190 @request.host = 'david.hostname.com' 126 191 get :index 127 192 assert_equal david_cache, @response.body 128 193 end 129 194 130 195 def test_xml_version_of_resource_is_treated_as_different_cache 131 196 @mock_controller.mock_url_for = 'http://example.org/posts/' … … 135 200 assert_equal 'example.org/posts/index.xml', path_object.path 136 201 end 137 202 138 203 def test_empty_path_is_normalized 139 204 @mock_controller.mock_url_for = 'http://example.org/' … … 142 207 assert_equal 'example.org/index', @path_class.path_for(@mock_controller) 143 208 end 144 209 145 210 def test_file_extensions 146 get :index, :id => 'kitten.jpg'147 211 get :index, :id => 'kitten.jpg' 212 get :index, :id => 'kitten.jpg' 148 213 149 214 assert_response :success 150 215 end 151 216 152 217 private 153 154 218 def content_to_cache 155 219 assigns(:cache_this) 156 220 end 157 221 158 222 def reset! 159 223 @request = ActionController::TestRequest.new … … 162 226 @request.host = 'hostname.com' 163 227 end 164 165 end 228 end