Skip to content

Commit

Permalink
Allow ::Rack::Handler::Puma.run to work regardless of whether Rack/Ra…
Browse files Browse the repository at this point in the history
…ckup are loaded (#3080)

* rack/handler/puma.rb - fix for use without Rack

* test/test_rack_handler.rb - add test for rackup bin file

* test_rack_server.rb - only load needed Rack files

* test_rack_handler.rb - remove Windows skip for test_bin
  • Loading branch information
MSP-Greg authored and nateberkopec committed Feb 28, 2023
1 parent e3d5794 commit cf77dcc
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 37 deletions.
7 changes: 3 additions & 4 deletions lib/rack/handler/puma.rb
Expand Up @@ -120,17 +120,16 @@ 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
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
78 changes: 46 additions & 32 deletions 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"]]}
Expand All @@ -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
Expand Down Expand Up @@ -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]
Expand All @@ -100,15 +85,15 @@ 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]
end

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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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]
Expand All @@ -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
5 changes: 4 additions & 1 deletion test/test_rack_server.rb
Expand Up @@ -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
Expand Down

0 comments on commit cf77dcc

Please sign in to comment.