Class: Pact::MockService::Server::WaitForServerUp

Inherits:
Object
  • Object
show all
Defined in:
lib/pact/mock_service/server/wait_for_server_up.rb

Class Method Summary collapse

Class Method Details

.call(host, port, options = {ssl: false}) ⇒ Object



10
11
12
13
14
15
16
17
18
# File 'lib/pact/mock_service/server/wait_for_server_up.rb', line 10

def self.call(host, port, options = {ssl: false})
  tries = 0
  responsive = false
  while !(responsive = responsive?(host, port, options)) && tries < 100
    tries += 1
    sleep 1
  end
  raise "Timed out waiting for server to start up on port #{port}" if !responsive
end

.responsive?(host, port, options) ⇒ Boolean

Returns:

  • (Boolean)


20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/pact/mock_service/server/wait_for_server_up.rb', line 20

def self.responsive? host, port, options
  http = Net::HTTP.new(host, port)
  if options[:ssl]
    http.use_ssl = true
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    scheme = 'https'
  else
    scheme = 'http'
  end
  http.start {
    request = Net::HTTP::Get.new "#{scheme}://#{host}:#{port}/"
    request['X-Pact-Mock-Service'] = true
    response = http.request request
    response.code == '200'
  }
rescue SystemCallError => e
  return false
rescue EOFError
  return false
end