@private
# 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
# File lib/vcr/library_hooks/fakeweb.rb, line 23 def handle super ensure invoke_after_request_hook(@vcr_response) unless @recursing end
# File lib/vcr/library_hooks/fakeweb.rb, line 31 def externally_stubbed? ::FakeWeb.registered_uri?(request_method, uri) end
# 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
# File lib/vcr/library_hooks/fakeweb.rb, line 52 def on_ignored_request perform_request(net_http.started?) end
# File lib/vcr/library_hooks/fakeweb.rb, line 40 def on_recordable_request perform_request(net_http.started?, :record_interaction) end
# 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
# 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
# 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
# File lib/vcr/library_hooks/fakeweb.rb, line 101 def request_method request.method.downcase.to_sym end
# 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
# File lib/vcr/library_hooks/fakeweb.rb, line 78 def uri @uri ||= ::FakeWeb::Utility.request_uri_as_string(net_http, request) end
# 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
# 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
# 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