Sha256: 6567cf7b7afa188a5b572c942943130d2d143852a1c8246bdb12d9c724d8988c

Contents?: true

Size: 1.77 KB

Versions: 1

Compression:

Stored size: 1.77 KB

Contents

require 'faraday'

module VCR
  module HttpStubbingAdapters
    module Faraday
      include Common
      extend self

      MINIMUM_VERSION = '0.5.3'
      MAXIMUM_VERSION = '0.5'

      attr_writer :http_connections_allowed, :ignore_localhost

      def http_connections_allowed?
        !!@http_connections_allowed
      end

      def ignore_localhost?
        !!@ignore_localhost
      end

      def stub_requests(http_interactions, match_attributes)
        grouped_responses(http_interactions, match_attributes).each do |request_matcher, responses|
          queue = stub_queues[request_matcher]
          responses.each { |res| queue << res }
        end
      end

      def create_stubs_checkpoint(checkpoint_name)
        checkpoints[checkpoint_name] = stub_queue_dup
      end

      def restore_stubs_checkpoint(checkpoint_name)
        @stub_queues = checkpoints.delete(checkpoint_name)
      end

      def stubbed_response_for(request_matcher)
        queue = stub_queues[request_matcher]
        return queue.shift if queue.size > 1
        queue.first
      end

      def reset!
        instance_variables.each do |ivar|
          remove_instance_variable(ivar)
        end
      end

      private

        def version
          ::Faraday::VERSION
        end

        def checkpoints
          @checkpoints ||= {}
        end

        def stub_queues
          @stub_queues ||= hash_of_arrays
        end

        def stub_queue_dup
          dup = hash_of_arrays

          stub_queues.each do |k, v|
            dup[k] = v.dup
          end

          dup
        end

        def hash_of_arrays
          Hash.new { |h, k| h[k] = [] }
        end
    end
  end
end

VCR::HttpStubbingAdapters::Common.add_vcr_info_to_exception_message(VCR::Middleware::Faraday::HttpConnectionNotAllowedError)

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
vcr-1.4.0 lib/vcr/http_stubbing_adapters/faraday.rb