Sha256: ada8898c88035adf25091203b0940ca9331e645e999ebada045b1082a77901a8

Contents?: true

Size: 1.23 KB

Versions: 36

Compression:

Stored size: 1.23 KB

Contents

require "ostruct"

module HttpRbSpecHelper
  def http_request(method, uri, options = {})
    chain = HTTP

    if basic_auth = options.delete(:basic_auth)
      chain = chain.basic_auth(user: basic_auth[0], pass: basic_auth[1])
    end

    response = chain.request(method, normalize_uri(uri), options)

    OpenStruct.new({
      body: response.body.to_s,
      headers: normalize_headers(response.headers.to_h),
      status: response.code.to_s,
      message: response.reason
    })
  end

  def client_timeout_exception_class
    return Errno::ETIMEDOUT if HTTP::VERSION < "1.0.0"
    HTTP::ConnectionError
  end

  def connection_refused_exception_class
    return Errno::ECONNREFUSED if HTTP::VERSION < "1.0.0"
    HTTP::ConnectionError
  end

  def http_library
    :http_rb
  end

  def normalize_uri(uri)
    Addressable::URI.heuristic_parse(uri).normalize.to_s
  end

  def normalize_headers(headers)
    headers = Hash[headers.map { |k, v| [k, Array(v).join(", ")] }]
    WebMock::Util::Headers.normalize_headers headers
  end

  def stub_simple_request(host, status = 200, headers = {}, body = nil)
    stub_request(:any, host).to_return({
      status: status,
      headers: headers.merge({ "Host" => host }),
      body: body
    })
  end
end

Version data entries

36 entries across 30 versions & 4 rubygems

Version Path
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/3.0.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.10.0 vendor/bundle/ruby/2.7.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.9.0 vendor/bundle/ruby/2.7.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.8.0 vendor/bundle/ruby/2.7.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.6.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.7.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.6.2 vendor/bundle/ruby/2.6.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.6.1 vendor/bundle/ruby/2.6.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.6.0 vendor/bundle/ruby/2.6.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.6.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.6.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.3.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.2.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.2.0.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.1.4.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb
vagrant-unbundled-2.1.2.0 vendor/bundle/ruby/2.3.0/gems/webmock-2.3.2/spec/acceptance/http_rb/http_rb_spec_helper.rb