From de267bdb5f9d0e888562532f2eacdbcf2d20d6bb Mon Sep 17 00:00:00 2001 From: Beth Skurrie Date: Thu, 21 Feb 2019 19:55:02 +1100 Subject: [PATCH] feat: allow host of mock service to be specified closes: https://github.com/pact-foundation/pact-ruby/issues/186 --- lib/pact/consumer/configuration/mock_service.rb | 10 ++++++++-- lib/pact/consumer/consumer_contract_builder.rb | 4 ++-- lib/pact/consumer/spec_hooks.rb | 4 ++-- pact.gemspec | 2 +- spec/features/foo_bar_spec.rb | 1 + .../pact/consumer/consumer_contract_builder_spec.rb | 2 ++ 6 files changed, 16 insertions(+), 7 deletions(-) diff --git a/lib/pact/consumer/configuration/mock_service.rb b/lib/pact/consumer/configuration/mock_service.rb index bbeb81ab..13b368c5 100644 --- a/lib/pact/consumer/configuration/mock_service.rb +++ b/lib/pact/consumer/configuration/mock_service.rb @@ -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' @@ -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 @@ -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 @@ -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 diff --git a/lib/pact/consumer/consumer_contract_builder.rb b/lib/pact/consumer/consumer_contract_builder.rb index c186b7ce..45faa9bc 100644 --- a/lib/pact/consumer/consumer_contract_builder.rb +++ b/lib/pact/consumer/consumer_contract_builder.rb @@ -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) diff --git a/lib/pact/consumer/spec_hooks.rb b/lib/pact/consumer/spec_hooks.rb index cf7c637b..a64151ba 100644 --- a/lib/pact/consumer/spec_hooks.rb +++ b/lib/pact/consumer/spec_hooks.rb @@ -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 diff --git a/pact.gemspec b/pact.gemspec index a660d7d0..215886e3 100644 --- a/pact.gemspec +++ b/pact.gemspec @@ -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' diff --git a/spec/features/foo_bar_spec.rb b/spec/features/foo_bar_spec.rb index 0ffd6882..ce424d45 100644 --- a/spec/features/foo_bar_spec.rb +++ b/spec/features/foo_bar_spec.rb @@ -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 diff --git a/spec/lib/pact/consumer/consumer_contract_builder_spec.rb b/spec/lib/pact/consumer/consumer_contract_builder_spec.rb index 62e867b8..ebb5789a 100644 --- a/spec/lib/pact/consumer/consumer_contract_builder_spec.rb +++ b/spec/lib/pact/consumer/consumer_contract_builder_spec.rb @@ -16,6 +16,7 @@ module Consumer provider_name: provider_name, pactfile_write_mode: :overwrite, port: 2222, + host: 'localhost', pact_dir: pact_dir) end @@ -70,6 +71,7 @@ module Consumer pact_dir: './spec/pacts', consumer_name: consumer_name, provider_name: provider_name, + host: 'localhost', port: 1234 ) end