test/controller/caching_test.rb in actionpack-2.1.2 vs test/controller/caching_test.rb in actionpack-2.2.2

- old
+ new

@@ -4,11 +4,10 @@ CACHE_DIR = 'test_cache' # Don't change '/../temp/' cavalierly or you might hose something you don't want hosed 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? } caches_page :found, :not_found @@ -108,11 +107,11 @@ assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/trailing_slash.html") end uses_mocha("should_cache_ok_at_custom_path") do def test_should_cache_ok_at_custom_path - @request.expects(:path).returns("/index.html") + @request.stubs(:path).returns("/index.html") get :ok assert_response :ok assert File.exist?("#{FILE_STORE_PATH}/index.html") end end @@ -129,12 +128,11 @@ end end end def test_page_caching_conditional_options - @request.env['HTTP_ACCEPT'] = 'application/json' - get :ok + get :ok, :format=>'json' assert_page_not_cached :ok end private def assert_page_cached(action, message = "#{action} should have been cached") @@ -148,16 +146,16 @@ def page_cached?(action) File.exist? "#{FILE_STORE_PATH}/page_caching_test/#{action}.html" end end - class ActionCachingTestController < ActionController::Base - caches_action :index, :redirected, :forbidden, :if => Proc.new { |c| !c.request.format.json? } + caches_action :index, :redirected, :forbidden, :if => Proc.new { |c| !c.request.format.json? }, :expires_in => 1.hour 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 + caches_action :layout_false, :layout => false layout 'talk_from_action.erb' def index @cache_this = MockTime.now.to_f.to_s @@ -179,15 +177,21 @@ end alias_method :show, :index alias_method :edit, :index alias_method :destroy, :index + alias_method :layout_false, :with_layout def expire expire_action :controller => 'action_caching_test', :action => 'index' render :nothing => true end + + def expire_xml + expire_action :controller => 'action_caching_test', :action => 'index', :format => 'xml' + render :nothing => true + end end class MockTime < Time # Let Time spicy to assure that Time.now != Time.now def to_f @@ -209,10 +213,12 @@ def request mocked_path = @mock_path Object.new.instance_eval(<<-EVAL) def path; '#{@mock_path}' end + def format; 'all' end + def cache_format; nil end self EVAL end end @@ -261,16 +267,41 @@ 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_with_layout_and_layout_cache_false + get :layout_false + cached_time = content_to_cache + assert_not_equal cached_time, @response.body + assert fragment_exist?('hostname.com/action_caching_test/layout_false') + reset! + + get :layout_false + assert_not_equal cached_time, @response.body + + assert_equal cached_time, read_fragment('hostname.com/action_caching_test/layout_false') + end + def test_action_cache_conditional_options + old_use_accept_header = ActionController::Base.use_accept_header + ActionController::Base.use_accept_header = true @request.env['HTTP_ACCEPT'] = 'application/json' get :index assert !fragment_exist?('hostname.com/action_caching_test') + ActionController::Base.use_accept_header = old_use_accept_header end + uses_mocha 'test action cache' do + def test_action_cache_with_store_options + MockTime.expects(:now).returns(12345).once + @controller.expects(:read_fragment).with('hostname.com/action_caching_test', :expires_in => 1.hour).once + @controller.expects(:write_fragment).with('hostname.com/action_caching_test', '12345.0', :expires_in => 1.hour).once + get :index + end + end + def test_action_cache_with_custom_cache_path get :show cached_time = content_to_cache assert_equal cached_time, @response.body assert fragment_exist?('test.host/custom/show') @@ -309,10 +340,24 @@ get :index assert_response :success assert_equal new_cached_time, @response.body end + def test_cache_expiration_isnt_affected_by_request_format + get :index + cached_time = content_to_cache + reset! + + @request.set_REQUEST_URI "/action_caching_test/expire.xml" + get :expire, :format => :xml + reset! + + get :index + new_cached_time = content_to_cache + assert_not_equal cached_time, @response.body + end + def test_cache_is_scoped_by_subdomain @request.host = 'jamis.hostname.com' get :index jamis_cache = content_to_cache @@ -353,15 +398,33 @@ get :forbidden assert_response :forbidden end def test_xml_version_of_resource_is_treated_as_different_cache - @mock_controller.mock_url_for = 'http://example.org/posts/' - @mock_controller.mock_path = '/posts/index.xml' - path_object = @path_class.new(@mock_controller, {}) - assert_equal 'xml', path_object.extension - assert_equal 'example.org/posts/index.xml', path_object.path + with_routing do |set| + ActionController::Routing::Routes.draw do |map| + map.connect ':controller/:action.:format' + map.connect ':controller/:action' + end + + get :index, :format => 'xml' + cached_time = content_to_cache + assert_equal cached_time, @response.body + assert fragment_exist?('hostname.com/action_caching_test/index.xml') + reset! + + get :index, :format => 'xml' + assert_equal cached_time, @response.body + assert_equal 'application/xml', @response.content_type + reset! + + get :expire_xml + reset! + + get :index, :format => 'xml' + assert_not_equal cached_time, @response.body + end end def test_correct_content_type_is_returned_for_cache_hit # run it twice to cache it the first time get :index, :id => 'content-type.xml' @@ -419,62 +482,64 @@ @response = ActionController::TestResponse.new @controller.params = @params @controller.request = @request @controller.response = @response @controller.send(:initialize_current_url) + @controller.send(:initialize_template_class, @response) + @controller.send(:assign_shortcuts, @request, @response) end def test_fragment_cache_key assert_equal 'views/what a key', @controller.fragment_cache_key('what a key') - assert_equal( "views/test.host/fragment_caching_test/some_action", - @controller.fragment_cache_key(:controller => 'fragment_caching_test',:action => 'some_action')) + assert_equal "views/test.host/fragment_caching_test/some_action", + @controller.fragment_cache_key(:controller => 'fragment_caching_test',:action => 'some_action') end - def test_read_fragment__with_caching_enabled + def test_read_fragment_with_caching_enabled @store.write('views/name', 'value') assert_equal 'value', @controller.read_fragment('name') end - def test_read_fragment__with_caching_disabled + def test_read_fragment_with_caching_disabled ActionController::Base.perform_caching = false @store.write('views/name', 'value') assert_nil @controller.read_fragment('name') end - def test_fragment_exist__with_caching_enabled + def test_fragment_exist_with_caching_enabled @store.write('views/name', 'value') assert @controller.fragment_exist?('name') assert !@controller.fragment_exist?('other_name') end - def test_fragment_exist__with_caching_disabled + def test_fragment_exist_with_caching_disabled ActionController::Base.perform_caching = false @store.write('views/name', 'value') assert !@controller.fragment_exist?('name') assert !@controller.fragment_exist?('other_name') end - def test_write_fragment__with_caching_enabled + def test_write_fragment_with_caching_enabled assert_nil @store.read('views/name') assert_equal 'value', @controller.write_fragment('name', 'value') assert_equal 'value', @store.read('views/name') end - def test_write_fragment__with_caching_disabled + def test_write_fragment_with_caching_disabled assert_nil @store.read('views/name') ActionController::Base.perform_caching = false assert_equal nil, @controller.write_fragment('name', 'value') assert_nil @store.read('views/name') end - def test_expire_fragment__with_simple_key + def test_expire_fragment_with_simple_key @store.write('views/name', 'value') @controller.expire_fragment 'name' assert_nil @store.read('views/name') end - def test_expire_fragment__with__regexp + def test_expire_fragment_with_regexp @store.write('views/name', 'value') @store.write('views/another_name', 'another_value') @store.write('views/primalgrasp', 'will not expire ;-)') @controller.expire_fragment /name/ @@ -482,75 +547,35 @@ assert_nil @store.read('views/name') assert_nil @store.read('views/another_name') assert_equal 'will not expire ;-)', @store.read('views/primalgrasp') end - def test_fragment_for__with_disabled_caching + def test_fragment_for_with_disabled_caching ActionController::Base.perform_caching = false @store.write('views/expensive', 'fragment content') fragment_computed = false buffer = 'generated till now -> ' - @controller.fragment_for(Proc.new { fragment_computed = true }, 'expensive') { buffer } + @controller.fragment_for(buffer, 'expensive') { fragment_computed = true } assert fragment_computed assert_equal 'generated till now -> ', buffer end def test_fragment_for @store.write('views/expensive', 'fragment content') fragment_computed = false buffer = 'generated till now -> ' - @controller.fragment_for(Proc.new { fragment_computed = true }, 'expensive') { buffer} + @controller.fragment_for(buffer, 'expensive') { fragment_computed = true } assert !fragment_computed assert_equal 'generated till now -> fragment content', buffer end - - def test_cache_erb_fragment - @store.write('views/expensive', 'fragment content') - _erbout = 'generated till now -> ' - - assert_equal( 'generated till now -> fragment content', - ActionView::TemplateHandlers::ERB.new(@controller).cache_fragment(Proc.new{ }, 'expensive')) - end - - def test_cache_rxml_fragment - @store.write('views/expensive', 'fragment content') - xml = 'generated till now -> ' - class << xml; def target!; to_s; end; end - - assert_equal( 'generated till now -> fragment content', - ActionView::TemplateHandlers::Builder.new(@controller).cache_fragment(Proc.new{ }, 'expensive')) - end - - def test_cache_rjs_fragment - @store.write('views/expensive', 'fragment content') - page = 'generated till now -> ' - - assert_equal( 'generated till now -> fragment content', - ActionView::TemplateHandlers::RJS.new(@controller).cache_fragment(Proc.new{ }, 'expensive')) - end - - def test_cache_rjs_fragment_debug_mode_does_not_interfere - @store.write('views/expensive', 'fragment content') - page = 'generated till now -> ' - - begin - debug_mode, ActionView::Base.debug_rjs = ActionView::Base.debug_rjs, true - assert_equal( 'generated till now -> fragment content', - ActionView::TemplateHandlers::RJS.new(@controller).cache_fragment(Proc.new{ }, 'expensive')) - assert ActionView::Base.debug_rjs - ensure - ActionView::Base.debug_rjs = debug_mode - end - end end - class FunctionalCachingController < ActionController::Base def fragment_cached end def html_fragment_cached_with_partial @@ -563,27 +588,33 @@ respond_to do |format| format.js end end + def formatted_fragment_cached + respond_to do |format| + format.html + format.xml + format.js + end + end def rescue_action(e) raise e end end -FunctionalCachingController.view_paths = [ File.dirname(__FILE__) + "/../fixtures/" ] - class FunctionalFragmentCachingTest < Test::Unit::TestCase def setup ActionController::Base.perform_caching = true @store = ActiveSupport::Cache::MemoryStore.new ActionController::Base.cache_store = @store @controller = FunctionalCachingController.new @request = ActionController::TestRequest.new @response = ActionController::TestResponse.new end + def test_fragment_caching get :fragment_cached assert_response :success expected_body = <<-CACHED Hello @@ -599,12 +630,51 @@ 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_render_inline_before_fragment_caching + get :inline_fragment_cached + assert_response :success + assert_match /Some inline content/, @response.body + assert_match /Some cached content/, @response.body + assert_match "Some cached content", @store.read('views/test.host/functional_caching/inline_fragment_cached') + end + def test_fragment_caching_in_rjs_partials xhr :get, :js_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/js_fragment_cached_with_partial') + end + + def test_html_formatted_fragment_caching + get :formatted_fragment_cached, :format => "html" + assert_response :success + expected_body = "<body>\n<p>ERB</p>\n</body>" + + assert_equal expected_body, @response.body + + assert_equal "<p>ERB</p>", @store.read('views/test.host/functional_caching/formatted_fragment_cached') + end + + def test_xml_formatted_fragment_caching + get :formatted_fragment_cached, :format => "xml" + assert_response :success + expected_body = "<body>\n <p>Builder</p>\n</body>\n" + + assert_equal expected_body, @response.body + + assert_equal " <p>Builder</p>\n", @store.read('views/test.host/functional_caching/formatted_fragment_cached') + end + + def test_js_formatted_fragment_caching + get :formatted_fragment_cached, :format => "js" + assert_response :success + expected_body = %(title = "Hey";\n$("element_1").visualEffect("highlight");\n) + + %($("element_2").visualEffect("highlight");\nfooter = "Bye";) + assert_equal expected_body, @response.body + + assert_equal ['$("element_1").visualEffect("highlight");', '$("element_2").visualEffect("highlight");'], + @store.read('views/test.host/functional_caching/formatted_fragment_cached') end end