Sha256: 212aef71ba5cdacee37274ca4056148385a5ab9485c59018ddd76e46dff7e4dc

Contents?: true

Size: 1.43 KB

Versions: 69

Compression:

Stored size: 1.43 KB

Contents

require 'ostruct'

module TyphoeusHydraSpecHelper
  class FakeTyphoeusHydraTimeoutError < StandardError; end
  class FakeTyphoeusHydraConnectError < StandardError; end


  def http_request(method, uri, options = {}, &block)
    uri = uri.gsub(" ", "%20") #typhoeus doesn't like spaces in the uri
    request_options =  {
      method: method,
      body: options[:body],
      headers: options[:headers],
      timeout: 25000
    }
    if options[:basic_auth]
      request_options[:userpwd] = options[:basic_auth].join(':')
    end
    request = Typhoeus::Request.new(uri, request_options)

    hydra = Typhoeus::Hydra.new
    hydra.queue(request)
    hydra.run

    response = request.response
    raise FakeTyphoeusHydraConnectError.new if response.return_code == :couldnt_connect
    raise FakeTyphoeusHydraTimeoutError.new if response.timed_out?
    OpenStruct.new({
      body: response.body,
      headers: WebMock::Util::Headers.normalize_headers(join_array_values(response.headers)),
      status: response.code.to_s,
      message: response.status_message
    })
  end

  def join_array_values(hash)
    joined = {}
    if hash
     hash.each do |k,v|
       v = v.join(", ") if v.is_a?(Array)
       joined[k] = v
     end
    end
    joined
  end


  def client_timeout_exception_class
    FakeTyphoeusHydraTimeoutError
  end

  def connection_refused_exception_class
    FakeTyphoeusHydraConnectError
  end

  def http_library
    :typhoeus
  end

end

Version data entries

69 entries across 69 versions & 5 rubygems

Version Path
webmock-3.5.0 spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb
webmock-3.4.2 spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb
webmock-3.4.1 spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb
webmock-3.4.0 spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb
webmock-3.3.0 spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb
webmock-3.2.1 spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb
webmock-3.2.0 spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb
webmock-3.1.1 spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb
webmock-3.1.0 spec/acceptance/typhoeus/typhoeus_hydra_spec_helper.rb