1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Fix GH #4873. Allow swapping middleware of same class

This commit is contained in:
kennyj 2012-02-04 12:08:39 +09:00
parent 7a72fdc7dc
commit e7ec969895
2 changed files with 9 additions and 2 deletions

View file

@ -93,8 +93,9 @@ module ActionDispatch
end end
def swap(target, *args, &block) def swap(target, *args, &block)
insert_before(target, *args, &block) index = assert_index(target, :before)
delete(target) insert(index, *args, &block)
middlewares.delete_at(index + 1)
end end
def delete(target) def delete(target)

View file

@ -81,6 +81,12 @@ class MiddlewareStackTest < ActiveSupport::TestCase
assert_equal BazMiddleware, @stack[0].klass assert_equal BazMiddleware, @stack[0].klass
end end
test "swaps one middleware out for same middleware class" do
assert_equal FooMiddleware, @stack[0].klass
@stack.swap(FooMiddleware, FooMiddleware, Proc.new { |env| [500, {}, ['error!']] })
assert_equal FooMiddleware, @stack[0].klass
end
test "raise an error on invalid index" do test "raise an error on invalid index" do
assert_raise RuntimeError do assert_raise RuntimeError do
@stack.insert("HiyaMiddleware", BazMiddleware) @stack.insert("HiyaMiddleware", BazMiddleware)