From db922514f49bbc8756a55da3cffe5ed5c0463bee Mon Sep 17 00:00:00 2001 From: Samuel Williams Date: Sat, 11 Jan 2020 10:08:56 +1300 Subject: [PATCH] Fix ActiveStorage use-case and add test case. Fixes #1464. --- lib/rack/files.rb | 2 +- test/spec_files.rb | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) diff --git a/lib/rack/files.rb b/lib/rack/files.rb index 20f048e44..f1a91c8bc 100644 --- a/lib/rack/files.rb +++ b/lib/rack/files.rb @@ -22,7 +22,7 @@ class Files attr_reader :root def initialize(root, headers = {}, default_mime = 'text/plain') - @root = ::File.expand_path root + @root = (::File.expand_path(root) if root) @headers = headers @default_mime = default_mime @head = Rack::Head.new(lambda { |env| get env }) diff --git a/test/spec_files.rb b/test/spec_files.rb index ad57972d2..6e75c073a 100644 --- a/test/spec_files.rb +++ b/test/spec_files.rb @@ -12,6 +12,20 @@ def files(*args) Rack::Lint.new Rack::Files.new(*args) end + it "can be used without root" do + # https://github.com/rack/rack/issues/1464 + + app = Rack::Files.new(nil) + + request = Rack::Request.new( + Rack::MockRequest.env_for("/cgi/test") + ) + + file_path = File.expand_path("cgi/test", __dir__) + status, headers, body = app.serving(request, file_path) + assert_equal 200, status + end + it 'serves files with + in the file name' do Dir.mktmpdir do |dir| File.write File.join(dir, "you+me.txt"), "hello world"