|
| 1 | +# This is a backport of a fix that was included in capybara 3.40.0 which was also Ruby version locked to 3.0+ |
| 2 | +if RUBY_VERSION.to_f < 3 && Rails::VERSION::STRING.to_f >= 7.1 |
| 3 | + Capybara.register_server :puma do |app, port, host, **options| |
| 4 | + begin |
| 5 | + require 'rackup' |
| 6 | + rescue LoadError # rubocop:disable Lint/SuppressedException |
| 7 | + end |
| 8 | + begin |
| 9 | + require 'rack/handler/puma' |
| 10 | + rescue LoadError |
| 11 | + raise LoadError, 'Capybara is unable to load `puma` for its server, please add `puma` ' \ |
| 12 | + 'to your project or specify a different server via something like `Capybara.server = :webrick`.' |
| 13 | + end |
| 14 | + puma_rack_handler = defined?(Rackup::Handler::Puma) ? Rackup::Handler::Puma : Rack::Handler::Puma |
| 15 | + |
| 16 | + unless puma_rack_handler.respond_to?(:config) |
| 17 | + raise LoadError, 'Capybara requires `puma` version 3.8.0 or higher,' \ |
| 18 | + ' please upgrade `puma` or register and specify your own server block' |
| 19 | + end |
| 20 | + |
| 21 | + # If we just run the Puma Rack handler it installs signal handlers which prevent us from being able to interrupt tests. |
| 22 | + # Therefore construct and run the Server instance ourselves. |
| 23 | + # puma_rack_handler.run(app, { Host: host, Port: port, Threads: "0:4", workers: 0, daemon: false }.merge(options)) |
| 24 | + default_options = { Host: host, Port: port, Threads: '0:4', workers: 0, daemon: false } |
| 25 | + options = default_options.merge(options) |
| 26 | + |
| 27 | + conf = puma_rack_handler.config(app, options) |
| 28 | + conf.clamp |
| 29 | + |
| 30 | + puma_ver = Gem::Version.new(Puma::Const::PUMA_VERSION) |
| 31 | + require 'capybara/registrations/patches/puma_ssl' if Gem::Requirement.new('>=4.0.0', '< 4.1.0').satisfied_by?(puma_ver) |
| 32 | + |
| 33 | + logger = (defined?(Puma::LogWriter) ? Puma::LogWriter : Puma::Events).then do |cls| |
| 34 | + conf.options[:Silent] ? cls.strings : cls.stdio |
| 35 | + end |
| 36 | + conf.options[:log_writer] = logger |
| 37 | + |
| 38 | + logger.log 'Capybara starting Puma...' |
| 39 | + logger.log "* Version #{Puma::Const::PUMA_VERSION}, codename: #{Puma::Const::CODE_NAME}" |
| 40 | + logger.log "* Min threads: #{conf.options[:min_threads]}, max threads: #{conf.options[:max_threads]}" |
| 41 | + |
| 42 | + Puma::Server.new( |
| 43 | + conf.app, |
| 44 | + defined?(Puma::LogWriter) ? nil : logger, |
| 45 | + conf.options |
| 46 | + ).tap do |s| |
| 47 | + s.binder.parse conf.options[:binds], (s.log_writer rescue s.events) # rubocop:disable Style/RescueModifier |
| 48 | + s.min_threads, s.max_threads = conf.options[:min_threads], conf.options[:max_threads] if s.respond_to? :min_threads= |
| 49 | + end.run.join |
| 50 | + end |
| 51 | +end |
0 commit comments