diff options
author | José Valim <jose.valim@plataformatec.com.br> | 2012-02-04 06:31:59 -0800 |
---|---|---|
committer | José Valim <jose.valim@gmail.com> | 2012-02-04 15:32:40 +0100 |
commit | 9cb0e12e46e0385b132dbe8716097225b7ef0863 (patch) | |
tree | 2bc8d0700dd74f1ef6a87ce56ec70f2386972b64 | |
parent | 9aa4c6d7ce0707fe7e43bc919618d804836e1d71 (diff) | |
download | rails-9cb0e12e46e0385b132dbe8716097225b7ef0863.tar.gz rails-9cb0e12e46e0385b132dbe8716097225b7ef0863.tar.bz2 rails-9cb0e12e46e0385b132dbe8716097225b7ef0863.zip |
Merge pull request #4879 from kennyj/fix_4873
Fix GH #4873. Allow swapping same class middleware.
-rw-r--r-- | actionpack/lib/action_dispatch/middleware/stack.rb | 5 | ||||
-rw-r--r-- | actionpack/test/dispatch/middleware_stack_test.rb | 6 |
2 files changed, 9 insertions, 2 deletions
diff --git a/actionpack/lib/action_dispatch/middleware/stack.rb b/actionpack/lib/action_dispatch/middleware/stack.rb index a4308f528c..28e8fbdab8 100644 --- a/actionpack/lib/action_dispatch/middleware/stack.rb +++ b/actionpack/lib/action_dispatch/middleware/stack.rb @@ -93,8 +93,9 @@ module ActionDispatch 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) diff --git a/actionpack/test/dispatch/middleware_stack_test.rb b/actionpack/test/dispatch/middleware_stack_test.rb index 831f3db3e2..4191ed1ff4 100644 --- a/actionpack/test/dispatch/middleware_stack_test.rb +++ b/actionpack/test/dispatch/middleware_stack_test.rb @@ -81,6 +81,12 @@ class MiddlewareStackTest < ActiveSupport::TestCase 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) |