Skip to content

Commit

Permalink
Fix deprecation warning when booting puma using rack 3 (#2706)
Browse files Browse the repository at this point in the history
edtect and prefer `Rackup::Handler::Puma` if present.

Fixes #2705
  • Loading branch information
mattbrictson authored Oct 8, 2023
1 parent 0b43cb9 commit 49e3faf
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 6 deletions.
13 changes: 7 additions & 6 deletions lib/capybara/registrations/servers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -27,19 +27,20 @@
require 'rack/handler/puma'
rescue LoadError
raise LoadError, 'Capybara is unable to load `puma` for its server, please add `puma` to your project or specify a different server via something like `Capybara.server = :webrick`.'
else
unless Rack::Handler::Puma.respond_to?(:config)
raise LoadError, 'Capybara requires `puma` version 3.8.0 or higher, please upgrade `puma` or register and specify your own server block'
end
end
puma_rack_handler = defined?(Rackup::Handler::Puma) ? Rackup::Handler::Puma : Rack::Handler::Puma

unless puma_rack_handler.respond_to?(:config)
raise LoadError, 'Capybara requires `puma` version 3.8.0 or higher, please upgrade `puma` or register and specify your own server block'
end

# If we just run the Puma Rack handler it installs signal handlers which prevent us from being able to interrupt tests.
# Therefore construct and run the Server instance ourselves.
# Rack::Handler::Puma.run(app, { Host: host, Port: port, Threads: "0:4", workers: 0, daemon: false }.merge(options))
# puma_rack_handler.run(app, { Host: host, Port: port, Threads: "0:4", workers: 0, daemon: false }.merge(options))
default_options = { Host: host, Port: port, Threads: '0:4', workers: 0, daemon: false }
options = default_options.merge(options)

conf = Rack::Handler::Puma.config(app, options)
conf = puma_rack_handler.config(app, options)
conf.clamp

puma_ver = Gem::Version.new(Puma::Const::PUMA_VERSION)
Expand Down
12 changes: 12 additions & 0 deletions spec/server_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,18 @@
Capybara.server = :default
end

it 'should not emit any warnings when booting puma' do
Capybara.server = :puma
app_proc = proc { |_env| [200, {}, ['Hello Puma!']] }
require 'puma'

expect {
described_class.new(app_proc).boot
}.not_to output.to_stderr
ensure
Capybara.server = :default
end

it 'should support SSL' do
key = File.join(Dir.pwd, 'spec', 'fixtures', 'key.pem')
cert = File.join(Dir.pwd, 'spec', 'fixtures', 'certificate.pem')
Expand Down

0 comments on commit 49e3faf

Please sign in to comment.