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

Fix usage of inherited Sinatra::Base classes keyword arguments #1670

Merged
4 changes: 2 additions & 2 deletions lib/sinatra/base.rb
Expand Up @@ -1522,8 +1522,8 @@ def prototype
# Create a new instance of the class fronted by its middleware
# pipeline. The object is guaranteed to respond to #call but may not be
# an instance of the class new was called on.
def new(*args, &bk)
instance = new!(*args, &bk)
def new(*args, **kwargs, &bk)
instance = kwargs.length > 0 ? new!(**kwargs, &bk) : new!(*args, &bk)
jkowens marked this conversation as resolved.
Show resolved Hide resolved
Wrapper.new(build(instance).to_app, instance)
end

Expand Down
18 changes: 18 additions & 0 deletions test/base_test.rb
Expand Up @@ -6,6 +6,14 @@ class TestApp < Sinatra::Base
get('/') { 'Hello World' }
end

class TestKeywordArgumentInitializerApp < Sinatra::Base
def initialize(argument:)
@argument = argument
end

get('/') { "Hello World with Keyword Arguments: #{@argument}" }
end

it 'include Rack::Utils' do
assert TestApp.included_modules.include?(Rack::Utils)
end
Expand Down Expand Up @@ -48,6 +56,16 @@ class TestApp < Sinatra::Base
TestApp.configure { context = self }
assert_equal self, context
end

it "allows constructor to receive keyword arguments" do
app = TestKeywordArgumentInitializerApp.new(argument: "some argument")
request = Rack::MockRequest.new(app)

response = request.get('/')

assert response.ok?
assert_equal 'Hello World with Keyword Arguments: some argument', response.body
end
end

describe "Sinatra::Base#new" do
Expand Down