Sha256: c1e2f05d85477fe47c9c3f20d3d9f08a3a9730c508bbfba8118f2aec0af4f1d7

Contents?: true

Size: 1.53 KB

Versions: 10

Compression:

Stored size: 1.53 KB

Contents

module EMHttpRequestSpecHelper

  def failed
    EventMachine.stop
    fail
  end

  def http_request(method, uri, options = {}, &block)
    @http = nil
    head = options[:headers] || {}
    response = nil
    error = nil
    uri = Addressable::URI.heuristic_parse(uri)
    EventMachine.run {
      request = EventMachine::HttpRequest.new("#{uri.normalize.to_s}")
      http = request.send(method, {
        :timeout => 10,
        :body => options[:body],
        :query => options[:query],
        :head => head.merge('authorization' => [uri.user, uri.password])
      }, &block)
      http.errback {
        error = if http.respond_to?(:errors)
          http.errors
        else
          http.error
        end
        failed
      }
      http.callback {
        response = OpenStruct.new({
          :body => http.response,
          :headers => WebMock::Util::Headers.normalize_headers(extract_response_headers(http)),
          :message => http.response_header.http_reason,
          :status => http.response_header.status.to_s
        })
        EventMachine.stop
      }
      @http = http
    }
    raise error if error
    response
  end

  def client_timeout_exception_class
    "WebMock timeout error"
  end

  def connection_refused_exception_class
    ""
  end

  def http_library
    :em_http_request
  end

  private

  def extract_response_headers(http)
    headers = {}
    if http.response_header
      http.response_header.each do |k,v|
        v = v.join(", ") if v.is_a?(Array)
        headers[k] = v
      end
    end
    headers
  end

end

Version data entries

10 entries across 10 versions & 1 rubygems

Version Path
webmock-1.7.10 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.7.8 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.7.7 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.7.6 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.7.5 spec/em_http_request_spec_helper.rb
webmock-1.7.4 spec/em_http_request_spec_helper.rb
webmock-1.7.3 spec/em_http_request_spec_helper.rb
webmock-1.7.2 spec/em_http_request_spec_helper.rb
webmock-1.7.1 spec/em_http_request_spec_helper.rb
webmock-1.7.0 spec/em_http_request_spec_helper.rb