Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Set temporary name for middleware subclass #344

Merged
merged 1 commit into from
Dec 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions lib/roda/plugins/middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,7 @@ class Forwarder
# and store +app+ as the next middleware to call.
def initialize(mid, app, *args, &block)
@mid = Class.new(mid)
@mid.set_temporary_name("#{mid.name}(middleware)") if mid.name && RUBY_VERSION >= "3.3"
if @mid.opts[:middleware_next_if_not_found]
@mid.plugin(:not_found, &NEXT_PROC)
end
Expand Down
20 changes: 20 additions & 0 deletions spec/plugin/middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,26 @@ def call
body.must_equal 'a'
end

it "sets temporary name of the subclass" do
app(:middleware) do |r|
r.get{self.class.name || "anonymous"}
end
a = app
app(:bare) do
use a
route {}
end
body.must_equal 'anonymous'

Object.const_set(:MyApp, a)
app(:bare) do
use a
route {}
end
body.must_equal 'MyApp(middleware)'
Object.send(:remove_const, :MyApp)
end if RUBY_VERSION >= "3.3"

it "should raise error if attempting to use options for Roda application that does not support configurable middleware" do
a1 = app(:bare){plugin :middleware}
proc{app(:bare){use a1, :foo; route{}; build_rack_app}}.must_raise Roda::RodaError
Expand Down
Loading