Add support for supplying options to `static!`, enabling the use of custom status codes, etc.

Add test case highlighting the breakage when attempting to use `send_file` with a specific status code. refs sinatra/sinatra#749
This commit is contained in:
Blake Watters 2013-07-31 17:47:10 -04:00
parent 816c0812f3
commit 8fb79acb99
2 changed files with 20 additions and 3 deletions

View File

@ -1015,14 +1015,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