1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00
rails--rails/railties/test/application/middleware/sendfile_test.rb
Piotr Sarnacki 55dd060347 Handle files from ActionDispatch::Static with Rack::Sendfile (fixes #5225)
This makes rails behave properly when you serve static assets
and you have X-Sendfile headers enabled. Nevertheless in most
cases you should not rely on that and serve static assets with
a webserver like Apache or Nginx (as you already have it in
place anyway if you use X-Sendfile)
2012-03-03 20:46:56 +01:00

74 lines
1.9 KiB
Ruby

require 'isolation/abstract_unit'
module ApplicationTests
class SendfileTest < ActiveSupport::TestCase
include ActiveSupport::Testing::Isolation
def setup
build_app
boot_rails
FileUtils.rm_rf "#{app_path}/config/environments"
end
def teardown
teardown_app
end
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
test "config.action_dispatch.x_sendfile_header defaults to nil" do
make_basic_app
simple_controller
get "/"
assert !last_response.headers["X-Sendfile"]
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
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'
app.config.serve_static_assets = true
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
end
end