Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Ensure application's middleware stack to mount ordered, unique Rack m…
…iddleware
- Loading branch information
Showing
4 changed files
with
60 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
RSpec.describe Hanami::Middleware, type: :cli do | ||
describe "#load!" do | ||
it "loads the middleware stack without duplicates" do | ||
with_project do | ||
generate "action web home#index" | ||
replace "apps/web/application.rb", "Application < Hanami::Application", <<-END | ||
class RackApp | ||
def initialize(app, options = {}, &blk) | ||
@app = app | ||
@options = options | ||
@blk = blk | ||
end | ||
def call(env) | ||
@app.call(env) | ||
end | ||
end | ||
class Application < Hanami::Application | ||
END | ||
|
||
replace "apps/web/application.rb", "configure do", <<-END | ||
configure do | ||
block1 = ->() {} | ||
block2 = ->() {} | ||
block3 = ->() {} | ||
middleware.use Web::RackApp, foo: :bar | ||
middleware.use Web::RackApp, foo: :bar # this is a duplicate and it shouldn't be included | ||
middleware.use Web::RackApp, baz: :bat | ||
middleware.use Web::RackApp, &block1 | ||
middleware.use Web::RackApp, &block1 # this is a duplicate and it shouldn't be included | ||
middleware.use Web::RackApp, &block2 | ||
middleware.prepend Web::RackApp, foo: :bar # this is a duplicate and it shouldn't be included | ||
middleware.prepend Web::RackApp, cap: :tain | ||
middleware.prepend Web::RackApp, &block1 # this is a duplicate and it shouldn't be included | ||
middleware.prepend Web::RackApp, &block2 # this is a duplicate and it shouldn't be included | ||
middleware.prepend Web::RackApp, &block3 | ||
END | ||
|
||
require Pathname.new(Dir.pwd).join("config", "environment") | ||
Hanami::Components.resolve('all') | ||
|
||
middleware = Web::Application.new.__send__(:middleware) | ||
|
||
# We tried to mount Web::RackApp 11 times. | ||
# | ||
# We have 5 duplicates marked inline. | ||
# | ||
# That leads us to 6 (11 - 5) mounted middleware. | ||
expect(middleware.__send__(:stack).count).to be(6) | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
115b330
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Isn't this a bandaid more than a fix? It does not address the reason why a given middleware would be pushed more than once on the stack.
115b330
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@beauby Hi! If you look at the parent commit, that ensures configurations are evaluated only once.
This commit introduces a secondary mechanism of defense and unit tests to prevent regressions.
115b330
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh sorry – I had assumed that the base issue hadn't been fixed. However, I am not convinced that silently suppressing duplicate middlewares makes sense then. Regression tests on the other hand are super helpful 👍.
115b330
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
115b330
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
115b330
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I realize my wording was confusing. What I meant was:
uniq!
on the middlewares stack is now useless. It is intended as a "second mechanism of defence", but all it will do is make the issue unnoticeable should a new bug that loads components multiple times arise.