class VCR::Middleware::Faraday

Public Instance Methods

call(env) click to toggle source
# File lib/vcr/middleware/faraday.rb, line 10
def call(env)
  VCR::HttpStubbingAdapters::Faraday.exclusively_enabled do
    VCR.use_cassette(*cassette_arguments(env)) do |cassette|
      request = request_for(env)
      request_matcher = request.matcher(cassette.match_requests_on)

      if VCR::HttpStubbingAdapters::Faraday.uri_should_be_ignored?(request.uri)
        @app.call(env)
      elsif response = VCR::HttpStubbingAdapters::Faraday.stubbed_response_for(request_matcher)
        headers = env[:response_headers] ||= ::Faraday::Utils::Headers.new
        headers.update response.headers if response.headers
        env.update :status => response.status.code, :body => response.body

        faraday_response = ::Faraday::Response.new
        faraday_response.finish(env) unless env[:parallel_manager]
        env[:response] = faraday_response
      elsif VCR::HttpStubbingAdapters::Faraday.http_connections_allowed?
        response = @app.call(env)

        # Checking #enabled? isn't strictly needed, but conforms
        # the Faraday adapter to the behavior of the other adapters
        if VCR::HttpStubbingAdapters::Faraday.enabled?
          VCR.record_http_interaction(VCR::HTTPInteraction.new(request, response_for(env)))
        end

        response
      else
        raise HttpConnectionNotAllowedError.new(
          "Real HTTP connections are disabled. Request: #{request.method.inspect} #{request.uri}"
        )
      end
    end
  end
end