Sha256: d115b8fc2a8bc8d3c0faf5449fdebe4df94116f8efa15e541cdb6067c2001eed

Contents?: true

Size: 1.6 KB

Versions: 17

Compression:

Stored size: 1.6 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],
        :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

17 entries across 17 versions & 3 rubygems

Version Path
solidus_backend-1.0.0.pre3 vendor/bundle/gems/webmock-1.8.11/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
solidus_backend-1.0.0.pre2 vendor/bundle/gems/webmock-1.8.11/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
solidus_backend-1.0.0.pre vendor/bundle/gems/webmock-1.8.11/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.11.0 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.10.2 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.10.1 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.10.0 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.9.3 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.9.2 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.9.1 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
simple-client-0.0.3 vendor/bundle/ruby/1.9.1/gems/webmock-1.8.8/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.9.0 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.8.11 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.8.10 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.8.9 spec/acceptance/em_http_request/em_http_request_spec_helper.rb
simple-client-0.0.2 vendor/bundle/ruby/1.8/gems/webmock-1.8.8/spec/acceptance/em_http_request/em_http_request_spec_helper.rb
webmock-1.8.8 spec/acceptance/em_http_request/em_http_request_spec_helper.rb