2017-08-14 13:08:09 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2016-08-06 13:16:09 -04:00
|
|
|
require "active_support"
|
|
|
|
require "active_support/testing/autorun"
|
|
|
|
require "rails/configuration"
|
|
|
|
require "active_support/test_case"
|
|
|
|
require "minitest/mock"
|
2012-05-21 19:20:18 -04:00
|
|
|
|
|
|
|
module Rails
|
|
|
|
module Configuration
|
|
|
|
class MiddlewareStackProxyTest < ActiveSupport::TestCase
|
|
|
|
def setup
|
|
|
|
@stack = MiddlewareStackProxy.new
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_playback_insert_before
|
|
|
|
@stack.insert_before :foo
|
|
|
|
assert_playback :insert_before, :foo
|
|
|
|
end
|
|
|
|
|
Delayed middleware delete does not allow move operations
While trying to fix #16433, we made the middleware deletions always
happen at the end. While this works for the case of deleting the
Rack::Runtime middleware, it makes operations like the following
misbehave.
```ruby
gem "bundler", "< 1.16"
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
end
require "action_controller/railtie"
class TestApp < Rails::Application
config.root = __dir__
secrets.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
middleware.insert_after ActionDispatch::Session::CookieStore, ::Rails::Rack::Logger, config.log_tags
middleware.delete ::Rails::Rack::Logger
end
require "minitest/autorun"
require "rack/test"
class BugTest < Minitest::Test
include Rack::Test::Methods
def test_returns_success
get "/"
assert last_response.ok?
end
private
def app
Rails.application
end
end
```
In the case ☝️ the ::Rails::Rack::Logger would be deleted instead of
moved, because the order of middleware stack building execution will be:
```ruby
[:insert, ActionDispatch::Session::CookieStore, [::Rails::Rack::Logger]]
[:delete, ::Rails::Rack::Logger, [config.log_tags]]
```
This is pretty surprising and hard to reason about behaviour, unless you
go spelunking into the Rails configuration code.
I have a few solutions in mind and all of them have their drawbacks.
1. Introduce a `Rails::Configuration::MiddlewareStackProxy#delete!` that
delays the deleted operations. This will make `#delete` to be executed
in order. The drawback here is backwards incompatible behavior and a new
public method.
2. Just revert to the old operations. This won't allow people to delete
the `Rack::Runtime` middleware.
3. Legitimize the middleware moving with the new `#move_after` and
`#move_before` methods. This does not breaks any backwards
compatibility, but includes 2 new methods to the middleware stack.
I have implemented `3.` in this pull request.
Happy holidays! 🎄
2020-01-07 13:20:03 -05:00
|
|
|
def test_playback_insert
|
|
|
|
@stack.insert :foo
|
|
|
|
assert_playback :insert_before, :foo
|
|
|
|
end
|
|
|
|
|
2012-05-21 19:20:18 -04:00
|
|
|
def test_playback_insert_after
|
|
|
|
@stack.insert_after :foo
|
|
|
|
assert_playback :insert_after, :foo
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_playback_swap
|
|
|
|
@stack.swap :foo
|
|
|
|
assert_playback :swap, :foo
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_playback_use
|
|
|
|
@stack.use :foo
|
|
|
|
assert_playback :use, :foo
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_playback_delete
|
|
|
|
@stack.delete :foo
|
|
|
|
assert_playback :delete, :foo
|
|
|
|
end
|
|
|
|
|
Delayed middleware delete does not allow move operations
While trying to fix #16433, we made the middleware deletions always
happen at the end. While this works for the case of deleting the
Rack::Runtime middleware, it makes operations like the following
misbehave.
```ruby
gem "bundler", "< 1.16"
begin
require "bundler/inline"
rescue LoadError => e
$stderr.puts "Bundler version 1.10 or later is required. Please update your Bundler"
raise e
end
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: "rails/rails"
end
require "action_controller/railtie"
class TestApp < Rails::Application
config.root = __dir__
secrets.secret_key_base = "secret_key_base"
config.logger = Logger.new($stdout)
Rails.logger = config.logger
middleware.insert_after ActionDispatch::Session::CookieStore, ::Rails::Rack::Logger, config.log_tags
middleware.delete ::Rails::Rack::Logger
end
require "minitest/autorun"
require "rack/test"
class BugTest < Minitest::Test
include Rack::Test::Methods
def test_returns_success
get "/"
assert last_response.ok?
end
private
def app
Rails.application
end
end
```
In the case ☝️ the ::Rails::Rack::Logger would be deleted instead of
moved, because the order of middleware stack building execution will be:
```ruby
[:insert, ActionDispatch::Session::CookieStore, [::Rails::Rack::Logger]]
[:delete, ::Rails::Rack::Logger, [config.log_tags]]
```
This is pretty surprising and hard to reason about behaviour, unless you
go spelunking into the Rails configuration code.
I have a few solutions in mind and all of them have their drawbacks.
1. Introduce a `Rails::Configuration::MiddlewareStackProxy#delete!` that
delays the deleted operations. This will make `#delete` to be executed
in order. The drawback here is backwards incompatible behavior and a new
public method.
2. Just revert to the old operations. This won't allow people to delete
the `Rack::Runtime` middleware.
3. Legitimize the middleware moving with the new `#move_after` and
`#move_before` methods. This does not breaks any backwards
compatibility, but includes 2 new methods to the middleware stack.
I have implemented `3.` in this pull request.
Happy holidays! 🎄
2020-01-07 13:20:03 -05:00
|
|
|
def test_playback_move_before
|
|
|
|
@stack.move_before :foo
|
|
|
|
assert_playback :move_before, :foo
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_playback_move
|
|
|
|
@stack.move :foo
|
|
|
|
assert_playback :move_before, :foo
|
|
|
|
end
|
|
|
|
|
|
|
|
def test_playback_move_after
|
|
|
|
@stack.move_after :foo
|
|
|
|
assert_playback :move_after, :foo
|
|
|
|
end
|
|
|
|
|
2012-05-21 19:20:18 -04:00
|
|
|
def test_order
|
|
|
|
@stack.swap :foo
|
|
|
|
@stack.delete :foo
|
|
|
|
|
2013-12-18 02:51:24 -05:00
|
|
|
mock = Minitest::Mock.new
|
2020-10-06 15:58:46 -04:00
|
|
|
mock.expect :swap, nil, [:foo]
|
|
|
|
mock.expect :delete, nil, [:foo]
|
2012-05-21 19:20:18 -04:00
|
|
|
|
|
|
|
@stack.merge_into mock
|
|
|
|
mock.verify
|
|
|
|
end
|
|
|
|
|
|
|
|
private
|
2016-08-06 13:55:02 -04:00
|
|
|
def assert_playback(msg_name, args)
|
|
|
|
mock = Minitest::Mock.new
|
2020-10-06 15:58:46 -04:00
|
|
|
mock.expect msg_name, nil, [args]
|
2016-08-06 13:55:02 -04:00
|
|
|
@stack.merge_into(mock)
|
|
|
|
mock.verify
|
|
|
|
end
|
2012-05-21 19:20:18 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|