You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Don't rebuild the middleware stack when using with_routing:
- Fixrails#54595
- ### Problem
When using the `with_routing` helper method in an integration test,
we rebuild the middleware stack so that the entrypoint
of our new stack is the new RouteSet object.
By rebuilding the middleware stack from scratch, we may end
un-configuring a middleware from an application. We also break
the contract of the Rails initialization process.
For instance, if you have a middleware that needs to be configured,
after the Rails routes are loaded, this is what it would look like:
```ruby
# config/application.rb
warden_config = nil
config.middleware.use Warden::Manager do |config|
# The Warden middleware yields its config when the middleware is
# instantiated. We can't configure warden yet as the routes
# are not yet loaded.
warden_config = config
end
initializer :configure_warden do
ActiveSupport.on_load(:after_routes_loaded) do
# The routes are now loaded, we can configure warden
end
end
```
This snippet follows the order of the documented Rails
initialization process where first the middleware stack is built
and then the routes gets loaded.
--------------------
By rebuilding the middleware stack, we unconfigure what was done
in the initializer.
### Solution
Since rebuilding the middleware stack has some caveats, my idea is
to modify the middleware stack original entry point and redefine
`call` so that it calls our new RouteSet middleware.
We have a reference to the original RouteSet middleware thanks to
the `application.routes` method.
0 commit comments