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
cloudsmith-api-0.49.94 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
cloudsmith-api-0.49.21 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
cloudsmith-api-0.49.15 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
cloudsmith-api-0.49.13 vendor/bundle/ruby/2.6.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
cloudsmith-api-0.49.9 vendor/bundle/ruby/2.3.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
vagrant-unbundled-2.2.7.0 vendor/bundle/ruby/2.4.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
cloudsmith-api-0.44.4 vendor/bundle/ruby/2.3.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
dadapush_client-1.0.1 vendor/bundle/ruby/2.3.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
cloudsmith-api-0.30.7 vendor/bundle/ruby/2.3.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
color_me_shop-1.0.0 vendor/bundle/ruby/2.5.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
vagrant-unbundled-2.0.2.0 vendor/bundle/ruby/2.4.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
vagrant-unbundled-2.0.1.0 vendor/bundle/ruby/2.4.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
cloudsmith-api-0.21.4 vendor/bundle/ruby/2.3.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
vagrant-unbundled-2.0.0.1 vendor/bundle/ruby/2.4.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
vagrant-unbundled-1.9.8.1 vendor/bundle/ruby/2.4.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
vagrant-unbundled-1.9.7.1 vendor/bundle/ruby/2.4.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
vagrant-unbundled-1.9.5.1 vendor/bundle/ruby/2.4.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
logstash-filter-zabbix-0.1.2 vendor/bundle/jruby/1.9/gems/webmock-1.21.0/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
logstash-filter-zabbix-0.1.1 vendor/bundle/jruby/1.9/gems/webmock-1.21.0/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
vagrant-unbundled-1.9.1.1 vendor/bundle/ruby/2.4.0/gems/webmock-1.24.6/spec/acceptance/em_http_request/em_http_request_spec_helper.rb