module Pact::PactFile

Constants

OPEN_TIMEOUT
READ_TIMEOUT
RETRY_LIMIT

Public Instance Methods

read(uri, options = {}) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 22
def read uri, options = {}
  uri_string = uri.to_s
  pact = render_pact(uri_string, options)
  if options[:save_pactfile_to_tmp]
    save_pactfile_to_tmp pact, ::File.basename(uri_string)
  end
  pact
rescue StandardError => e
  $stderr.puts "Error reading file from #{uri}"
  $stderr.puts "#{e.to_s} #{e.backtrace.join("\n")}"
  raise e
end
render_pact(uri_string, options) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 42
def render_pact(uri_string, options)
  local?(uri_string) ? get_local(uri_string, options) : get_remote_with_retry(uri_string, options)
end
save_pactfile_to_tmp(pact, name) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 35
def save_pactfile_to_tmp pact, name
  ::FileUtils.mkdir_p Pact.configuration.tmp_dir
  ::File.open(Pact.configuration.tmp_dir + "/#{name}", "w") { |file|  file << pact}
rescue Errno::EROFS
  # do nothing, probably on RunKit
end

Private Instance Methods

abort_retry?(count, options) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 143
def abort_retry?(count, options)
  count >= (options[:retry_limit] || RETRY_LIMIT)
end
delay_retry(count) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 147
def delay_retry(count)
  Kernel.sleep(2 ** count * 0.3)
end
disable_ssl_verification?() click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 172
def disable_ssl_verification?
  ENV['PACT_DISABLE_SSL_VERIFICATION'] == 'true' || ENV['PACT_BROKER_DISABLE_SSL_VERIFICATION'] == 'true'
end
get_local(filepath, _) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 52
def get_local(filepath, _)
  File.read windows_safe(filepath)
end
get_remote(uri, options) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 82
def get_remote(uri, options)
  request = Net::HTTP::Get.new(uri)
  request = prepare_auth(request, options) if options[:username] || options[:token]

  http = prepare_request(uri, options)
  response = perform_http_request(http, request, options)

  if response.is_a?(Net::HTTPRedirection)
    uri = URI(response.header['location'])
    req = Net::HTTP::Get.new(uri)
    req = prepare_auth(req, options) if options[:username] || options[:token]

    http = prepare_request(uri, options)
    response = perform_http_request(http, req, options)
  end
  response
end
get_remote_with_retry(uri_string, options) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 56
def get_remote_with_retry(uri_string, options)
  uri = URI(uri_string)
  if uri.userinfo
    options[:username] = uri.user unless options[:username]
    options[:password] = uri.password unless options[:password]
  end
  ((options[:retry_limit] || RETRY_LIMIT) + 1).times do |i|
    begin
      response = get_remote(uri, options)
      case
      when success?(response)
        return response.body
      when retryable?(response)
        raise HttpError.new(uri, response) if abort_retry?(i, options)
        delay_retry(i + 1)
        next
      else
        raise HttpError.new(uri, response)
      end
    rescue Timeout::Error => e
      raise e if abort_retry?(i, options)
      delay_retry(i + 1)
    end
  end
end
local?(uri) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 48
def local? uri
  !uri.start_with?("http://", "https://")
end
perform_http_request(http, request, options) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 127
def perform_http_request(http, request, options)
  http.start do |http|
    http.open_timeout = options[:open_timeout] || OPEN_TIMEOUT
    http.read_timeout = options[:read_timeout] || READ_TIMEOUT
    http.request(request)
  end
end
prepare_auth(request, options) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 100
def prepare_auth(request, options)
  request.basic_auth(options[:username], options[:password]) if options[:username]
  request['Authorization'] = "Bearer #{options[:token]}" if options[:token]
  request
end
prepare_request(uri, options) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 106
def prepare_request(uri, options)
  http = Net::HTTP.new(uri.host, uri.port, :ENV)
  http.use_ssl = (uri.scheme == 'https')
  http.ca_file = ENV['SSL_CERT_FILE'] if ENV['SSL_CERT_FILE'] && ENV['SSL_CERT_FILE'] != ''
  http.ca_path = ENV['SSL_CERT_DIR'] if ENV['SSL_CERT_DIR'] && ENV['SSL_CERT_DIR'] != ''
  http.set_debug_output(Pact::Http::AuthorizationHeaderRedactor.new(Pact.configuration.output_stream)) if verbose?(options)

  if x509_certificate?
    http.cert = OpenSSL::X509::Certificate.new(x509_client_cert_file)
    http.key = OpenSSL::PKey::RSA.new(x509_client_key_file)
  end

  if disable_ssl_verification?
    if verbose?(options)
      Pact.configuration.output_stream.puts("SSL verification is disabled")
    end
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
  end
  http
end
retryable?(response) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 139
def retryable?(response)
  (500...600).cover?(response.code.to_i)
end
success?(response) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 135
def success?(response)
  response.code.to_i == 200
end
verbose?(options) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 155
def verbose?(options)
  options[:verbose] || ENV['VERBOSE'] == 'true'
end
windows_safe(uri) click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 151
def windows_safe(uri)
  uri.start_with?("http") ? uri : uri.gsub("\\", File::SEPARATOR)
end
x509_certificate?() click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 159
def x509_certificate?
  ENV['X509_CLIENT_CERT_FILE'] && ENV['X509_CLIENT_CERT_FILE'] != '' &&
    ENV['X509_CLIENT_KEY_FILE'] && ENV['X509_CLIENT_KEY_FILE'] != ''
end
x509_client_cert_file() click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 164
def x509_client_cert_file
  File.read(ENV['X509_CLIENT_CERT_FILE'])
end
x509_client_key_file() click to toggle source
# File lib/pact/consumer_contract/pact_file.rb, line 168
def x509_client_key_file
  File.read(ENV['X509_CLIENT_KEY_FILE'])
end