class EventMachine::WebMockHttpClient

Public Instance Methods

request_signature() click to toggle source
# File lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb, line 124
def request_signature
  @request_signature ||= build_request_signature
end
send_request(head, body) click to toggle source
Calls superclass method
# File lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb, line 93
def send_request(head, body)
  WebMock::RequestRegistry.instance.requested_signatures.put(request_signature)

  if stubbed_webmock_response
    WebMock::CallbackRegistry.invoke_callbacks({:lib => :em_http_request}, request_signature, stubbed_webmock_response)
    EM.next_tick {
      setup(make_raw_response(stubbed_webmock_response), @uri,
            stubbed_webmock_response.should_timeout ? "WebMock timeout error" : nil)
    }
    self
  elsif WebMock.net_connect_allowed?(request_signature.uri)
    super
  else
    raise WebMock::NetConnectNotAllowedError.new(request_signature)
  end
end
setup(response, uri, error = nil) click to toggle source
# File lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb, line 82
def setup(response, uri, error = nil)
  @last_effective_url = @uri = uri
  if error
    on_error(error)
    fail(self)
  else
    @conn.receive_data(response)
    succeed(self)
  end
end
stubbed_webmock_response() click to toggle source
# File lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb, line 128
def stubbed_webmock_response
  unless defined?(@stubbed_webmock_response)
    @stubbed_webmock_response = WebMock::StubRegistry.instance.response_for_request(request_signature)
  end

  @stubbed_webmock_response
end
unbind(reason = nil) click to toggle source
Calls superclass method
# File lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb, line 110
def unbind(reason = nil)
  if !stubbed_webmock_response && WebMock::CallbackRegistry.any_callbacks?
    webmock_response = build_webmock_response
    WebMock::CallbackRegistry.invoke_callbacks(
      {:lib => :em_http_request, :real_request => true},
      request_signature,
      webmock_response)
  end
  @request_signature = nil
  remove_instance_variable(:@stubbed_webmock_response)

  super
end
uri() click to toggle source
# File lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb, line 78
def uri
  @req.uri
end

Private Instance Methods

build_request_signature() click to toggle source
# File lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb, line 156
def build_request_signature
  headers, body = @req.headers, @req.body

  @conn.middleware.select {|m| m.respond_to?(:request) }.each do |m|
    headers, body = m.request(self, headers, body)
  end

  method = @req.method
  uri = @req.uri.clone
  auth = @req.headers[:'proxy-authorization']
  query = @req.query

  if auth
    userinfo = auth.join(':')
    userinfo = WebMock::Util::URI.encode_unsafe_chars_in_userinfo(userinfo)
    if @req
      @req.proxy.reject! {|k,v| t.to_s == 'authorization' }
    else
      options.reject! {|k,v| k.to_s == 'authorization' } #we added it to url userinfo
    end
    uri.userinfo = userinfo
  end

  uri.query = encode_query(@req.uri, query).slice(/\?(.*)/, 1)

  body = form_encode_body(body) if body.is_a?(Hash)

  WebMock::RequestSignature.new(
    method.downcase.to_sym,
    uri.to_s,
    :body => body || (@req.file && File.read(@req.file)),
    :headers => headers
  )
end
build_webmock_response() click to toggle source
# File lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb, line 148
def build_webmock_response
  webmock_response = WebMock::Response.new
  webmock_response.status = [response_header.status, response_header.http_reason]
  webmock_response.headers = response_header
  webmock_response.body = response
  webmock_response
end
make_raw_response(response) click to toggle source
# File lib/webmock/http_lib_adapters/em_http_request/em_http_request_1_x.rb, line 191
def make_raw_response(response)
  response.raise_error_if_any

  status, headers, body = response.status, response.headers, response.body
  headers ||= {}

  response_string = []
  response_string << "HTTP/1.1 #{status[0]} #{status[1]}"

  headers["Content-Length"] = body.bytesize unless headers["Content-Length"]
  headers.each do |header, value|
    if header =~ /set-cookie/i
      [value].flatten.each do |cookie|
        response_string << "#{header}: #{cookie}"
      end
    else
      value = value.join(", ") if value.is_a?(Array)

      # WebMock's internal processing will not handle the body
      # correctly if the header indicates that it is chunked, unless
      # we also create all the chunks.
      # It's far easier just to remove the header.
      next if header =~ /transfer-encoding/i && value =~/chunked/i

      response_string << "#{header}: #{value}"
    end
  end if headers

  response_string << "" << body
  response_string.join("\n")
end