class VCR::LibraryHooks::FakeWeb::RequestHandler

@private

Attributes

net_http[R]
request[R]
request_body[R]
response_block[R]

Public Class Methods

new(net_http, request, request_body = nil, &response_block) click to toggle source
# File lib/vcr/library_hooks/fakeweb.rb, line 17
def initialize(net_http, request, request_body = nil, &response_block)
  @net_http, @request, @request_body, @response_block =
   net_http,  request,  request_body,  response_block
  @stubbed_response, @vcr_response, @recursing = nil, nil, false
end

Public Instance Methods

handle() click to toggle source
Calls superclass method VCR::RequestHandler#handle
# File lib/vcr/library_hooks/fakeweb.rb, line 23
def handle
  super
ensure
  invoke_after_request_hook(@vcr_response) unless @recursing
end

Private Instance Methods

externally_stubbed?() click to toggle source
# File lib/vcr/library_hooks/fakeweb.rb, line 31
def externally_stubbed?
  ::FakeWeb.registered_uri?(request_method, uri)
end
on_externally_stubbed_request() click to toggle source
# File lib/vcr/library_hooks/fakeweb.rb, line 35
def on_externally_stubbed_request
  # just perform the request--FakeWeb will handle it
  perform_request(:started)
end
on_ignored_request() click to toggle source
# File lib/vcr/library_hooks/fakeweb.rb, line 52
def on_ignored_request
  perform_request(net_http.started?)
end
on_recordable_request() click to toggle source
# File lib/vcr/library_hooks/fakeweb.rb, line 40
def on_recordable_request
  perform_request(net_http.started?, :record_interaction)
end
on_stubbed_by_vcr_request() click to toggle source
# File lib/vcr/library_hooks/fakeweb.rb, line 44
def on_stubbed_by_vcr_request
  with_exclusive_fakeweb_stub(stubbed_response) do
    # force it to be considered started since it doesn't
    # recurse in this case like the others.
    perform_request(:started)
  end
end
perform_request(started, record_interaction = false) click to toggle source
# File lib/vcr/library_hooks/fakeweb.rb, line 56
def perform_request(started, record_interaction = false)
  # Net::HTTP calls #request recursively in certain circumstances.
  # We only want to record the request when the request is started, as
  # that is the final time through #request.
  unless started
    @recursing = true
    request.instance_variable_set(:@__vcr_request_handler, recursive_request_handler)
    return net_http.request_without_vcr(request, request_body, &response_block)
  end

  net_http.request_without_vcr(request, request_body) do |response|
    @vcr_response = vcr_response_from(response)

    if record_interaction
      VCR.record_http_interaction VCR::HTTPInteraction.new(vcr_request, @vcr_response)
    end

    response.extend VCR::Net::HTTPResponse # "unwind" the response
    response_block.call(response) if response_block
  end
end
recursive_request_handler() click to toggle source
# File lib/vcr/library_hooks/fakeweb.rb, line 121
def recursive_request_handler
  @recursive_request_handler ||= RecursiveRequestHandler.new(
    @after_hook_typed_request.type, @stubbed_response, @vcr_request,
    @net_http, @request, @request_body, &@response_block
  )
end
request_method() click to toggle source
# File lib/vcr/library_hooks/fakeweb.rb, line 101
def request_method
  request.method.downcase.to_sym
end
response_hash(response) click to toggle source
# File lib/vcr/library_hooks/fakeweb.rb, line 82
def response_hash(response)
  (response.headers || {}).merge(
    :body   => response.body,
    :status => [response.status.code.to_s, response.status.message]
  )
end
uri() click to toggle source
# File lib/vcr/library_hooks/fakeweb.rb, line 78
def uri
  @uri ||= ::FakeWeb::Utility.request_uri_as_string(net_http, request)
end
vcr_request() click to toggle source
# File lib/vcr/library_hooks/fakeweb.rb, line 105
def vcr_request
  @vcr_request ||= VCR::Request.new              request_method,
    uri,
    (request_body || request.body),
    request.to_hash
end
vcr_response_from(response) click to toggle source
# File lib/vcr/library_hooks/fakeweb.rb, line 113
def vcr_response_from(response)
  VCR::Response.new              VCR::ResponseStatus.new(response.code.to_i, response.message),
    response.to_hash,
    response.body,
    response.http_version
end
with_exclusive_fakeweb_stub(response) { || ... } click to toggle source
# File lib/vcr/library_hooks/fakeweb.rb, line 89
def with_exclusive_fakeweb_stub(response)
  original_map = ::FakeWeb::Registry.instance.uri_map.dup
  ::FakeWeb.clean_registry
  ::FakeWeb.register_uri(:any, /.*/, response_hash(response))

  begin
    return yield
  ensure
    ::FakeWeb::Registry.instance.uri_map = original_map
  end
end