Class: EventMachine::WebMockHttpClient

Inherits:
HttpClient
  • Object
show all
Includes:
HttpEncoding
Defined in:
lib/webmock/http_lib_adapters/em_http_request_adapter.rb

Instance Method Summary collapse

Instance Method Details

#connection_completedObject



102
103
104
105
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 102

def connection_completed
  @state = :response_header
  send_request(request_signature.headers, request_signature.body)
end


151
152
153
154
155
156
157
158
159
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 151

def get_response_cookie(name)
  name = name.to_s

  raw_cookie = response_header.cookie
  raw_cookie = [raw_cookie] if raw_cookie.is_a? String

  cookie = raw_cookie.select { |c| c.start_with? name }.first
  cookie and cookie.split('=', 2)[1]
end

#request_signatureObject



139
140
141
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 139

def request_signature
  @request_signature ||= build_request_signature
end

#send_request(head, body) ⇒ Object



107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 107

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)
    @uri ||= nil
    EM.next_tick {
      setup(make_raw_response(stubbed_webmock_response), @uri,
            stubbed_webmock_response.should_timeout ? Errno::ETIMEDOUT : 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) ⇒ Object



90
91
92
93
94
95
96
97
98
99
100
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 90

def setup(response, uri, error = nil)
  @last_effective_url = @uri = uri
  if error
    on_error(error)
    @conn.drop_client
    fail(self)
  else
    @conn.receive_data(response)
    succeed(self)
  end
end

#stubbed_webmock_responseObject



143
144
145
146
147
148
149
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 143

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) ⇒ Object



125
126
127
128
129
130
131
132
133
134
135
136
137
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 125

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

#uriObject



86
87
88
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 86

def uri
  @req.uri
end