2009-01-13 12:53:53 -05:00
|
|
|
require File.dirname(__FILE__) + '/helper'
|
2008-12-13 16:06:02 -05:00
|
|
|
|
2009-03-26 11:42:13 -04:00
|
|
|
class StaticTest < Test::Unit::TestCase
|
|
|
|
setup do
|
2009-01-13 12:53:53 -05:00
|
|
|
mock_app {
|
2008-12-13 16:06:02 -05:00
|
|
|
set :static, true
|
2009-01-30 20:27:17 -05:00
|
|
|
set :public, File.dirname(__FILE__)
|
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__)}")
|
2009-01-21 14:37:56 -05:00
|
|
|
status, headers, body = @app.call(env)
|
|
|
|
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__)}")
|
|
|
|
status, headers, body = @app.call(env)
|
|
|
|
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_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-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
|
|
|
|
|
|
|
|
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
|
|
|
|
@app.set :public, 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
|
|
|
|
|
|
|
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
|
|
|
|
mock_app {
|
|
|
|
set :static, true
|
|
|
|
set :public, File.dirname(__FILE__) + '/data'
|
|
|
|
}
|
|
|
|
get "/../#{File.basename(__FILE__)}"
|
|
|
|
assert not_found?
|
|
|
|
end
|
2008-12-13 16:06:02 -05:00
|
|
|
end
|