Top Level Namespace

Defined Under Namespace

Modules: Curl, EventMachine, HTTP, Net, Test, WebMock, WebMockHTTPClients Classes: MessageMock, PatchedStringIO, StubSocket, WebMockHTTPClient, WebMockJSONClient

Constant Summary

RSPEC_NAMESPACE =
Spec
RSPEC_CONFIGURER =
Spec::Runner

Instance Method Summary collapse

Instance Method Details

#build_request_signature(req, reuse_existing = false) ⇒ Object



169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
# File 'lib/webmock/http_lib_adapters/httpclient_adapter.rb', line 169

def build_request_signature(req, reuse_existing = false)
  uri = WebMock::Util::URI.heuristic_parse(req.header.request_uri.to_s)
  uri.query = WebMock::Util::QueryMapper.values_to_query(req.header.request_query, :notation => WebMock::Config.instance.query_values_notation) if req.header.request_query
  uri.port = req.header.request_uri.port

  @request_filter.each do |filter|
    filter.filter_request(req)
  end

  headers = req.header.all.inject({}) do |hdrs, header|
    hdrs[header[0]] ||= []
    hdrs[header[0]] << header[1]
    hdrs
  end
  headers = headers_from_session(uri).merge(headers)

  signature = WebMock::RequestSignature.new(
    req.header.request_method.downcase.to_sym,
    uri.to_s,
    :body => req.http_body.dump,
    :headers => headers
  )

  # reuse a previous identical signature object if we stored one for later use
  if reuse_existing && previous_signature = previous_signature_for(signature)
    return previous_signature
  end

  signature
end

#build_webmock_response(httpclient_response, body = nil) ⇒ Object



142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
# File 'lib/webmock/http_lib_adapters/httpclient_adapter.rb', line 142

def build_webmock_response(httpclient_response, body = nil)
  webmock_response = WebMock::Response.new
  webmock_response.status = [httpclient_response.status, httpclient_response.reason]

  webmock_response.headers = {}.tap do |hash|
    httpclient_response.header.all.each do |(key, value)|
      if hash.has_key?(key)
        hash[key] = Array(hash[key]) + [value]
      else
        hash[key] = value
      end
    end
  end

  if body
    webmock_response.body = body
  elsif httpclient_response.content.respond_to?(:read)
    webmock_response.body = httpclient_response.content.read
    body = HTTP::Message::Body.new
    body.init_response(StringIO.new(webmock_response.body))
    httpclient_response.body = body
  else
    webmock_response.body = httpclient_response.content
  end
  webmock_response
end

#previous_signature_for(signature) ⇒ Object



210
211
212
213
# File 'lib/webmock/http_lib_adapters/httpclient_adapter.rb', line 210

def previous_signature_for(signature)
  return nil unless index = webmock_request_signatures.index(signature)
  webmock_request_signatures.delete_at(index)
end

#webmock_request_signaturesObject



206
207
208
# File 'lib/webmock/http_lib_adapters/httpclient_adapter.rb', line 206

def webmock_request_signatures
  @webmock_request_signatures ||= []
end

#webmock_responsesObject



200
201
202
203
204
# File 'lib/webmock/http_lib_adapters/httpclient_adapter.rb', line 200

def webmock_responses
  @webmock_responses ||= Hash.new do |hash, request_signature|
    hash[request_signature] = WebMock::StubRegistry.instance.response_for_request(request_signature)
  end
end