Sha256: 1febad059b3f0be74fc521a09a7e20071d51d93af62b77c205ad9a69aecd122c

Contents?: true

Size: 1.35 KB

Versions: 67

Compression:

Stored size: 1.35 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

    ssl_ctx = OpenSSL::SSL::SSLContext.new
    ssl_ctx.verify_mode = OpenSSL::SSL::VERIFY_NONE

    response = chain.request(method, normalize_uri(uri), options.merge(ssl_context: ssl_ctx))

    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::TimeoutError
  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

67 entries across 67 versions & 5 rubygems

Version Path
fluent-plugin-google-cloud-logging-on-prem-0.1.0 vendor/ruby/3.1.0/gems/webmock-3.18.1/spec/acceptance/http_rb/http_rb_spec_helper.rb
webmock-3.18.1 spec/acceptance/http_rb/http_rb_spec_helper.rb
webmock-3.18.0 spec/acceptance/http_rb/http_rb_spec_helper.rb
webmock-3.15.2 spec/acceptance/http_rb/http_rb_spec_helper.rb
webmock-3.16.2 spec/acceptance/http_rb/http_rb_spec_helper.rb
webmock-3.16.1 spec/acceptance/http_rb/http_rb_spec_helper.rb
webmock-3.15.1 spec/acceptance/http_rb/http_rb_spec_helper.rb
webmock-3.17.1 spec/acceptance/http_rb/http_rb_spec_helper.rb
webmock-3.17.0 spec/acceptance/http_rb/http_rb_spec_helper.rb
logstash-output-scalyr-0.2.1.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/acceptance/http_rb/http_rb_spec_helper.rb
logstash-output-scalyr-0.2.0 vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/acceptance/http_rb/http_rb_spec_helper.rb
logstash-output-scalyr-0.2.0.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/acceptance/http_rb/http_rb_spec_helper.rb
logstash-output-scalyr-0.1.26.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/acceptance/http_rb/http_rb_spec_helper.rb
logstash-output-scalyr-0.1.25.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/acceptance/http_rb/http_rb_spec_helper.rb
logstash-output-scalyr-0.1.24.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/acceptance/http_rb/http_rb_spec_helper.rb
logstash-output-scalyr-0.1.23.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/acceptance/http_rb/http_rb_spec_helper.rb
logstash-output-scalyr-0.1.22.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/acceptance/http_rb/http_rb_spec_helper.rb
logstash-output-scalyr-0.1.21.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/acceptance/http_rb/http_rb_spec_helper.rb
logstash-output-scalyr-0.1.20.beta vendor/bundle/jruby/2.5.0/gems/webmock-3.13.0/spec/acceptance/http_rb/http_rb_spec_helper.rb
webmock-3.14.0 spec/acceptance/http_rb/http_rb_spec_helper.rb