Sha256: 0e00d28f9c416978cf7344259586ef8a7f53870183a5bfb9c42b4fc6a3ad20a2

Contents?: true

Size: 1.13 KB

Versions: 73

Compression:

Stored size: 1.13 KB

Contents

require "ostruct"

module HttpRbSpecHelper
  def http_request(method, uri, options = {})
    response = HTTP.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

73 entries across 73 versions & 5 rubygems

Version Path
cloudsmith-api-2.0.15 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-2.0.14 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-2.0.13 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-2.0.12 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-2.0.11 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-2.0.10 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-2.0.9 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-2.0.8 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-2.0.7 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-2.0.6 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-2.0.5 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-2.0.4 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-2.0.3 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-2.0.2 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-2.0.1 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-2.0.0 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-1.142.3 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-1.120.3 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-1.61.3 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb
cloudsmith-api-1.42.3 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/http_rb/http_rb_spec_helper.rb