class VCR::Middleware::Excon::RequestHandler
@private
Handles a single Excon request.
def after_request(response)
- Private: -
def after_request(response) vcr_response = vcr_response_for(response) if vcr_response && should_record? VCR.record_http_interaction(VCR::HTTPInteraction.new(vcr_request, vcr_response)) end invoke_after_request_hook(vcr_response) end
def before_request(request_params)
- Private: -
def before_request(request_params) @request_params = request_params @response_body_reader = create_response_body_reader handle end
def create_response_body_reader
def create_response_body_reader block = request_params[:response_block] return NonStreamingResponseBodyReader unless block StreamingResponseBodyReader.new(block).tap do |response_block_wrapper| request_params[:response_block] = response_block_wrapper end end
def ensure_response_body_can_be_read_for_error_case
def ensure_response_body_can_be_read_for_error_case # Excon does not invoke the `:response_block` when an error # has occurred, so we need to be sure to use the non-streaming # body reader. @response_body_reader = NonStreamingResponseBodyReader end
def externally_stubbed?
def externally_stubbed? !!::Excon.stub_for(request_params) end
def initialize
def initialize @request_params = nil @response_body_reader = nil @should_record = false end
def normalized_headers(headers)
def normalized_headers(headers) normalized = {} headers.each do |k, v| v = v.join(', ') if v.respond_to?(:join) normalized[k] = v end normalized end
def on_recordable_request
def on_recordable_request @should_record = true end
def on_stubbed_by_vcr_request
def on_stubbed_by_vcr_request request_params[:response] = { :body => stubbed_response.body.dup, # Excon mutates the body, so we must dup it :-( :headers => normalized_headers(stubbed_response.headers || {}), :status => stubbed_response.status.code } end
def should_record?
def should_record? @should_record end
def uri
def uri @uri ||= "#{::Excon::Utils.request_uri(request_params)}" end
def uri
def uri @uri ||= "#{request_params[:scheme]}://#{request_params[:host]}:#{request_params[:port]}#{request_params[:path]}#{query}" end
def vcr_request
def vcr_request @vcr_request ||= begin headers = request_params[:headers].dup headers.delete("Host") VCR::Request.new \ request_params[:method], uri, request_params[:body], headers end end
def vcr_response_for(response)
def vcr_response_for(response) return nil if response.nil? VCR::Response.new( VCR::ResponseStatus.new(response.fetch(:status), nil), response.fetch(:headers), response_body_reader.read_body_from(response), nil ) end