diff options
author | Jeremy Kemper <jeremy@bitsweat.net> | 2012-04-20 12:17:03 -0700 |
---|---|---|
committer | Jeremy Kemper <jeremy@bitsweat.net> | 2012-04-20 16:02:12 -0700 |
commit | 42f6e9fb38101c0fe199d5425bd71965f9a6dfc9 (patch) | |
tree | 88df125892d8bcdf141bba092c03b4412893178c | |
parent | 99eae3f82a5a2b7c5e485ee1d370852e65ab8bde (diff) | |
download | rails-42f6e9fb38101c0fe199d5425bd71965f9a6dfc9.tar.gz rails-42f6e9fb38101c0fe199d5425bd71965f9a6dfc9.tar.bz2 rails-42f6e9fb38101c0fe199d5425bd71965f9a6dfc9.zip |
Freeze the middleware stack after it's built
So apps that accidentally add middlewares later aren't unwittingly dumping them in a black hole.
Closes #5911
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/stack.rb | 2 | ||||
-rw-r--r-- | railties/test/application/middleware/exceptions_test.rb | 35 | ||||
-rw-r--r-- | railties/test/application/middleware_test.rb | 7 |
3 files changed, 26 insertions, 18 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb index 28e8fbdab8..12bc438be3 100644 --- a/actionpack/lib/action_dispatch/middleware/stack.rb +++ b/actionpack/lib/action_dispatch/middleware/stack.rb @@ -110,7 +110,7 @@ module ActionDispatch def build(app = nil, &block) app ||= block raise "MiddlewareStack#build requires an app" unless app - middlewares.reverse.inject(app) { |a, e| e.build(a) } + middlewares.freeze.reverse.inject(app) { |a, e| e.build(a) } end protected diff --git a/railties/test/application/middleware/exceptions_test.rb b/railties/test/application/middleware/exceptions_test.rb index a80898092d..c5048afa13 100644 --- a/railties/test/application/middleware/exceptions_test.rb +++ b/railties/test/application/middleware/exceptions_test.rb @@ -17,31 +17,32 @@ module ApplicationTests end test "show exceptions middleware filter backtrace before logging" do - my_middleware = Struct.new(:app) do - def call(env) - raise "Failure" + controller :foo, <<-RUBY + class FooController < ActionController::Base + def index + raise 'oops' + end end - end - - app.config.middleware.use my_middleware + RUBY - stringio = StringIO.new - Rails.logger = Logger.new(stringio) + get "/foo" + assert_equal 500, last_response.status - get "/" - assert_no_match(/action_dispatch/, stringio.string) + log = File.read(Rails.application.config.paths["log"].first) + assert_no_match(/action_dispatch/, log, log) + assert_match(/oops/, log, log) end test "renders active record exceptions as 404" do - my_middleware = Struct.new(:app) do - def call(env) - raise ActiveRecord::RecordNotFound + controller :foo, <<-RUBY + class FooController < ActionController::Base + def index + raise ActiveRecord::RecordNotFound + end end - end - - app.config.middleware.use my_middleware + RUBY - get "/" + get "/foo" assert_equal 404, last_response.status end diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb index fc5fb60174..ba747bc633 100644 --- a/railties/test/application/middleware_test.rb +++ b/railties/test/application/middleware_test.rb @@ -148,6 +148,13 @@ module ApplicationTests assert_equal "Rack::Config", middleware.first end + test "can't change middleware after it's built" do + boot! + assert_raise RuntimeError do + app.config.middleware.use Rack::Config + end + end + # ConditionalGet + Etag test "conditional get + etag middlewares handle http caching based on body" do make_basic_app |