Skip to content
Browse files

Merge pull request #4879 from kennyj/fix_4873

Fix GH #4873. Allow swapping same class middleware.
  • Loading branch information...
1 parent 9aa4c6d commit 9cb0e12e46e0385b132dbe8716097225b7ef0863 @josevalim josevalim committed with josevalim
View
5 actionpack/lib/action_dispatch/middleware/stack.rb
@@ -93,8 +93,9 @@ def insert_after(index, *args, &block)
end
def swap(target, *args, &block)
- insert_before(target, *args, &block)
- delete(target)
+ index = assert_index(target, :before)
+ insert(index, *args, &block)
+ middlewares.delete_at(index + 1)
end
def delete(target)
View
6 actionpack/test/dispatch/middleware_stack_test.rb
@@ -81,6 +81,12 @@ def setup
assert_equal BazMiddleware, @stack[0].klass
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
assert_raise RuntimeError do
@stack.insert("HiyaMiddleware", BazMiddleware)

0 comments on commit 9cb0e12

Please sign in to comment.
Something went wrong with that request. Please try again.