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"