From 7708650f73ddb4db300ea2059c60c1d907a4384e Mon Sep 17 00:00:00 2001 From: Joshua Peek Date: Wed, 14 May 2008 12:33:54 -0500 Subject: =?UTF-8?q?Added=20conditional=20support=20to=20caches=5Faction=20?= =?UTF-8?q?[Jos=C3=A9=20Valim]=20[#166=20state:resolved]?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- actionpack/test/controller/caching_test.rb | 80 +++++++++++++++++++++++++----- 1 file changed, 67 insertions(+), 13 deletions(-) (limited to 'actionpack/test') diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index ddc1c68383..4aacb4a78a 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -6,6 +6,7 @@ CACHE_DIR = 'test_cache' FILE_STORE_PATH = File.join(File.dirname(__FILE__), '/../temp/', CACHE_DIR) ActionController::Base.page_cache_directory = FILE_STORE_PATH ActionController::Base.cache_store = :file_store, FILE_STORE_PATH +ActionController::Base.view_paths = [ File.dirname(__FILE__) + '/../fixtures/' ] class PageCachingTestController < ActionController::Base caches_page :ok, :no_content, :if => Proc.new { |c| !c.request.format.json? } @@ -128,7 +129,7 @@ class PageCachingTest < Test::Unit::TestCase end end end - + def test_page_caching_conditional_options @request.env['HTTP_ACCEPT'] = 'application/json' get :ok @@ -151,12 +152,15 @@ end class ActionCachingTestController < ActionController::Base - caches_action :index, :redirected, :forbidden + caches_action :index, :redirected, :forbidden, :if => Proc.new { |c| !c.request.format.json? } caches_action :show, :cache_path => 'http://test.host/custom/show' caches_action :edit, :cache_path => Proc.new { |c| c.params[:id] ? "http://test.host/#{c.params[:id]};edit" : "http://test.host/edit" } + caches_action :with_layout + + layout 'talk_from_action.erb' def index - @cache_this = Time.now.to_f.to_s + @cache_this = MockTime.now.to_f.to_s render :text => @cache_this end @@ -169,14 +173,26 @@ class ActionCachingTestController < ActionController::Base headers["Status"] = "403 Forbidden" end + def with_layout + @cache_this = MockTime.now.to_f.to_s + render :text => @cache_this, :layout => true + end + alias_method :show, :index alias_method :edit, :index + alias_method :destroy, :index def expire expire_action :controller => 'action_caching_test', :action => 'index' render :nothing => true end +end +class MockTime < Time + # Let Time spicy to assure that Time.now != Time.now + def to_f + super+rand + end end class ActionCachingMockController @@ -223,6 +239,36 @@ class ActionCacheTest < Test::Unit::TestCase assert_equal cached_time, @response.body end + def test_simple_action_not_cached + get :destroy + cached_time = content_to_cache + assert_equal cached_time, @response.body + assert_cache_does_not_exist 'hostname.com/action_caching_test/destroy' + reset! + + get :destroy + assert_not_equal cached_time, @response.body + end + + def test_action_cache_with_layout + get :with_layout + cached_time = content_to_cache + assert_not_equal cached_time, @response.body + assert_cache_exists 'hostname.com/action_caching_test/with_layout' + reset! + + get :with_layout + assert_not_equal cached_time, @response.body + + assert_equal @response.body, read_fragment('hostname.com/action_caching_test/with_layout') + end + + def test_action_cache_conditional_options + @request.env['HTTP_ACCEPT'] = 'application/json' + get :index + assert_cache_does_not_exist 'hostname.com/action_caching_test' + end + def test_action_cache_with_custom_cache_path get :show cached_time = content_to_cache @@ -350,9 +396,22 @@ class ActionCacheTest < Test::Unit::TestCase end def assert_cache_exists(path) - full_path = File.join(FILE_STORE_PATH, "views", path + '.cache') + full_path = cache_path(path) assert File.exist?(full_path), "#{full_path.inspect} does not exist." end + + def assert_cache_does_not_exist(path) + full_path = cache_path(path) + assert !File.exist?(full_path), "#{full_path.inspect} should not exist." + end + + def cache_path(path) + File.join(FILE_STORE_PATH, 'views', path + '.cache') + end + + def read_fragment(path) + @controller.read_fragment(path) + end end class FragmentCachingTestController < ActionController::Base @@ -516,7 +575,7 @@ class FunctionalFragmentCachingTest < Test::Unit::TestCase def setup ActionController::Base.perform_caching = true @store = ActiveSupport::Cache::MemoryStore.new - ActionController::Base.cache_store = @store + ActionController::Base.cache_store = @store @controller = FunctionalCachingController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new @@ -529,17 +588,17 @@ Hello This bit's fragment cached CACHED assert_equal expected_body, @response.body - + assert_equal "This bit's fragment cached", @store.read('views/test.host/functional_caching/fragment_cached') end - + def test_fragment_caching_in_partials get :html_fragment_cached_with_partial assert_response :success assert_match /Fragment caching in a partial/, @response.body assert_match "Fragment caching in a partial", @store.read('views/test.host/functional_caching/html_fragment_cached_with_partial') end - + def test_fragment_caching_in_rjs_partials xhr :get, :js_fragment_cached_with_partial assert_response :success @@ -547,8 +606,3 @@ CACHED assert_match "Fragment caching in a partial", @store.read('views/test.host/functional_caching/js_fragment_cached_with_partial') end end - - - - - -- cgit v1.2.3