diff options
author | Matthew Draper <matthew@trebex.net> | 2016-12-31 08:20:35 +1030 |
---|---|---|
committer | Matthew Draper <matthew@trebex.net> | 2016-12-31 08:40:01 +1030 |
commit | 6b126ffdcd8031bfe83fc311a230799c05e2c955 (patch) | |
tree | b1ddc4ce35782de91aa12d360536aee9d58671f0 /railties/test | |
parent | 2c5190e3299c83a357c8bf8010d6ca7bff99c1b5 (diff) | |
download | rails-6b126ffdcd8031bfe83fc311a230799c05e2c955.tar.gz rails-6b126ffdcd8031bfe83fc311a230799c05e2c955.tar.bz2 rails-6b126ffdcd8031bfe83fc311a230799c05e2c955.zip |
Enforce middleware ordering with a test, instead of comments
We want the actual order to be very predictable, so it's rightly defined
in code -- not with an on-the-fly tsort.
But we can do the tsort here, and then verify that it matches the
implemented ordering. This way we don't leave future readers guessing
which parts of the ordering are deliberate and which are arbitrary.
Diffstat (limited to 'railties/test')
-rw-r--r-- | railties/test/application/middleware_test.rb | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/railties/test/application/middleware_test.rb b/railties/test/application/middleware_test.rb index 71d822bb41..0a6e5b52e9 100644 --- a/railties/test/application/middleware_test.rb +++ b/railties/test/application/middleware_test.rb @@ -30,8 +30,8 @@ module ApplicationTests "Rack::Runtime", "Rack::MethodOverride", "ActionDispatch::RequestId", - "ActionDispatch::RemoteIp", # Must come before Rails::Rack::Logger to properly log request_id - "Rails::Rack::Logger", # must come after Rack::MethodOverride to properly log overridden methods + "ActionDispatch::RemoteIp", + "Rails::Rack::Logger", "ActionDispatch::ShowExceptions", "ActionDispatch::DebugExceptions", "ActionDispatch::Reloader", @@ -59,7 +59,7 @@ module ApplicationTests "Rack::Runtime", "ActionDispatch::RequestId", "ActionDispatch::RemoteIp", - "Rails::Rack::Logger", # must come after Rack::MethodOverride to properly log overridden methods + "Rails::Rack::Logger", "ActionDispatch::ShowExceptions", "ActionDispatch::DebugExceptions", "ActionDispatch::Reloader", @@ -70,6 +70,37 @@ module ApplicationTests ], middleware end + test "middleware dependencies" do + boot! + + # The following array-of-arrays describes dependencies between + # middlewares: the first item in each list depends on the + # remaining items (and therefore must occur later in the + # middleware stack). + + dependencies = [ + # Logger needs a fully "corrected" request environment + %w(Rails::Rack::Logger Rack::MethodOverride ActionDispatch::RequestId ActionDispatch::RemoteIp), + + # Serving public/ doesn't invoke user code, so it should skip + # locks etc + %w(ActionDispatch::Executor ActionDispatch::Static), + + # Errors during reload must be reported + %w(ActionDispatch::Reloader ActionDispatch::ShowExceptions ActionDispatch::DebugExceptions), + + # Outright dependencies + %w(ActionDispatch::Static Rack::Sendfile), + %w(ActionDispatch::Flash ActionDispatch::Session::CookieStore), + %w(ActionDispatch::Session::CookieStore ActionDispatch::Cookies), + ] + + require "tsort" + sorted = TSort.tsort((middleware | dependencies.flatten).method(:each), + lambda { |n, &b| dependencies.each { |m, *ds| ds.each(&b) if m == n } }) + assert_equal sorted, middleware + end + test "Rack::Cache is not included by default" do boot! |