Sha256: 3fdb639052faf602f85314ceb9dfb09e4ecde4c4749089eb0e10577455ede0ea

Contents?: true

Size: 1.13 KB

Versions: 5

Compression:

Stored size: 1.13 KB

Contents

require 'ostruct'

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

    sess.connect_timeout = 10
    sess.timeout = 10
    sess.max_redirects = 0
    uri = "#{uri.path}#{uri.query ? '?' : ''}#{uri.query}"
    uri.gsub!(' ','+')
    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
    OpenStruct.new({
      :body => response.body,
      :headers => WebMock::Util::Headers.normalize_headers(headers),
      :status => response.status.to_s,
      :message => response.status_line
    })
  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

5 entries across 5 versions & 1 rubygems

Version Path
webmock-1.7.4 spec/patron_spec_helper.rb
webmock-1.7.3 spec/patron_spec_helper.rb
webmock-1.7.2 spec/patron_spec_helper.rb
webmock-1.7.1 spec/patron_spec_helper.rb
webmock-1.7.0 spec/patron_spec_helper.rb