Sha256: e0326d480f9a7cd508e2620379bd3c79f201c61001f0ee27adf657d68791eaa5

Contents?: true

Size: 1.26 KB

Versions: 22

Compression:

Stored size: 1.26 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 = 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

22 entries across 22 versions & 2 rubygems

Version Path
whos_dated_who-0.1.0 vendor/bundle/gems/webmock-1.18.0/spec/acceptance/patron/patron_spec_helper.rb
whos_dated_who-0.0.1 vendor/bundle/gems/webmock-1.18.0/spec/acceptance/patron/patron_spec_helper.rb
webmock-1.18.0 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.17.4 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.17.3 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.17.2 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.17.1 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.17.0 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.16.1 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.16.0 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.15.2 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.15.0 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.14.0 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.13.0 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.12.3 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.12.2 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.12.1 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.12.0 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.11.0 spec/acceptance/patron/patron_spec_helper.rb
webmock-1.10.2 spec/acceptance/patron/patron_spec_helper.rb