From c37bc6b2619ee053e77e729d0c1f1342c6a3b38e Mon Sep 17 00:00:00 2001 From: Wijnand Wiersma Date: Sun, 16 May 2010 09:40:19 +0200 Subject: [PATCH] Respect the accept header when using caches_page. When requested as /jobs.xml it would create the file public/jobs.xml . However, when requested as /jobs with a header of 'Accept: text/xml' it would render the XML but cache to public/jobs.html . This meant GET /jobs.html would now produce XML. --- actionpack/lib/action_controller/caching/pages.rb | 10 +++++++++- actionpack/test/controller/caching_test.rb | 12 ++++++++++++ 2 files changed, 21 insertions(+), 1 deletions(-) diff --git a/actionpack/lib/action_controller/caching/pages.rb b/actionpack/lib/action_controller/caching/pages.rb index bd3b5a5..9c1093a 100644 --- a/actionpack/lib/action_controller/caching/pages.rb +++ b/actionpack/lib/action_controller/caching/pages.rb @@ -131,6 +131,12 @@ module ActionController #:nodoc: def cache_page(content = nil, options = nil) return unless perform_caching && caching_allowed + orig_extension = self.class.page_cache_extension + should_set_page_cache_extension = (self.request.format != 'text/html' ) + if should_set_page_cache_extension + self.class.page_cache_extension = ".#{self.request.format.to_sym}" + end + path = case options when Hash url_for(options.merge(:only_path => true, :skip_relative_url_root => true, :format => params[:format])) @@ -140,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 = orig_extension if should_set_page_cache_extension + end end private diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index d67e31f..ee14b2a 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -120,6 +120,18 @@ class PageCachingTest < ActionController::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 + [:ok, :no_content, :found, :not_found].each do |status| [:get, :post, :put, :delete].each do |method| unless method == :get and status == :ok -- 1.6.4.2