Sha256: 4ea19e2553e728b71077c3eb979145f9b309a3251bca6789f2d4d08356e1d867

Contents?: true

Size: 1.33 KB

Versions: 36

Compression:

Stored size: 1.33 KB

Contents

require 'ostruct'

module PatronSpecHelper
  def http_request(method, uri, options = {}, &block)
    method = method.to_sym
    uri = Addressable::URI.heuristic_parse(uri)
    sess = Patron::Session.new
    sess.base_url = "#{uri.omit(:path, :query).normalize.to_s}".gsub(/\/$/,"")

    if options[:basic_auth]
      sess.username = options[:basic_auth][0]
      sess.password = options[:basic_auth][1]
    end

    sess.connect_timeout = 30
    sess.timeout = 30
    sess.max_redirects = 0
    uri = "#{uri.path}#{uri.query ? '?' : ''}#{uri.query}"
    uri.gsub!(' ','%20')
    response = sess.request(method, uri, options[:headers] || {}, {
      data: options[:body]
    })
    headers = {}
    if response.headers
      response.headers.each do |k,v|
        v = v.join(", ") if v.is_a?(Array)
        headers[k] = v
      end
    end

    status_line_pattern = %r(\AHTTP/(\d+\.\d+)\s+(\d\d\d)\s*([^\r\n]+)?)
    message = response.status_line.match(status_line_pattern)[3] || ""

    OpenStruct.new({
      body: response.body,
      headers: WebMock::Util::Headers.normalize_headers(headers),
      status: response.status.to_s,
      message: message
    })
  end

  def client_timeout_exception_class
    Patron::TimeoutError
  end

  def connection_refused_exception_class
    Patron::ConnectionFailed
  end

  def http_library
    :patron
  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/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.16.0 vendor/bundle/ruby/2.7.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.14.0 vendor/bundle/ruby/2.7.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.10.0 vendor/bundle/ruby/2.7.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.9.0 vendor/bundle/ruby/2.7.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.8.0 vendor/bundle/ruby/2.7.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.6.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.7.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.6.2 vendor/bundle/ruby/2.6.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.6.1 vendor/bundle/ruby/2.6.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.6.0 vendor/bundle/ruby/2.6.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.6.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.5.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.6.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.4.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.3.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.2.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.2.0.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.1.4.0 vendor/bundle/ruby/2.5.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb
vagrant-unbundled-2.1.2.0 vendor/bundle/ruby/2.3.0/gems/webmock-2.3.2/spec/acceptance/patron/patron_spec_helper.rb