Ensure that while caching a page rails takes into

account the resolved mime type for the request

[#6110 state:resolved]

Signed-off-by: José Valim <jose.valim@gmail.com>
This commit is contained in:
Neeraj Singh 2010-12-04 17:40:54 -05:00 committed by José Valim
parent cc48192248
commit 0027b65421
2 changed files with 29 additions and 7 deletions

@ -70,9 +70,9 @@ def expire_page(path)
# Manually cache the +content+ in the key determined by +path+. Example:
# cache_page "I'm the cached content", "/lists/show"
def cache_page(content, path)
def cache_page(content, path, extension = nil)
return unless perform_caching
path = page_cache_path(path)
path = page_cache_path(path, extension)
instrument_page_cache :write_page, path do
FileUtils.makedirs(File.dirname(path))
@ -97,14 +97,16 @@ def caches_page(*actions)
end
private
def page_cache_file(path)
def page_cache_file(path, extension)
name = (path.empty? || path == "/") ? "/index" : URI.parser.unescape(path.chomp('/'))
name << page_cache_extension unless (name.split('/').last || name).include? '.'
unless (name.split('/').last || name).include? '.'
name << (extension || self.page_cache_extension)
end
return name
end
def page_cache_path(path)
page_cache_directory + page_cache_file(path)
def page_cache_path(path, extension = nil)
page_cache_directory + page_cache_file(path, extension)
end
def instrument_page_cache(name, path)
@ -145,7 +147,11 @@ def cache_page(content = nil, options = nil)
request.path
end
self.class.cache_page(content || response.body, path)
if (type = Mime::LOOKUP[self.content_type]) && (type_symbol = type.symbol).present?
extension = ".#{type_symbol}"
end
self.class.cache_page(content || response.body, path, extension)
end
end

@ -16,6 +16,7 @@ class CachingController < ActionController::Base
class PageCachingTestController < CachingController
caches_page :ok, :no_content, :if => Proc.new { |c| !c.request.format.json? }
caches_page :found, :not_found
caches_page :about_me
def ok
@ -47,6 +48,14 @@ def expire_custom_path
def trailing_slash
render :text => "Sneak attack"
end
def about_me
respond_to do |format|
format.html {render :text => 'I am html'}
format.xml {render :text => 'I am xml'}
end
end
end
class PageCachingTest < ActionController::TestCase
@ -111,6 +120,13 @@ def test_should_cache_without_trailing_slash_on_url
assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/trailing_slash.html")
end
def test_should_obey_http_accept_attribute
@request.env['HTTP_ACCEPT'] = 'text/xml'
get :about_me
assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/about_me.xml")
assert_equal 'I am xml', @response.body
end
def test_should_cache_with_trailing_slash_on_url
@controller.class.cache_page 'cached content', '/page_caching_test/trailing_slash/'
assert File.exist?("#{FILE_STORE_PATH}/page_caching_test/trailing_slash.html")