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



106
107
108
109
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 106

def connection_completed
  @state = :response_header
  send_request(*headers_and_body_processed_by_middleware)
end


155
156
157
158
159
160
161
162
163
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 155

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.detect { |c| c.start_with? name }
  cookie and cookie.split('=', 2)[1]
end

#request_signatureObject



143
144
145
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 143

def request_signature
  @request_signature ||= build_request_signature
end

#send_request(head, body) ⇒ Object



111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 111

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



94
95
96
97
98
99
100
101
102
103
104
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 94

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



147
148
149
150
151
152
153
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 147

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



129
130
131
132
133
134
135
136
137
138
139
140
141
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 129

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



90
91
92
# File 'lib/webmock/http_lib_adapters/em_http_request_adapter.rb', line 90

def uri
  @req.uri
end