Skip to content

Commit

Permalink
feat: allow host of mock service to be specified
Browse files Browse the repository at this point in the history
  • Loading branch information
bethesque committed Feb 21, 2019
1 parent 7f0357d commit de267bd
Show file tree
Hide file tree
Showing 6 changed files with 16 additions and 7 deletions.
10 changes: 8 additions & 2 deletions lib/pact/consumer/configuration/mock_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,13 +11,14 @@ class MockService

extend Pact::DSL

attr_accessor :port, :standalone, :verify, :provider_name, :consumer_name, :pact_specification_version
attr_accessor :port, :host, :standalone, :verify, :provider_name, :consumer_name, :pact_specification_version

def initialize name, consumer_name, provider_name
@name = name
@consumer_name = consumer_name
@provider_name = provider_name
@port = nil
@host = "localhost"
@standalone = false
@verify = true
@pact_specification_version = '2'
Expand All @@ -29,6 +30,10 @@ def port port
self.port = port
end

def host host
self.host = host
end

def standalone standalone
self.standalone = standalone
end
Expand All @@ -53,7 +58,7 @@ def finalize

def register_mock_service
unless standalone
url = "http://localhost#{port.nil? ? '' : ":#{port}"}"
url = "http://#{host}#{port.nil? ? '' : ":#{port}"}"
ret = Pact::MockService::AppManager.instance.register_mock_service_for(provider_name, url, mock_service_options)
raise "pact-mock_service(v#{Pact::MockService::VERSION}) does not support 'find available port' feature" unless ret
@port = ret
Expand All @@ -73,6 +78,7 @@ def create_consumer_contract_builder
:provider_name => provider_name,
:pactfile_write_mode => Pact.configuration.pactfile_write_mode,
:port => port,
:host => host,
:pact_dir => Pact.configuration.pact_dir
}
Pact::Consumer::ConsumerContractBuilder.new consumer_contract_builder_fields
Expand Down
4 changes: 2 additions & 2 deletions lib/pact/consumer/consumer_contract_builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,8 @@ def initialize(attributes)
pactfile_write_mode: attributes[:pactfile_write_mode].to_s,
pact_dir: attributes.fetch(:pact_dir)
}
@mock_service_client = Pact::MockService::Client.new(attributes[:port])
@mock_service_base_url = "http://localhost:#{attributes[:port]}"
@mock_service_client = Pact::MockService::Client.new(attributes[:port], attributes[:host])
@mock_service_base_url = "http://#{attributes[:host]}:#{attributes[:port]}"
end

def given(provider_state)
Expand Down
4 changes: 2 additions & 2 deletions lib/pact/consumer/spec_hooks.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ def before_all
def before_each example_description
Pact.consumer_world.register_pact_example_ran
Pact.configuration.logger.info "Clearing all expectations"
Pact::MockService::AppManager.instance.ports_of_mock_services.each do | port |
Pact::MockService::Client.clear_interactions port, example_description
Pact::MockService::AppManager.instance.urls_of_mock_services.each do | url |
Pact::MockService::Client.clear_interactions url, example_description
end
end

Expand Down
2 changes: 1 addition & 1 deletion pact.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Gem::Specification.new do |gem|
gem.add_runtime_dependency 'term-ansicolor', '~> 1.0'

gem.add_runtime_dependency 'pact-support', '~> 1.8'
gem.add_runtime_dependency 'pact-mock_service', '~> 2.10'
gem.add_runtime_dependency 'pact-mock_service', '~> 3.0'

gem.add_development_dependency 'rake', '~> 10.0.3'
gem.add_development_dependency 'webmock', '~> 3.0'
Expand Down
1 change: 1 addition & 0 deletions spec/features/foo_bar_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
has_pact_with "Bar" do
mock_service :bar_service do
pact_specification_version "2"
host "127.0.0.1"
port 4638
end
end
Expand Down
2 changes: 2 additions & 0 deletions spec/lib/pact/consumer/consumer_contract_builder_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ module Consumer
provider_name: provider_name,
pactfile_write_mode: :overwrite,
port: 2222,
host: 'localhost',
pact_dir: pact_dir)
end

Expand Down Expand Up @@ -70,6 +71,7 @@ module Consumer
pact_dir: './spec/pacts',
consumer_name: consumer_name,
provider_name: provider_name,
host: 'localhost',
port: 1234
)
end
Expand Down

0 comments on commit de267bd

Please sign in to comment.