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

Add Middleware#remove to delete middleware or raise if not found.

`Middleware#remove` works just like `Middleware#delete` but will
raise an error if the middleware isn't found.

This partly reverts 07558ff57c, where
`delete` would raise an error.
It might be expected that `delete` fails silently for some environments.
Or maybe you want to make sure a middleware isn't present.

Co-authored-by: Alex Ghiculescu <alexghiculescu@gmail.com>
This commit is contained in:
Petrik 2021-07-20 18:06:01 +02:00
parent acfadacb07
commit 688ed7084a
3 changed files with 30 additions and 14 deletions

View file

@ -1,3 +1,10 @@
* Add `Middleware#remove` to delete middleware or raise if not found.
`Middleware#remove` works just like `Middleware#delete` but will
raise an error if the middleware isn't found.
*Alex Ghiculescu*, *Petrik de Heus*
* Exclude additional flash types from `ActionController::Base.action_methods`.
Ensures that additional flash types defined on ActionController::Base subclasses
@ -11,13 +18,6 @@
*Gavin Morrice*
* Deleting an item from the Middleware stack will raise if the item is not found
Previously, calling `config.middleware.delete(ItemNotInMiddleware)` would fail silently.
Now it will raise, same as `config.middleware.move(0, ItemNotInMiddleware)` does.
*Alex Ghiculescu*
* OpenSSL constants are now used for Digest computations.
*Dirkjan Bussink*

View file

@ -130,7 +130,11 @@ module ActionDispatch
ruby2_keywords(:swap)
def delete(target)
middlewares.reject! { |m| m.name == target.name } || (raise "No such middleware to delete: #{target.inspect}")
middlewares.reject! { |m| m.name == target.name }
end
def remove(target)
delete(target) || (raise "No such middleware to remove: #{target.inspect}")
end
def move(target, source)

View file

@ -37,6 +37,24 @@ class MiddlewareStackTest < ActiveSupport::TestCase
end
end
test "delete ignores middleware not in the stack" do
assert_no_difference "@stack.size" do
@stack.delete BazMiddleware
end
end
test "remove deletes the middleware" do
assert_difference "@stack.size", -1 do
@stack.remove FooMiddleware
end
end
test "remove requires the middleware to be in the stack" do
assert_raises RuntimeError do
@stack.remove BazMiddleware
end
end
test "use should push middleware as class onto the stack" do
assert_difference "@stack.size" do
@stack.use BazMiddleware
@ -105,12 +123,6 @@ class MiddlewareStackTest < ActiveSupport::TestCase
end
end
test "delete requires the middleware to be in the stack" do
assert_raises RuntimeError do
@stack.delete(BazMiddleware)
end
end
test "move preserves the arguments of the moved middleware" do
@stack.use BazMiddleware, true, foo: "bar"
@stack.move_before(FooMiddleware, BazMiddleware)