Permalink
Browse files

adds check for :static_cache_control setting to Sinatra::Base.static!

  • Loading branch information...
kenichi nakamura
kenichi nakamura committed Jun 6, 2011
1 parent 89c6a38 commit 9dc155f3e9761493ca6e6fbbaf3acb2487824597
Showing with 23 additions and 0 deletions.
  1. +2 −0 lib/sinatra/base.rb
  2. +21 −0 test/static_test.rb
View
@@ -708,6 +708,7 @@ def static!
return unless path.start_with?(public_dir) and File.file?(path)
env['sinatra.static_file'] = path
+ cache_control *settings.static_cache_control if settings.static_cache_control?
send_file path, :disposition => nil
end
@@ -1339,6 +1340,7 @@ class << self
set :public, Proc.new { root && File.join(root, 'public') }
set :static, Proc.new { public && File.exist?(public) }
+ set :static_cache_control, false
error ::Exception do
response.status = 500
View
@@ -154,4 +154,25 @@ def assert_valid_range(http_range, range, path, file)
assert_equal "bytes */#{length}",response['Content-Range'], "416 response should include actual length"
end
end
+
+ it 'does not include static cache control headers by default' do
+ env = Rack::MockRequest.env_for("/#{File.basename(__FILE__)}")
+ status, headers, body = @app.call(env)
+ assert !headers.has_key?('Cache-Control')
+ end
+
+ it 'sets cache control headers on static files if set' do
+ @app.set :static_cache_control, :public
+ env = Rack::MockRequest.env_for("/#{File.basename(__FILE__)}")
+ status, headers, body = @app.call(env)
+ assert headers.has_key?('Cache-Control')
+ assert_equal headers['Cache-Control'], 'public'
+
+ @app.set :static_cache_control, [:public, :must_revalidate, {:max_age => 300}]
+ env = Rack::MockRequest.env_for("/#{File.basename(__FILE__)}")
+ status, headers, body = @app.call(env)
+ assert headers.has_key?('Cache-Control')
+ assert_equal headers['Cache-Control'], 'public, must-revalidate, max-age=300'
+ end
+
end

0 comments on commit 9dc155f

Please sign in to comment.