diff --git a/.github/workflows/test.yml b/.github/workflows/test.yml index f885e8e..5497721 100644 --- a/.github/workflows/test.yml +++ b/.github/workflows/test.yml @@ -9,7 +9,10 @@ jobs: matrix: ruby_version: ["2.7", "3.0", "3.1", "3.2", "3.3"] os: ["ubuntu-latest","windows-latest","macos-latest"] + rack_version: ["2", "3"] runs-on: ${{ matrix.os }} + env: + RACK_VERSION: ${{ matrix.rack_version }} steps: - uses: actions/checkout@v4 - uses: ruby/setup-ruby@v1 diff --git a/lib/pact/consumer/mock_service/rack_request_helper.rb b/lib/pact/consumer/mock_service/rack_request_helper.rb index 130fc15..f9184d1 100644 --- a/lib/pact/consumer/mock_service/rack_request_helper.rb +++ b/lib/pact/consumer/mock_service/rack_request_helper.rb @@ -24,7 +24,7 @@ def request_as_hash_from env end request[:headers] = headers_from env - body_string = request[:body].read + body_string = request[:body]&.read || "" if body_string.empty? request.delete :body diff --git a/lib/pact/consumer/server.rb b/lib/pact/consumer/server.rb index 767d837..7878e2d 100644 --- a/lib/pact/consumer/server.rb +++ b/lib/pact/consumer/server.rb @@ -1,6 +1,7 @@ require 'uri' require 'net/http' require 'rack' +require 'rack/handler/webbrick' # Copied shamelessly from Capybara # Used to run a mock service in a new thread when started by the AppManager or the ControlServer @@ -66,11 +67,6 @@ def responsive? end def run_default_server(app, port) - begin - require 'rack/handler/webrick' - rescue LoadError - require 'rackup/handler/webrick' - end Rack::Handler::WEBrick.run(app, **webrick_opts) do |server| @port = server[:Port] end diff --git a/lib/pact/mock_service/control_server/run.rb b/lib/pact/mock_service/control_server/run.rb index 0524e2b..8b654c7 100644 --- a/lib/pact/mock_service/control_server/run.rb +++ b/lib/pact/mock_service/control_server/run.rb @@ -1,5 +1,6 @@ require 'pact/mock_service/control_server/app' require 'pact/mock_service/server/webrick_request_monkeypatch' +require 'rack/handler/webbrick' module Pact module MockService diff --git a/lib/pact/mock_service/run.rb b/lib/pact/mock_service/run.rb index 85f3166..d0677f4 100644 --- a/lib/pact/mock_service/run.rb +++ b/lib/pact/mock_service/run.rb @@ -5,6 +5,7 @@ require 'pact/mock_service/server/webrick_request_monkeypatch' require 'pact/specification_version' require 'pact/support/metrics' +require 'rack/handler/webbrick' module Pact module MockService diff --git a/lib/rack/handler/webbrick.rb b/lib/rack/handler/webbrick.rb new file mode 100644 index 0000000..6b5acc0 --- /dev/null +++ b/lib/rack/handler/webbrick.rb @@ -0,0 +1,12 @@ +module Rack + module Handler + begin + require 'rack/handler/webrick' + WEBrick = Class.new(Rack::Handler::WEBrick) + rescue LoadError + require 'rackup/handler/webrick' + WEBrick = Class.new(Rackup::Handler::WEBrick) + end + end +end + diff --git a/pact-mock_service.gemspec b/pact-mock_service.gemspec index d5aebf5..5e46486 100644 --- a/pact-mock_service.gemspec +++ b/pact-mock_service.gemspec @@ -20,8 +20,12 @@ Gem::Specification.new do |gem| gem.require_paths = ["lib"] gem.license = 'MIT' - gem.add_runtime_dependency 'rack', '>= 2.0', '< 4.0' - gem.add_runtime_dependency 'rackup', '~> 2.0' + if ENV['RACK_VERSION'] == '2' + gem.add_runtime_dependency 'rack', '>= 2.0', '< 3.0' + else + gem.add_runtime_dependency 'rack', '>= 3.0', '< 4.0' + gem.add_runtime_dependency 'rackup', '~> 2.0' + end gem.add_runtime_dependency 'rspec', '>=2.14' gem.add_runtime_dependency 'find_a_port', '~> 1.0.1' gem.add_runtime_dependency 'thor', '>= 0.19', '< 2.0'