From 2fafdad2bed03c85d09775664e5b00d9d22bbb39 Mon Sep 17 00:00:00 2001 From: Wijnand Wiersma Date: Sun, 16 May 2010 17:51:38 +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 | 14 +++++++++++++- actionpack/test/controller/caching_test.rb | 12 ++++++++++++ 2 files changed, 25 insertions(+), 1 deletions(-) diff --git a/actionpack/lib/action_controller/caching/pages.rb b/actionpack/lib/action_controller/caching/pages.rb index 4f7a5d3..11114a0 100644 --- a/actionpack/lib/action_controller/caching/pages.rb +++ b/actionpack/lib/action_controller/caching/pages.rb @@ -145,7 +145,8 @@ module ActionController #:nodoc: else request.path end - + path += set_right_extension(path) + self.class.cache_page(content || response.body, path) end @@ -153,6 +154,17 @@ module ActionController #:nodoc: def caching_allowed request.get? && response.status.to_i == 200 end + + def set_right_extension(path) + unless path.split('/').last.include? '.' # extension missing + if (self.request.format != 'text/html' ) + return ".#{self.request.format.to_sym}" + else + return self.class.page_cache_extension + end + end + '' + end end end end diff --git a/actionpack/test/controller/caching_test.rb b/actionpack/test/controller/caching_test.rb index 4431eb2..c9d3697 100644 --- a/actionpack/test/controller/caching_test.rb +++ b/actionpack/test/controller/caching_test.rb @@ -123,6 +123,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