2010-10-02 11:42:36 -04:00
|
|
|
require 'isolation/abstract_unit'
|
|
|
|
|
|
|
|
module ApplicationTests
|
2012-01-05 20:30:17 -05:00
|
|
|
class SendfileTest < ActiveSupport::TestCase
|
2010-10-02 11:42:36 -04:00
|
|
|
include ActiveSupport::Testing::Isolation
|
|
|
|
|
|
|
|
def setup
|
|
|
|
build_app
|
|
|
|
boot_rails
|
|
|
|
FileUtils.rm_rf "#{app_path}/config/environments"
|
|
|
|
end
|
|
|
|
|
2011-06-06 08:54:05 -04:00
|
|
|
def teardown
|
|
|
|
teardown_app
|
|
|
|
end
|
|
|
|
|
2010-10-02 11:42:36 -04:00
|
|
|
def app
|
|
|
|
@app ||= Rails.application
|
|
|
|
end
|
|
|
|
|
|
|
|
define_method :simple_controller do
|
|
|
|
class ::OmgController < ActionController::Base
|
|
|
|
def index
|
|
|
|
send_file __FILE__
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
# x_sendfile_header middleware
|
2011-08-07 11:34:49 -04:00
|
|
|
test "config.action_dispatch.x_sendfile_header defaults to nil" do
|
2010-10-02 11:42:36 -04:00
|
|
|
make_basic_app
|
|
|
|
simple_controller
|
|
|
|
|
|
|
|
get "/"
|
2011-08-07 11:34:49 -04:00
|
|
|
assert !last_response.headers["X-Sendfile"]
|
2010-10-02 11:42:36 -04:00
|
|
|
assert_equal File.read(__FILE__), last_response.body
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config.action_dispatch.x_sendfile_header can be set" do
|
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.action_dispatch.x_sendfile_header = "X-Sendfile"
|
|
|
|
end
|
|
|
|
|
|
|
|
simple_controller
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
assert_equal File.expand_path(__FILE__), last_response.headers["X-Sendfile"]
|
|
|
|
end
|
|
|
|
|
|
|
|
test "config.action_dispatch.x_sendfile_header is sent to Rack::Sendfile" do
|
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.action_dispatch.x_sendfile_header = 'X-Lighttpd-Send-File'
|
|
|
|
end
|
|
|
|
|
|
|
|
simple_controller
|
|
|
|
|
|
|
|
get "/"
|
|
|
|
assert_equal File.expand_path(__FILE__), last_response.headers["X-Lighttpd-Send-File"]
|
|
|
|
end
|
2012-03-03 14:05:45 -05:00
|
|
|
|
|
|
|
test "files handled by ActionDispatch::Static are handled by Rack::Sendfile" do
|
|
|
|
make_basic_app do |app|
|
|
|
|
app.config.action_dispatch.x_sendfile_header = 'X-Sendfile'
|
2015-11-04 16:31:16 -05:00
|
|
|
app.config.public_file_server.enabled = true
|
2012-03-03 14:05:45 -05:00
|
|
|
app.paths["public"] = File.join(rails_root, "public")
|
|
|
|
end
|
|
|
|
|
|
|
|
app_file "public/foo.txt", "foo"
|
|
|
|
|
|
|
|
get "/foo.txt", "HTTP_X_SENDFILE_TYPE" => "X-Sendfile"
|
|
|
|
assert_equal File.join(rails_root, "public/foo.txt"), last_response.headers["X-Sendfile"]
|
|
|
|
end
|
2010-10-02 11:42:36 -04:00
|
|
|
end
|
|
|
|
end
|