Sha256: 9e21ca394802bc4a66bf71b52fffc40f32fb9dd71e57f4c753e2e7c71974db8a

Contents?: true

Size: 1.45 KB

Versions: 129

Compression:

Stored size: 1.45 KB

Contents

module NetHTTPSpecHelper
  def http_request(method, uri, options = {}, &block)
    begin
      uri = URI.parse(uri)
    rescue
      uri = Addressable::URI.heuristic_parse(uri)
    end
    response = nil
    clazz = Net::HTTP.const_get("#{method.to_s.capitalize}")
    req = clazz.new("#{uri.path}#{uri.query ? '?' : ''}#{uri.query}", nil)
    options[:headers].each do |k,v|
      if v.is_a?(Array)
        v.each_with_index do |v,i|
          i == 0 ? (req[k] = v) : req.add_field(k, v)
        end
      else
        req[k] = v
      end
    end if options[:headers]

    req.basic_auth uri.user, uri.password if uri.user
    http = Net::HTTP.new(uri.host, uri.port)
    if uri.scheme == "https"
      http.use_ssl = true
      #1.9.1 has a bug with ssl_timeout
      http.ssl_timeout = 20 unless RUBY_PLATFORM =~ /java/
      http.open_timeout = 60
      http.read_timeout = 60
    end
    http.verify_mode = OpenSSL::SSL::VERIFY_NONE
    response = http.start {|http|
      http.request(req, options[:body], &block)
    }
    headers = {}
    response.each_header {|name, value| headers[name] = value}
    OpenStruct.new({
      :body => response.body,
      :headers => WebMock::Util::Headers.normalize_headers(headers),
      :status => response.code,
      :message => response.message
    })
  end

  def client_timeout_exception_class
    Timeout::Error
  end

  def connection_refused_exception_class
    Errno::ECONNREFUSED
  end

  def http_library
    :net_http
  end
end

Version data entries

129 entries across 127 versions & 11 rubygems

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