pact-foundation / pact-mock_service

Provides a mock service for use with Pact
https://pact.io
MIT License
72 stars 68 forks source link

Allow Host to be configured #128

Closed mhall58 closed 3 years ago

mhall58 commented 3 years ago

In our testing, the host is not being passed when creating a new mock service. We were only ever able to create a server on localhost. This might fix the issue.

mhall58 commented 3 years ago

WEBrick run expects :Host



def self.run(app, **options)
  environment  = ENV['RACK_ENV'] || 'development'
  default_host = environment == 'development' ? 'localhost' : nil

  if !options[:BindAddress] || options[:Host]
    options[:BindAddress] = options.delete(:Host) || default_host
  end
  options[:Port] ||= 8080
  if options[:SSLEnable]
    require 'webrick/https'
  end

  @server = ::WEBrick::HTTPServer.new(options)
  @server.mount "/", Rack::Handler::WEBrick, app
  yield @server  if block_given?
  @server.start
end```
bethesque commented 3 years ago

Thanks for the contribution. That code is only for the mock service that gets started and managed by the Ruby Pact consumer library, so I suspect it's not the bit you're interested in. The code that gets executed when the mock service is started from the command line is here https://github.com/pact-foundation/pact-mock_service/blob/master/lib/pact/mock_service/run.rb#L88

mhall58 commented 3 years ago

I am trying to use the pact ruby consumer library with 2 docker containers, I do believe this is the code I am using. I'm not using the command line utility.

On Wed, May 12, 2021, 8:07 PM Beth Skurrie @.***> wrote:

Thanks for the contribution. That code is only for the mock service that gets started and managed by the Ruby Pact consumer library, so I suspect it's not the bit you're interested in. The code that gets executed when the mock service is started from the command line is here https://github.com/pact-foundation/pact-mock_service/blob/master/lib/pact/mock_service/run.rb#L88

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/pact-foundation/pact-mock_service/pull/128#issuecomment-840180956, or unsubscribe https://github.com/notifications/unsubscribe-auth/ABGCVM4U7CETSFNKZKCK5K3TNMJ3RANCNFSM44YZQSTQ .

bethesque commented 3 years ago

Can you explain how your tests are running over the two containers?

mhall58 commented 3 years ago

Yes. We have a php container with a cli command that calls an http endpoint. We have another container named qa which has our ruby tests.

On that container we have this pact helper file

require 'pact/consumer/rspec'

Pact.service_consumer "partner websites cron" do
  has_pact_with "similar web" do
    mock_service :similar_web do
      port 5678
      host "0.0.0.0" # Optional, defaults to "localhost". Useful when using a standalone mock service.
      standalone false # Optional. If true, does not automatically start a mock service
      # You must start/stop the mock service manually.
    end
  end
end

We also have an rspec file where we use the mock service and configure the test :

       similar_web.given("a website exists").
          upon_receiving("a request for #{url}").
          with(method: :get, path: url, query: 'api_key=').
          will_respond_with(
            status: 200,
            headers: { 'Content-Type' => 'application/json' },
            body: body)

the test goes on to trigger the php cli command and expect that it does various things in the database and behaves correctly.

The php cli command is configured to point to our mock service on the qa container at the specified port.

I have tested the above change by forking this repository and overriding it in my gem file. It works with this change and does not without it.

mhall58 commented 3 years ago

my gem file

  gem 'pact'
  gem "pact-mock_service", :git => "https://github.com/mhall58/pact-mock_service.git"
bethesque commented 3 years ago

I'll merge it, but I feel like there must be a better way to do what you're doing! What is the point of the ruby tests? Are they testing anything, or are they just setting up the mock service for the CLI?

mhall58 commented 3 years ago

Thank you. The tests are checking that the cli command handles the data supplied by the mock service correctly. It verifies records in the database are the values that were passed to it from the mock service. We can now use this to cover other areas where we have dependent services and use the pact files to ensure they are meeting their contractual obligations. My superiors will be very pleased 😄