diff --git a/actionpack/lib/action_controller/caching/pages.rb b/actionpack/lib/action_controller/caching/pages.rb index a70ed72..598369e 100644 --- a/actionpack/lib/action_controller/caching/pages.rb +++ b/actionpack/lib/action_controller/caching/pages.rb @@ -54,7 +54,7 @@ module ActionController #:nodoc: @@page_cache_directory = defined?(Rails.public_path) ? Rails.public_path : "" cattr_accessor :page_cache_directory - @@page_cache_extension = '.html' + @@page_cache_extension = nil cattr_accessor :page_cache_extension end end @@ -133,6 +133,10 @@ module ActionController #:nodoc: def cache_page(content = nil, options = nil) return unless perform_caching && caching_allowed + should_set_page_cache_extension = !self.class.page_cache_extension + if should_set_page_cache_extension + self.class.page_cache_extension = ".#{self.request.format.to_sym}" || ".html" + end path = case options when Hash url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format])) @@ -142,7 +146,9 @@ module ActionController #:nodoc: request.path end - self.class.cache_page(content || response.body, path) + returning(self.class.cache_page(content || response.body, path)) do + self.class.page_cache_extension = nil if should_set_page_cache_extension + end end private @@ -151,4 +157,4 @@ module ActionController #:nodoc: end end end -end \ No newline at end of file +end diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index b6cdd11..2df22dc 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -89,6 +89,29 @@ class PageCachingTest < Test::Unit::TestCase assert File.exist?("#{FILE_STORE_PATH}/index.html") end + def test_should_cache_with_correct_extension + xml_format = Mime::Type.lookup('text/xml') + @request.stubs(:format).returns(xml_format) + @params[:format] = nil + get :ok + assert_response :ok + assert ! File.exist?("#{FILE_STORE_PATH}/page_caching_test/ok.html"), + "get with ok status of type text/xml should not have been cached as html" + assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/ok.xml"), + "get with ok status of type text/xml should have been cached" + end + + def test_should_cache_with_default_extension_when_set + with_cache_extension('.shtml') do + get :ok + end + assert_response :ok + assert ! File.exist?("#{FILE_STORE_PATH}/page_caching_test/ok.html"), + "get with ok status with a set page_cache_extension should not have been cached as html" + assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/ok.shtml"), + "get with ok status with a set page_cache_extension should have been cached" + end + def test_should_expire_cache_with_custom_path get :custom_path assert File.exist?("#{FILE_STORE_PATH}/index.html") @@ -98,12 +121,16 @@ class PageCachingTest < Test::Unit::TestCase end def test_should_cache_without_trailing_slash_on_url - @controller.class.cache_page 'cached content', '/page_caching_test/trailing_slash' + with_cache_extension('.html') do + @controller.class.cache_page 'cached content', '/page_caching_test/trailing_slash' + end assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/trailing_slash.html") end def test_should_cache_with_trailing_slash_on_url - @controller.class.cache_page 'cached content', '/page_caching_test/trailing_slash/' + with_cache_extension('.html') do + @controller.class.cache_page 'cached content', '/page_caching_test/trailing_slash/' + end assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/trailing_slash.html") end @@ -146,6 +173,17 @@ class PageCachingTest < Test::Unit::TestCase def page_cached?(action) File.exist? "#{FILE_STORE_PATH}/page_caching_test/#{action}.html" end + + def with_cache_extension(extension) + begin + prior_extension = ActionController::Base.page_cache_extension + ActionController::Base.page_cache_extension = extension + yield + assert_equal extension, ActionController::Base.page_cache_extension + ensure + ActionController::Base.page_cache_extension = prior_extension + end + end end class ActionCachingTestController < ActionController::Base