Skip to content

Commit

Permalink
Allow Rack::Runtime to be deleted from middleware stack.
Browse files Browse the repository at this point in the history
Fixes: rails#16433.
  • Loading branch information
tgxworld committed Feb 19, 2015
1 parent 28fccad commit a39498a
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 2 deletions.
8 changes: 8 additions & 0 deletions railties/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,11 @@
* `delete` operations in configurations are run last in order to eliminate
'No such middleware' errors when `insert_before` or `insert_after` are added
after the `delete` operation for the middleware being deleted.

Fixes: #16433.

*Guo Xiang Tan*

* Newly generated applications get a `README.md` in Markdown.

*Xavier Noria*
Expand Down
6 changes: 4 additions & 2 deletions railties/lib/rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ module Configuration
class MiddlewareStackProxy
def initialize
@operations = []
@delete_operations = []
end

def insert_before(*args, &block)
Expand All @@ -56,17 +57,18 @@ def use(*args, &block)
end

def delete(*args, &block)
@operations << [__method__, args, block]
@delete_operations << [__method__, args, block]
end

def unshift(*args, &block)
@operations << [__method__, args, block]
end

def merge_into(other) #:nodoc:
@operations.each do |operation, args, block|
@operations.concat(@delete_operations).each do |operation, args, block|
other.send(operation, *args, &block)
end

other
end
end
Expand Down
16 changes: 16 additions & 0 deletions railties/test/application/middleware_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,22 @@ def app
assert !middleware.include?("ActionDispatch::Static")
end

test "can delete a middleware from the stack even if insert_before is added after delete" do
add_to_config "config.middleware.delete Rack::Runtime"
add_to_config "config.middleware.insert_before(Rack::Runtime, Rack::Config)"
boot!
assert middleware.include?("Rack::Config")
assert_not middleware.include?("Rack::Runtime")
end

test "can delete a middleware from the stack even if insert_after is added after delete" do
add_to_config "config.middleware.delete Rack::Runtime"
add_to_config "config.middleware.insert_after(Rack::Runtime, Rack::Config)"
boot!
assert middleware.include?("Rack::Config")
assert_not middleware.include?("Rack::Runtime")
end

test "includes exceptions middlewares even if action_dispatch.show_exceptions is disabled" do
add_to_config "config.action_dispatch.show_exceptions = false"
boot!
Expand Down

0 comments on commit a39498a

Please sign in to comment.