Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Rack 3 compatibility #146

Merged
merged 1 commit into from
Oct 18, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Given that we have rackup declared as a dependency now, do we need to use this approach, or could we go straight to requiring rackup?

begin
require 'rack/handler/webrick'
rescue LoadError
require 'rackup/handler/webrick'
end
Rack::Handler::WEBrick.run(app, **webrick_opts) do |server|

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if rackup/handler/webrick is required in previous rescue, then this needs to be Rackup::Handler::WEBrick otherwise it throws a NameError. If it needs to be compatible with rack then we need to handle the error doing something like below (though there might be a neater way of doing it)

    def run_default_server(app, port)
      begin
        require 'rack/handler/webrick'
      rescue LoadError
        require 'rackup/handler/webrick'
      end
      begin
        Rack::Handler::WEBrick.run(app, **webrick_opts) do |server|
          @port = server[:Port]
        end
      rescue NameError
        Rackup::Handler::WEBrick.run(app, **webrick_opts) do |server|
          @port = server[:Port]
        end
      end
    end

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hey, I assume this is causing you issues as it stands?

happy to consider an additional pull request.

it may be worth tracking your particular problem as a new issue for visibility

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ive just seen the issue and pr now, thanks!

Copy link
Contributor Author

@agfor agfor Jun 14, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looking at this now, it looks wrong / incomplete to me too, but we're running it on Rack 3 successfully and it worked when I tested it last year.

Ah, I see on the new PR, it's only Rack 3.1 where this is an issue.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yep sorry should have followed up with the issue we've created for reference #151

@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