Browse files

Ensure the proper content type is returned for static files.

  • Loading branch information...
1 parent 10d014a commit 0b51f3cc73ac21ed56b45a15fcce1d31beb7170c @josevalim josevalim committed Oct 4, 2010
Showing with 30 additions and 23 deletions.
  1. +3 −3 actionpack/lib/action_dispatch/middleware/static.rb
  2. +27 −20 actionpack/test/dispatch/static_test.rb
View
6 actionpack/lib/action_dispatch/middleware/static.rb
@@ -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]
View
47 actionpack/test/dispatch/static_test.rb
@@ -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
@@ -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.