Sha256: 5d3f0668f5e7b942df4234d7af4c89388160376ea3a6829ef9436671629a2633

Contents?: true

Size: 1.63 KB

Versions: 107

Compression:

Stored size: 1.63 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
    error_set = false
    uri = Addressable::URI.heuristic_parse(uri)
    EventMachine.run {
      request = EventMachine::HttpRequest.new("#{uri.normalize.to_s}")
      http = request.send(method, {
        :timeout => 30,
        :body => options[:body],
        :file => options[:file],
        :query => options[:query],
        :head => head.merge('authorization' => [uri.user, uri.password])
      }, &block)
      http.errback {
        error_set = true
        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.to_s if error_set
    response
  end

  def client_timeout_exception_class
    "WebMock timeout error"
  end

  def connection_refused_exception_class
    RuntimeError
  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

107 entries across 105 versions & 9 rubygems

Version Path
webmock-1.22.2 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.22.1 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.21.0 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.20.4 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.20.3 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.20.2 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.20.1 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.20.0 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.19.0 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
whos_dated_who-0.1.0 vendor/bundle/gems/webmock-1.18.0/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
whos_dated_who-0.0.1 vendor/bundle/gems/webmock-1.18.0/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.18.0 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.17.4 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.17.3 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.17.2 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.17.1 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.17.0 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.16.1 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.16.0 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.15.2 spec/acceptance/em_http_request/em_http_request_spec_helper.rb