Permalink
Browse files

cache content_type, fixes #1462

  • Loading branch information...
ujifgc committed Oct 17, 2013
1 parent c5e6274 commit 2005508a215200e80c5fce698faca5775c008554
Showing with 35 additions and 4 deletions.
  1. +8 −2 padrino-cache/lib/padrino-cache/helpers/page.rb
  2. +27 −2 padrino-cache/test/test_padrino_cache.rb
@@ -107,7 +107,10 @@ def self.padrino_route_added(route, verb, path, args, options, block)
value = settings.cache[resolve_cache_key || env['PATH_INFO']]
logger.debug "GET Cache", began_at, @route.cache_key || env['PATH_INFO'] if defined?(logger) && value
if value
if value.kind_of?(Hash)
content_type value[:content_type]
halt 200, value[:body]
elsif value
halt 200, value
end
end
@@ -116,7 +119,10 @@ def self.padrino_route_added(route, verb, path, args, options, block)
route.after_filters do
if settings.caching? && @_response_buffer.kind_of?(String)
began_at = Time.now
content = @_response_buffer
content = {
:body => @_response_buffer,
:content_type => @_content_type
}
if @_last_expires
settings.cache.store(resolve_cache_key || env['PATH_INFO'], content, :expires => @_last_expires)
@@ -90,14 +90,14 @@
get "/foo"
assert_equal 200, status
assert_equal 'foo', body
assert_equal 'foo', @app.cache[:foo]
assert_equal 'foo', @app.cache[:foo][:body]
get "/foo"
assert_equal 'foo', body
get "/bar"
assert_equal 200, status
assert_equal 'bar', body
assert_equal 'bar', @app.cache[:bar]
assert_equal 'bar', @app.cache[:bar][:body]
get "/bar"
assert_equal 'bar', body
end
@@ -306,4 +306,29 @@
assert_raises(RuntimeError) { get '/foo' }
end
should 'cache content_type' do
called = false
mock_app do
register Padrino::Cache
enable :caching
get '/foo', :cache => true do
content_type :json
if called
"you'll never see me"
else
cache_key :foo
called = '{"foo":"bar"}'
called
end
end
end
get "/foo"
assert_equal 200, status
assert_equal '{"foo":"bar"}', body
assert_equal '{"foo":"bar"}', @app.cache[:foo][:body]
get "/foo"
assert_equal '{"foo":"bar"}', body
assert_match /json/, last_response.content_type
end
end

0 comments on commit 2005508

Please sign in to comment.