2011-05-11 03:44:02 -04:00
|
|
|
require File.expand_path('../helper', __FILE__)
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2015-01-10 14:30:47 -05:00
|
|
|
class StaticTest < Minitest::Test
|
2009-03-26 11:42:13 -04:00
|
|
|
setup do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2008-12-13 16:06:02 -05:00
|
|
|
set :static, true
|
2020-03-13 17:20:04 -04:00
|
|
|
set :public_folder, __dir__
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'serves GET requests for files in the public directory' do
|
2009-01-30 20:27:17 -05:00
|
|
|
get "/#{File.basename(__FILE__)}"
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal File.read(__FILE__), body
|
|
|
|
assert_equal File.size(__FILE__).to_s, response['Content-Length']
|
|
|
|
assert response.headers.include?('Last-Modified')
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2009-01-21 14:37:56 -05:00
|
|
|
it 'produces a body that can be iterated over multiple times' do
|
2009-01-30 20:27:17 -05:00
|
|
|
env = Rack::MockRequest.env_for("/#{File.basename(__FILE__)}")
|
2014-03-28 00:09:42 -04:00
|
|
|
_, _, body = @app.call(env)
|
2009-01-21 14:37:56 -05:00
|
|
|
buf1, buf2 = [], []
|
|
|
|
body.each { |part| buf1 << part }
|
|
|
|
body.each { |part| buf2 << part }
|
|
|
|
assert_equal buf1.join, buf2.join
|
|
|
|
assert_equal File.read(__FILE__), buf1.join
|
|
|
|
end
|
|
|
|
|
2010-01-28 11:59:30 -05:00
|
|
|
it 'sets the sinatra.static_file env variable if served' do
|
|
|
|
env = Rack::MockRequest.env_for("/#{File.basename(__FILE__)}")
|
2014-03-28 00:09:42 -04:00
|
|
|
@app.call(env)
|
2010-01-28 11:59:30 -05:00
|
|
|
assert_equal File.expand_path(__FILE__), env['sinatra.static_file']
|
|
|
|
end
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it 'serves HEAD requests for files in the public directory' do
|
2009-01-30 20:27:17 -05:00
|
|
|
head "/#{File.basename(__FILE__)}"
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert_equal '', body
|
|
|
|
assert response.headers.include?('Last-Modified')
|
2012-05-23 16:39:31 -04:00
|
|
|
assert_equal File.size(__FILE__).to_s, response['Content-Length']
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2009-03-25 13:55:16 -04:00
|
|
|
%w[POST PUT DELETE].each do |verb|
|
|
|
|
it "does not serve #{verb} requests" do
|
|
|
|
send verb.downcase, "/#{File.basename(__FILE__)}"
|
|
|
|
assert_equal 404, status
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it 'serves files in preference to custom routes' do
|
2009-01-30 20:27:17 -05:00
|
|
|
@app.get("/#{File.basename(__FILE__)}") { 'Hello World' }
|
|
|
|
get "/#{File.basename(__FILE__)}"
|
2009-01-14 17:00:26 -05:00
|
|
|
assert ok?
|
|
|
|
assert body != 'Hello World'
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'does not serve directories' do
|
|
|
|
get "/"
|
2009-01-14 17:00:26 -05:00
|
|
|
assert not_found?
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
2020-03-13 09:56:04 -04:00
|
|
|
it 'passes to the next handler when the path contains null bytes' do
|
|
|
|
get "/foo%00"
|
|
|
|
assert not_found?
|
|
|
|
end
|
|
|
|
|
2008-12-13 16:06:02 -05:00
|
|
|
it 'passes to the next handler when the static option is disabled' do
|
|
|
|
@app.set :static, false
|
2009-01-30 20:27:17 -05:00
|
|
|
get "/#{File.basename(__FILE__)}"
|
2009-01-14 17:00:26 -05:00
|
|
|
assert not_found?
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'passes to the next handler when the public option is nil' do
|
2011-06-15 18:24:27 -04:00
|
|
|
@app.set :public_folder, nil
|
2009-01-30 20:27:17 -05:00
|
|
|
get "/#{File.basename(__FILE__)}"
|
2009-01-14 17:00:26 -05:00
|
|
|
assert not_found?
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
it '404s when a file is not found' do
|
|
|
|
get "/foobarbaz.txt"
|
2009-01-14 17:00:26 -05:00
|
|
|
assert not_found?
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|
2009-03-09 17:46:45 -04:00
|
|
|
|
2020-10-05 09:18:49 -04:00
|
|
|
it 'path is escaped in 404 error pages' do
|
|
|
|
env = Rack::MockRequest.env_for("/dummy").tap { |env| env["PATH_INFO"] = "/<script>" }
|
|
|
|
_, _, body = @app.call(env)
|
|
|
|
assert_equal(["GET /<script>"], body, "Unexpected response content.")
|
|
|
|
end
|
|
|
|
|
2009-03-09 17:46:45 -04:00
|
|
|
it 'serves files when .. path traverses within public directory' do
|
|
|
|
get "/data/../#{File.basename(__FILE__)}"
|
|
|
|
assert ok?
|
|
|
|
assert_equal File.read(__FILE__), body
|
|
|
|
end
|
|
|
|
|
|
|
|
it '404s when .. path traverses outside of public directory' do
|
2012-05-21 17:21:59 -04:00
|
|
|
mock_app do
|
2009-03-09 17:46:45 -04:00
|
|
|
set :static, true
|
2020-03-13 17:20:04 -04:00
|
|
|
set :public_folder, __dir__ + '/data'
|
2021-02-17 17:43:15 -05:00
|
|
|
disable :protection
|
2012-05-21 17:21:59 -04:00
|
|
|
end
|
2009-03-09 17:46:45 -04:00
|
|
|
get "/../#{File.basename(__FILE__)}"
|
|
|
|
assert not_found?
|
|
|
|
end
|
2010-10-17 15:31:07 -04:00
|
|
|
|
2010-10-19 05:39:20 -04:00
|
|
|
def assert_valid_range(http_range, range, path, file)
|
2010-10-17 15:31:07 -04:00
|
|
|
request = Rack::MockRequest.new(@app)
|
|
|
|
response = request.get("/#{File.basename(path)}", 'HTTP_RANGE' => http_range)
|
|
|
|
|
|
|
|
should_be = file[range]
|
|
|
|
expected_range = "bytes #{range.begin}-#{range.end}/#{file.length}"
|
|
|
|
|
2012-05-21 17:21:59 -04:00
|
|
|
assert_equal(
|
|
|
|
206,response.status,
|
|
|
|
"Should be HTTP/1.1 206 Partial content"
|
|
|
|
)
|
|
|
|
assert_equal(
|
|
|
|
should_be.length,
|
|
|
|
response.body.length,
|
|
|
|
"Unexpected response length for #{http_range}"
|
|
|
|
)
|
|
|
|
assert_equal(
|
|
|
|
should_be,
|
|
|
|
response.body,
|
|
|
|
"Unexpected response data for #{http_range}"
|
|
|
|
)
|
|
|
|
assert_equal(
|
|
|
|
should_be.length.to_s,
|
|
|
|
response['Content-Length'],
|
|
|
|
"Incorrect Content-Length for #{http_range}"
|
|
|
|
)
|
|
|
|
assert_equal(
|
|
|
|
expected_range,
|
|
|
|
response['Content-Range'],
|
|
|
|
"Incorrect Content-Range for #{http_range}"
|
|
|
|
)
|
2010-10-17 15:31:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'handles valid byte ranges correctly' do
|
|
|
|
# Use the biggest file in this dir so we can test ranges > 8k bytes. (StaticFile sends in 8k chunks.)
|
2020-03-13 17:20:04 -04:00
|
|
|
path = __dir__ + '/helpers_test.rb' # currently 16k bytes
|
2010-10-17 15:31:07 -04:00
|
|
|
file = File.read(path)
|
|
|
|
length = file.length
|
|
|
|
assert length > 9000, "The test file #{path} is too short (#{length} bytes) to run these tests"
|
|
|
|
|
|
|
|
[0..0, 42..88, 1234..1234, 100..9000, 0..(length-1), (length-1)..(length-1)].each do |range|
|
2010-10-19 05:39:20 -04:00
|
|
|
assert_valid_range("bytes=#{range.begin}-#{range.end}", range, path, file)
|
2010-10-17 15:31:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
[0, 100, length-100, length-1].each do |start|
|
2010-10-19 05:39:20 -04:00
|
|
|
assert_valid_range("bytes=#{start}-", (start..length-1), path, file)
|
2010-10-17 15:31:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
[1, 100, length-100, length-1, length].each do |range_length|
|
2010-10-19 05:39:20 -04:00
|
|
|
assert_valid_range("bytes=-#{range_length}", (length-range_length..length-1), path, file)
|
2010-10-17 15:31:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# Some valid ranges that exceed the length of the file:
|
2010-10-19 05:39:20 -04:00
|
|
|
assert_valid_range("bytes=100-999999", (100..length-1), path, file)
|
|
|
|
assert_valid_range("bytes=100-#{length}", (100..length-1), path, file)
|
|
|
|
assert_valid_range("bytes=-#{length}", (0..length-1), path, file)
|
|
|
|
assert_valid_range("bytes=-#{length+1}", (0..length-1), path, file)
|
|
|
|
assert_valid_range("bytes=-999999", (0..length-1), path, file)
|
2010-10-17 15:31:07 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
it 'correctly ignores syntactically invalid range requests' do
|
2020-03-10 10:24:05 -04:00
|
|
|
["bytes=45-40", "bytes=IV-LXVI", "octets=10-20", "bytes=", "bytes=3-1,4-5"].each do |http_range|
|
2010-10-17 15:31:07 -04:00
|
|
|
request = Rack::MockRequest.new(@app)
|
|
|
|
response = request.get("/#{File.basename(__FILE__)}", 'HTTP_RANGE' => http_range)
|
|
|
|
|
2012-05-21 17:21:59 -04:00
|
|
|
assert_equal(
|
|
|
|
200,
|
|
|
|
response.status,
|
|
|
|
"Invalid range '#{http_range}' should be ignored"
|
|
|
|
)
|
2016-12-28 10:13:51 -05:00
|
|
|
assert_nil(
|
2012-05-21 17:21:59 -04:00
|
|
|
response['Content-Range'],
|
|
|
|
"Invalid range '#{http_range}' should be ignored"
|
|
|
|
)
|
2010-10-17 15:31:07 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'returns error 416 for unsatisfiable range requests' do
|
|
|
|
# An unsatisfiable request is one that specifies a start that's at or past the end of the file.
|
|
|
|
length = File.read(__FILE__).length
|
|
|
|
["bytes=888888-", "bytes=888888-999999", "bytes=#{length}-#{length}"].each do |http_range|
|
|
|
|
request = Rack::MockRequest.new(@app)
|
|
|
|
response = request.get("/#{File.basename(__FILE__)}", 'HTTP_RANGE' => http_range)
|
|
|
|
|
2012-05-21 17:21:59 -04:00
|
|
|
assert_equal(
|
|
|
|
416,
|
|
|
|
response.status,
|
|
|
|
"Unsatisfiable range '#{http_range}' should return 416"
|
|
|
|
)
|
|
|
|
assert_equal(
|
|
|
|
"bytes */#{length}",
|
|
|
|
response['Content-Range'],
|
|
|
|
"416 response should include actual length"
|
|
|
|
)
|
2010-10-17 15:31:07 -04:00
|
|
|
end
|
|
|
|
end
|
2011-06-06 14:09:41 -04:00
|
|
|
|
|
|
|
it 'does not include static cache control headers by default' do
|
|
|
|
env = Rack::MockRequest.env_for("/#{File.basename(__FILE__)}")
|
2014-03-28 00:09:42 -04:00
|
|
|
_, headers, _ = @app.call(env)
|
2011-06-06 14:09:41 -04:00
|
|
|
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__)}")
|
2019-04-28 10:38:36 -04:00
|
|
|
_, headers, _ = @app.call(env)
|
2011-06-06 14:09:41 -04:00
|
|
|
assert headers.has_key?('Cache-Control')
|
|
|
|
assert_equal headers['Cache-Control'], 'public'
|
|
|
|
|
2012-05-21 17:21:59 -04:00
|
|
|
@app.set(
|
|
|
|
:static_cache_control,
|
|
|
|
[:public, :must_revalidate, {:max_age => 300}]
|
|
|
|
)
|
2011-06-06 14:09:41 -04:00
|
|
|
env = Rack::MockRequest.env_for("/#{File.basename(__FILE__)}")
|
2019-04-28 10:38:36 -04:00
|
|
|
_, headers, _ = @app.call(env)
|
2011-06-06 14:09:41 -04:00
|
|
|
assert headers.has_key?('Cache-Control')
|
2012-05-21 17:21:59 -04:00
|
|
|
assert_equal(
|
|
|
|
headers['Cache-Control'],
|
|
|
|
'public, must-revalidate, max-age=300'
|
|
|
|
)
|
2011-06-06 14:09:41 -04:00
|
|
|
end
|
|
|
|
|
2013-07-31 17:47:10 -04:00
|
|
|
it 'renders static assets with custom status via options' do
|
|
|
|
mock_app do
|
|
|
|
set :static, true
|
2020-03-13 17:20:04 -04:00
|
|
|
set :public_folder, __dir__
|
2013-07-31 17:47:10 -04:00
|
|
|
|
|
|
|
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
|
|
|
|
|
2015-07-20 11:10:34 -04:00
|
|
|
it 'serves files with a + sign in the path' do
|
|
|
|
mock_app do
|
|
|
|
set :static, true
|
2020-03-13 17:20:04 -04:00
|
|
|
set :public_folder, File.join(__dir__, 'public')
|
2015-07-20 11:10:34 -04:00
|
|
|
end
|
2016-12-28 10:13:51 -05:00
|
|
|
|
2015-07-20 11:10:34 -04:00
|
|
|
get "/hello+world.txt"
|
2016-12-28 10:13:51 -05:00
|
|
|
|
2020-03-13 17:20:04 -04:00
|
|
|
real_path = File.join(__dir__, 'public', 'hello+world.txt')
|
2015-07-20 11:10:34 -04:00
|
|
|
assert ok?
|
|
|
|
assert_equal File.read(real_path), body
|
|
|
|
assert_equal File.size(real_path).to_s, response['Content-Length']
|
|
|
|
assert response.headers.include?('Last-Modified')
|
|
|
|
end
|
|
|
|
|
|
|
|
it 'serves files with a URL encoded + sign (%2B) in the path' do
|
|
|
|
mock_app do
|
|
|
|
set :static, true
|
2020-03-13 17:20:04 -04:00
|
|
|
set :public_folder, File.join(__dir__, 'public')
|
2015-07-20 11:10:34 -04:00
|
|
|
end
|
2016-12-28 10:13:51 -05:00
|
|
|
|
2015-07-20 11:10:34 -04:00
|
|
|
get "/hello%2bworld.txt"
|
2016-12-28 10:13:51 -05:00
|
|
|
|
2020-03-13 17:20:04 -04:00
|
|
|
real_path = File.join(__dir__, 'public', 'hello+world.txt')
|
2015-07-20 11:10:34 -04:00
|
|
|
assert ok?
|
|
|
|
assert_equal File.read(real_path), body
|
|
|
|
assert_equal File.size(real_path).to_s, response['Content-Length']
|
|
|
|
assert response.headers.include?('Last-Modified')
|
|
|
|
end
|
|
|
|
|
2015-01-10 14:30:47 -05:00
|
|
|
end
|