Merge pull request #790 from GateGuru/feature/enable-options-in-static-method

Feature/enable options in static method
This commit is contained in:
Konstantin Haase 2014-02-21 10:37:33 +01:00
commit faf2efc670
2 changed files with 20 additions and 3 deletions

View File

@ -1023,14 +1023,14 @@ module Sinatra
# Attempt to serve static files from public directory. Throws :halt when
# a matching file is found, returns nil otherwise.
def static!
def static!(options = {})
return if (public_dir = settings.public_folder).nil?
path = File.expand_path("#{public_dir}#{unescape(request.path_info)}" )
return unless File.file?(path)
env['sinatra.static_file'] = path
cache_control(*settings.static_cache_control) if settings.static_cache_control?
send_file path, :disposition => nil
send_file path, options.merge(:disposition => nil)
end
# Enable string or symbol key access to the nested params hash.

View File

@ -216,4 +216,21 @@ class StaticTest < Test::Unit::TestCase
)
end
end
it 'renders static assets with custom status via options' do
mock_app do
set :static, true
set :public_folder, File.dirname(__FILE__)
post '/*' do
static!(:status => params[:status])
end
end
post "/#{File.basename(__FILE__)}?status=422"
assert_equal response.status, 422
assert_equal File.read(__FILE__), body
assert_equal File.size(__FILE__).to_s, response['Content-Length']
assert response.headers.include?('Last-Modified')
end
end