diff options
author | Andrew White <andyw@pixeltrix.co.uk> | 2012-12-06 17:06:59 +0000 |
---|---|---|
committer | Andrew White <andyw@pixeltrix.co.uk> | 2012-12-06 17:20:53 +0000 |
commit | c59734f756b79c39486c45273d2cc5d42cd0c864 (patch) | |
tree | 3fc32497850a341872b896c09a0c065f218b18d8 | |
parent | 5a8f25f003f022ebc6986b20b0c10329e9553dc3 (diff) | |
download | rails-c59734f756b79c39486c45273d2cc5d42cd0c864.tar.gz rails-c59734f756b79c39486c45273d2cc5d42cd0c864.tar.bz2 rails-c59734f756b79c39486c45273d2cc5d42cd0c864.zip |
Invert precedence of content in ActionDispatch::Static
This commit inverts the precedence in ActionDispatch::Static so that
dynamic content will be served before static content. This is so that
precompiled assets do not inadvertently get included when running in
development mode - it should have no effect in production where static
files are usually handled by the web server.
Closes #6421
-rw-r--r-- | actionpack/CHANGELOG.md | 7 | ||||
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/static.rb | 18 | ||||
-rw-r--r-- | actionpack/test/dispatch/static_test.rb | 15 | ||||
-rw-r--r-- | railties/test/application/routing_test.rb | 23 |
4 files changed, 51 insertions, 12 deletions
diff --git a/actionpack/CHANGELOG.md b/actionpack/CHANGELOG.md index b57408ede3..43880f2412 100644 --- a/actionpack/CHANGELOG.md +++ b/actionpack/CHANGELOG.md @@ -1,4 +1,11 @@ ## Rails 4.0.0 (unreleased) ## + +* Invert precedence in `ActionDispatch::Static` so that dynamic content is preferred. + This prevents precompiled assets inadvertently being included twice when running + in development mode. Fixes #6421 + + *Andrew White* + * Add :if / :unless conditions to fragment cache: <%= cache @model, if: some_condition(@model) do %> diff --git a/actionpack/lib/action_dispatch/middleware/static.rb b/actionpack/lib/action_dispatch/middleware/static.rb index e3b15b43b9..65670b0ff1 100644 --- a/actionpack/lib/action_dispatch/middleware/static.rb +++ b/actionpack/lib/action_dispatch/middleware/static.rb @@ -51,16 +51,20 @@ module ActionDispatch end def call(env) - case env['REQUEST_METHOD'] - when 'GET', 'HEAD' - path = env['PATH_INFO'].chomp('/') - if match = @file_handler.match?(path) - env["PATH_INFO"] = match - return @file_handler.call(env) + path = env['PATH_INFO'].chomp('/') + response = @app.call(env) + + if response[1]['X-Cascade'] == 'pass' + case env['REQUEST_METHOD'] + when 'GET', 'HEAD' + if match = @file_handler.match?(path) + env["PATH_INFO"] = match + return @file_handler.call(env) + end end end - @app.call(env) + response end end end diff --git a/actionpack/test/dispatch/static_test.rb b/actionpack/test/dispatch/static_test.rb index 112f470786..f90be450e2 100644 --- a/actionpack/test/dispatch/static_test.rb +++ b/actionpack/test/dispatch/static_test.rb @@ -4,11 +4,19 @@ require 'rbconfig' module StaticTests def test_serves_dynamic_content + dummy_app = lambda { |env| [200, {"Content-Type" => "text/plain"}, ["Hello, World!"]] } + @app = ActionDispatch::Static.new(dummy_app, "#{FIXTURE_LOAD_PATH}/public", "public, max-age=60") assert_equal "Hello, World!", get("/nofile").body end + def test_dynamic_content_has_precedence_over_static_files + dummy_app = lambda { |env| [200, {"Content-Type" => "text/html"}, ["/foo/baz.html"]] } + @app = ActionDispatch::Static.new(dummy_app, "#{FIXTURE_LOAD_PATH}/public", "public, max-age=60") + assert_html "/foo/baz.html", get("/foo/bar.html") + end + def test_handles_urls_with_bad_encoding - assert_equal "Hello, World!", get("/doorkeeper%E3E4").body + assert_equal "", get("/doorkeeper%E3E4").body end def test_sets_cache_control @@ -40,7 +48,6 @@ module StaticTests assert_html "means hello in Japanese\n", get("/foo/#{Rack::Utils.escape("こんにちは.html")}") end - def test_serves_static_file_with_exclamation_mark_in_filename with_static_file "/foo/foo!bar.html" do |file| assert_html file, get("/foo/foo%21bar.html") @@ -142,9 +149,7 @@ module StaticTests end class StaticTest < ActiveSupport::TestCase - DummyApp = lambda { |env| - [200, {"Content-Type" => "text/plain"}, ["Hello, World!"]] - } + DummyApp = lambda { |env| [404, {"X-Cascade" => "pass"}, []] } App = ActionDispatch::Static.new(DummyApp, "#{FIXTURE_LOAD_PATH}/public", "public, max-age=60") def setup diff --git a/railties/test/application/routing_test.rb b/railties/test/application/routing_test.rb index ffcdeac7f0..0550afdb40 100644 --- a/railties/test/application/routing_test.rb +++ b/railties/test/application/routing_test.rb @@ -268,5 +268,28 @@ module ApplicationTests get '/yazilar' assert_equal 200, last_response.status end + + test 'routes take precedence over static files' do + app('development') + + app_file 'config/routes.rb', <<-RUBY + AppTemplate::Application.routes.draw do + get 'foo', to: 'foo#index' + end + RUBY + + app_file 'public/foo.json', '{"foo":"bar"}' + + controller :foo, <<-RUBY + class FooController < ApplicationController + def index + render json: { foo: 'baz' } + end + end + RUBY + + get '/foo.json' + assert_equal '{"foo":"baz"}', last_response.body + end end end |