diff --git a/lib/rack/handler/puma.rb b/lib/rack/handler/puma.rb index 976dd24e08..cf448f286a 100644 --- a/lib/rack/handler/puma.rb +++ b/lib/rack/handler/puma.rb @@ -120,7 +120,8 @@ class << self register :puma, Puma end end -elsif Object.const_defined?(:Rack) && Rack::RELEASE < '3' +else + do_register = Object.const_defined?(:Rack) && Rack::RELEASE < '3' module Rack module Handler module Puma @@ -128,9 +129,7 @@ class << self include ::Puma::RackHandler end end - register :puma, Puma end end -else - raise "You must install the rackup gem when using Rack 3" + ::Rack::Handler.register(:puma, ::Rack::Handler::Puma) if do_register end diff --git a/test/test_rack_handler.rb b/test/test_rack_handler.rb index c89cb8036a..76001d47a4 100644 --- a/test/test_rack_handler.rb +++ b/test/test_rack_handler.rb @@ -1,25 +1,10 @@ require_relative "helper" +# Most tests check that ::Rack::Handler::Puma works by itself +# RackUp#test_bin runs Puma using the rackup bin file module TestRackUp - if ENV.key? "PUMA_CI_RACK_2" - require "rack" - RACK_HANDLER_MOD = ::Rack::Handler - else - require "rackup" - RACK_HANDLER_MOD = ::Rackup::Handler - end - require "rack/handler/puma" - class TestHandlerGetStrSym < Minitest::Test - def test_handler - handler = RACK_HANDLER_MOD.get(:puma) - assert_equal RACK_HANDLER_MOD::Puma, handler - handler = RACK_HANDLER_MOD.get('Puma') - assert_equal RACK_HANDLER_MOD::Puma, handler - end - end - class TestPathHandler < Minitest::Test def app Proc.new {|env| @input = env; [200, {}, ["hello world"]]} @@ -35,7 +20,7 @@ def in_handler(app, options = {}) @launcher = nil thread = Thread.new do - RACK_HANDLER_MOD::Puma.run(app, **options) do |s, p| + ::Rack::Handler::Puma.run(app, **options) do |s, p| @launcher = s end end @@ -79,7 +64,7 @@ def test_port_wins_over_config File.open("config/puma.rb", "w") { |f| f << "port #{file_port}" } @options[:Port] = user_port - conf = RACK_HANDLER_MOD::Puma.config(->{}, @options) + conf = ::Rack::Handler::Puma.config(->{}, @options) conf.load assert_equal ["tcp://0.0.0.0:#{user_port}"], conf.options[:binds] @@ -100,7 +85,7 @@ def test_host_uses_supplied_port_default @options[:Host] = user_host @options[:Port] = user_port - conf = RACK_HANDLER_MOD::Puma.config(->{}, @options) + conf = ::Rack::Handler::Puma.config(->{}, @options) conf.load assert_equal ["tcp://#{user_host}:#{user_port}"], conf.options[:binds] @@ -108,7 +93,7 @@ def test_host_uses_supplied_port_default def test_ipv6_host_supplied_port_default @options[:Host] = "::1" - conf = RACK_HANDLER_MOD::Puma.config(->{}, @options) + conf = ::Rack::Handler::Puma.config(->{}, @options) conf.load assert_equal ["tcp://[::1]:9292"], conf.options[:binds] @@ -131,7 +116,7 @@ def test_config_file_wins_over_port File.open("config/puma.rb", "w") { |f| f << "port #{file_port}" } @options[:Port] = user_port - conf = RACK_HANDLER_MOD::Puma.config(->{}, @options) + conf = ::Rack::Handler::Puma.config(->{}, @options) conf.load assert_equal ["tcp://0.0.0.0:#{file_port}"], conf.options[:binds] @@ -150,7 +135,7 @@ def test_default_host_when_using_config_file @options[:Host] = "localhost" @options[:Port] = user_port - conf = RACK_HANDLER_MOD::Puma.config(->{}, @options) + conf = ::Rack::Handler::Puma.config(->{}, @options) conf.load assert_equal ["tcp://localhost:#{file_port}"], conf.options[:binds] @@ -169,7 +154,7 @@ def test_default_host_when_using_config_file_with_explicit_host @options[:Host] = "localhost" @options[:Port] = user_port - conf = RACK_HANDLER_MOD::Puma.config(->{}, @options) + conf = ::Rack::Handler::Puma.config(->{}, @options) conf.load assert_equal ["tcp://1.2.3.4:#{file_port}"], conf.options[:binds] @@ -184,7 +169,7 @@ def setup end def test_default_port_when_no_config_file - conf = RACK_HANDLER_MOD::Puma.config(->{}, @options) + conf = ::Rack::Handler::Puma.config(->{}, @options) conf.load assert_equal ["tcp://0.0.0.0:9292"], conf.options[:binds] @@ -198,7 +183,7 @@ def test_config_wins_over_default FileUtils.mkdir("config") File.open("config/puma.rb", "w") { |f| f << "port #{file_port}" } - conf = RACK_HANDLER_MOD::Puma.config(->{}, @options) + conf = ::Rack::Handler::Puma.config(->{}, @options) conf.load assert_equal ["tcp://0.0.0.0:#{file_port}"], conf.options[:binds] @@ -210,7 +195,7 @@ def test_user_port_wins_over_default_when_user_supplied_is_blank user_port = 5001 @options[:user_supplied_options] = [] @options[:Port] = user_port - conf = RACK_HANDLER_MOD::Puma.config(->{}, @options) + conf = ::Rack::Handler::Puma.config(->{}, @options) conf.load assert_equal ["tcp://0.0.0.0:#{user_port}"], conf.options[:binds] @@ -219,7 +204,7 @@ def test_user_port_wins_over_default_when_user_supplied_is_blank def test_user_port_wins_over_default user_port = 5001 @options[:Port] = user_port - conf = RACK_HANDLER_MOD::Puma.config(->{}, @options) + conf = ::Rack::Handler::Puma.config(->{}, @options) conf.load assert_equal ["tcp://0.0.0.0:#{user_port}"], conf.options[:binds] @@ -235,7 +220,7 @@ def test_user_port_wins_over_config File.open("config/puma.rb", "w") { |f| f << "port #{file_port}" } @options[:Port] = user_port - conf = RACK_HANDLER_MOD::Puma.config(->{}, @options) + conf = ::Rack::Handler::Puma.config(->{}, @options) conf.load assert_equal ["tcp://0.0.0.0:#{user_port}"], conf.options[:binds] @@ -244,7 +229,7 @@ def test_user_port_wins_over_config end def test_default_log_request_when_no_config_file - conf = RACK_HANDLER_MOD::Puma.config(->{}, @options) + conf = ::Rack::Handler::Puma.config(->{}, @options) conf.load assert_equal false, conf.options[:log_requests] @@ -257,7 +242,7 @@ def test_file_log_requests_wins_over_default_config 'test/config/t1_conf.rb' ] - conf = RACK_HANDLER_MOD::Puma.config(->{}, @options) + conf = ::Rack::Handler::Puma.config(->{}, @options) conf.load assert_equal file_log_requests_config, conf.options[:log_requests] @@ -271,10 +256,39 @@ def test_user_log_requests_wins_over_file_config 'test/config/t1_conf.rb' ] - conf = RACK_HANDLER_MOD::Puma.config(->{}, @options) + conf = ::Rack::Handler::Puma.config(->{}, @options) conf.load assert_equal user_log_requests_config, conf.options[:log_requests] end end + + # Run using IO.popen so we don't load Rack and/or Rackup in the main process + class RackUp < Minitest::Test + def setup + FileUtils.copy_file 'test/rackup/hello.ru', 'config.ru' + end + + def teardown + FileUtils.rm 'config.ru' + end + + def test_bin + # JRuby & TruffleRuby take a long time using IO.popen + skip_unless :mri + io = IO.popen "rackup -p 0" + io.wait_readable 2 + sleep 0.7 + log = io.sysread 2_048 + pid = log[/PID: (\d+)/, 1] || io.pid + assert_includes log, 'Puma version' + assert_includes log, 'Use Ctrl-C to stop' + ensure + if Puma::IS_WINDOWS + `taskkill /F /PID #{pid}` + else + `kill #{pid}` + end + end + end end diff --git a/test/test_rack_server.rb b/test/test_rack_server.rb index 278ff5c3e3..41aee707e5 100644 --- a/test/test_rack_server.rb +++ b/test/test_rack_server.rb @@ -2,8 +2,11 @@ require_relative "helper" require "net/http" -require "rack" +# don't load Rack, as it autoloads everything require "rack/body_proxy" +require "rack/lint" +require "rack/version" +require "rack/common_logger" # Rack::Chunked is loaded by Rack v2, needs to be required by Rack 3.0, # and is removed in Rack 3.1