Skip to content

Commit

Permalink
feat: add Rack 3 compatibility (#146)
Browse files Browse the repository at this point in the history
  • Loading branch information
agfor authored Oct 18, 2023
1 parent 43dec1d commit 9afea51
Show file tree
Hide file tree
Showing 6 changed files with 23 additions and 20 deletions.
6 changes: 5 additions & 1 deletion lib/pact/consumer/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,11 @@ def responsive?
end

def run_default_server(app, port)
require 'rack/handler/webrick'
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
Expand Down
6 changes: 5 additions & 1 deletion lib/pact/mock_service/cli.rb
Original file line number Diff line number Diff line change
Expand Up @@ -171,7 +171,11 @@ def version

def require_common_dependencies
require 'webrick/https'
require 'rack/handler/webrick'
begin
require 'rack/handler/webrick'
rescue LoadError
require 'rackup/handler/webrick'
end
require 'fileutils'
require 'pact/mock_service/server/wait_for_server_up'
require 'pact/mock_service/cli/pidfile'
Expand Down
2 changes: 1 addition & 1 deletion lib/pact/mock_service/request_handlers/interaction_post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def request_method
end

def respond env
request_body = env['rack.input'].string
request_body = env['rack.input'].read
parsing_options = { pact_specification_version: pact_specification_version }
interaction = Interaction.from_hash(JSON.load(request_body), parsing_options) # Load creates the Pact::XXX classes

Expand Down
6 changes: 5 additions & 1 deletion lib/pact/stub_service/cli.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
require 'pact/mock_service/cli/custom_thor'
require 'webrick/https'
require 'rack/handler/webrick'
begin
require 'rack/handler/webrick'
rescue LoadError
require 'rackup/handler/webrick'
end
require 'fileutils'
require 'pact/mock_service/server/wait_for_server_up'
require 'pact/mock_service/cli/pidfile'
Expand Down
5 changes: 3 additions & 2 deletions pact-mock_service.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,16 @@ Gem::Specification.new do |gem|
gem.require_paths = ["lib"]
gem.license = 'MIT'

gem.add_runtime_dependency 'rack', '~> 2.0'
gem.add_runtime_dependency 'rack', '>= 2.0', '< 4.0'
gem.add_runtime_dependency 'rackup', '~> 2.0'
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'
gem.add_runtime_dependency 'json'
gem.add_runtime_dependency 'webrick', '~> 1.8'
gem.add_runtime_dependency 'pact-support', '~> 1.16', '>= 1.16.4'

gem.add_development_dependency 'rack-test', '~> 0.7'
gem.add_development_dependency 'rack-test', '>= 0.7', '< 3.0'
gem.add_development_dependency 'rake', '~> 13.0', '>= 13.0.1'
gem.add_development_dependency 'webmock', '~> 3.4'
gem.add_development_dependency 'pry'
Expand Down
18 changes: 4 additions & 14 deletions spec/integration/cli_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
it "starts up and responds with mocked responses" do
response = setup_interaction 8888
expect(response.status).to eq 200
expect(response.headers['X-Pact-Mock-Service-Location']).to eq 'http://0.0.0.0:8888'

response = invoke_expected_request 8888
puts response.body if response.status != 200
Expand All @@ -21,37 +22,26 @@

write_pact 8888
expect(response.status).to eq 200
end

it "respects headers with underscores" do
setup_interaction_with_underscored_header 8888
response = invoke_request_with_underscored_header 8888
puts response.body unless response.status == 200
expect(response.status).to eq 200
end

it "sets the X-Pact-Mock-Service-Location header" do
response = setup_interaction 8888
expect(response.headers['X-Pact-Mock-Service-Location']).to eq 'http://0.0.0.0:8888'
Process.kill "INT", @pid
sleep 1 # Naughty, but so much less code
@pid = nil
end

it "writes logs to the specified log file" do
expect(File.exist?('tmp/integration.log')).to be true
end

it "writes the pact to the specified directory" do
clear_interactions 8888
setup_interaction 8888
invoke_expected_request 8888
expect(File.exist?('tmp/pacts/consumer-provider.json')).to be true
end

it "sets the pact specification version" do
clear_interactions 8888
setup_interaction 8888
invoke_expected_request 8888

write_pact 8888
expect(File.read("tmp/pacts/consumer-provider.json")).to include "3.0.0"
end

Expand Down

0 comments on commit 9afea51

Please sign in to comment.