Skip to content

Commit

Permalink
Ensure the proper content type is returned for static files.
Browse files Browse the repository at this point in the history
  • Loading branch information
josevalim committed Oct 4, 2010
1 parent 10d014a commit 0b51f3c
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 23 deletions.
6 changes: 3 additions & 3 deletions actionpack/lib/action_dispatch/middleware/static.rb
Expand Up @@ -6,13 +6,13 @@ def initialize(at, root)
@at, @root = at.chomp('/'), root.chomp('/')
@compiled_at = (Regexp.compile(/^#{Regexp.escape(at)}/) unless @at.blank?)
@compiled_root = Regexp.compile(/^#{Regexp.escape(root)}/)
@file_server = ::Rack::File.new(root)
@file_server = ::Rack::File.new(@root)
end

def match?(path)
path = path.dup
if @compiled_at.blank? || path.sub!(@compiled_at, '')
full_path = File.join(@root, ::Rack::Utils.unescape(path))
if !@compiled_at || path.sub!(@compiled_at, '')
full_path = path.empty? ? @root : File.join(@root, ::Rack::Utils.unescape(path))
paths = "#{full_path}#{ext}"

matches = Dir[paths]
Expand Down
47 changes: 27 additions & 20 deletions actionpack/test/dispatch/static_test.rb
Expand Up @@ -2,30 +2,37 @@

module StaticTests
def test_serves_dynamic_content
assert_equal "Hello, World!", get("/nofile")
assert_equal "Hello, World!", get("/nofile").body
end

def test_serves_static_index_at_root
assert_equal "/index.html", get("/index.html")
assert_equal "/index.html", get("/index")
assert_equal "/index.html", get("/")
assert_html "/index.html", get("/index.html")
assert_html "/index.html", get("/index")
assert_html "/index.html", get("/")
assert_html "/index.html", get("")
end

def test_serves_static_file_in_directory
assert_equal "/foo/bar.html", get("/foo/bar.html")
assert_equal "/foo/bar.html", get("/foo/bar/")
assert_equal "/foo/bar.html", get("/foo/bar")
assert_html "/foo/bar.html", get("/foo/bar.html")
assert_html "/foo/bar.html", get("/foo/bar/")
assert_html "/foo/bar.html", get("/foo/bar")
end

def test_serves_static_index_file_in_directory
assert_equal "/foo/index.html", get("/foo/index.html")
assert_equal "/foo/index.html", get("/foo/")
assert_equal "/foo/index.html", get("/foo")
assert_html "/foo/index.html", get("/foo/index.html")
assert_html "/foo/index.html", get("/foo/")
assert_html "/foo/index.html", get("/foo")
end

private

def assert_html(body, response)
assert_equal body, response.body
assert_equal "text/html", response.headers["Content-Type"]
end

def get(path)
Rack::MockRequest.new(@app).request("GET", path).body
Rack::MockRequest.new(@app).request("GET", path)
end
end

Expand Down Expand Up @@ -59,16 +66,16 @@ def setup
include StaticTests

test "serves files from other mounted directories" do
assert_equal "/blog/index.html", get("/blog/index.html")
assert_equal "/blog/index.html", get("/blog/index")
assert_equal "/blog/index.html", get("/blog/")
assert_html "/blog/index.html", get("/blog/index.html")
assert_html "/blog/index.html", get("/blog/index")
assert_html "/blog/index.html", get("/blog/")

assert_equal "/blog/blog.html", get("/blog/blog/")
assert_equal "/blog/blog.html", get("/blog/blog.html")
assert_equal "/blog/blog.html", get("/blog/blog")
assert_html "/blog/blog.html", get("/blog/blog/")
assert_html "/blog/blog.html", get("/blog/blog.html")
assert_html "/blog/blog.html", get("/blog/blog")

assert_equal "/blog/subdir/index.html", get("/blog/subdir/index.html")
assert_equal "/blog/subdir/index.html", get("/blog/subdir/")
assert_equal "/blog/subdir/index.html", get("/blog/subdir")
assert_html "/blog/subdir/index.html", get("/blog/subdir/index.html")
assert_html "/blog/subdir/index.html", get("/blog/subdir/")
assert_html "/blog/subdir/index.html", get("/blog/subdir")
end
end

0 comments on commit 0b51f3c

Please sign in to comment.