Skip to content

Commit

Permalink
cache content_type, fixes #1462
Browse files Browse the repository at this point in the history
  • Loading branch information
ujifgc committed Oct 17, 2013
1 parent c5e6274 commit 2005508
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
10 changes: 8 additions & 2 deletions padrino-cache/lib/padrino-cache/helpers/page.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -107,7 +107,10 @@ def self.padrino_route_added(route, verb, path, args, options, block)
value = settings.cache[resolve_cache_key || env['PATH_INFO']] 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 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 halt 200, value
end end
end end
Expand All @@ -116,7 +119,10 @@ def self.padrino_route_added(route, verb, path, args, options, block)
route.after_filters do route.after_filters do
if settings.caching? && @_response_buffer.kind_of?(String) if settings.caching? && @_response_buffer.kind_of?(String)
began_at = Time.now began_at = Time.now
content = @_response_buffer content = {
:body => @_response_buffer,
:content_type => @_content_type
}


if @_last_expires if @_last_expires
settings.cache.store(resolve_cache_key || env['PATH_INFO'], content, :expires => @_last_expires) settings.cache.store(resolve_cache_key || env['PATH_INFO'], content, :expires => @_last_expires)
Expand Down
29 changes: 27 additions & 2 deletions padrino-cache/test/test_padrino_cache.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -90,14 +90,14 @@
get "/foo" get "/foo"
assert_equal 200, status assert_equal 200, status
assert_equal 'foo', body assert_equal 'foo', body
assert_equal 'foo', @app.cache[:foo] assert_equal 'foo', @app.cache[:foo][:body]
get "/foo" get "/foo"
assert_equal 'foo', body assert_equal 'foo', body


get "/bar" get "/bar"
assert_equal 200, status assert_equal 200, status
assert_equal 'bar', body assert_equal 'bar', body
assert_equal 'bar', @app.cache[:bar] assert_equal 'bar', @app.cache[:bar][:body]
get "/bar" get "/bar"
assert_equal 'bar', body assert_equal 'bar', body
end end
Expand Down Expand Up @@ -306,4 +306,29 @@
assert_raises(RuntimeError) { get '/foo' } assert_raises(RuntimeError) { get '/foo' }
end 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 end

0 comments on commit 2005508

Please sign in to comment.