class VCR::Configuration
Stores the VCR configuration.
def after_http_request(*filters)
- See: #around_http_request -
See: #before_http_request -
Other tags:
- Yieldparam: response - the response from the request
Yieldparam: request - the request that is being made
Other tags:
- Yield: - the callback
Parameters:
-
filters
(optional splat of #to_proc
) -- one or more filters to apply.
def after_http_request(*filters) super(*filters.map { |f| request_filter_from(f) }) end
def allow_http_connections_when_no_cassette?
- Private: - (documented above)
def allow_http_connections_when_no_cassette? !!@allow_http_connections_when_no_cassette end
def around_http_request(*filters, &block)
- See: #after_http_request -
See: #before_http_request -
Other tags:
- Note: - You _must_ call `request.proceed` or pass the request as a proc on to a
Note: - This method can only be used on ruby interpreters that support
Parameters:
-
filters
(optional splat of #to_proc
) -- one or more filters to apply.
Raises:
-
(VCR::Errors::NotSupportedError)
- if the fiber library cannot be loaded.
Other tags:
- Yieldparam: request - the request that is being made
Other tags:
- Yield: - the callback
def around_http_request(*filters, &block) unless VCR.fibers_available? raise Errors::NotSupportedError.new \ "VCR::Configuration#around_http_request requires fibers, " + "which are not available on your ruby intepreter." end fibers = {} fiber_errors = {} hook_allowed, hook_declaration = false, caller.first before_http_request(*filters) do |request| hook_allowed = true start_new_fiber_for(request, fibers, fiber_errors, hook_declaration, block) end after_http_request(lambda { hook_allowed }) do |request, response| fiber = fibers.delete(Thread.current) resume_fiber(fiber, fiber_errors, response, hook_declaration) end end
def before_playback(tag = nil, &block)
- See: #before_record -
Other tags:
- Yieldparam: cassette - The current cassette.
Yieldparam: interaction - The interaction that is being
Other tags:
- Yield: - the callback
Parameters:
-
tag
((optional) Symbol
) -- Used to apply this hook to only cassettes that match
def before_playback(tag = nil, &block) super(tag_filter_from(tag), &block) end
def before_record(tag = nil, &block)
- See: #before_playback -
Other tags:
- Yieldparam: cassette - The current cassette.
Yieldparam: interaction - The interaction that will be
Other tags:
- Yield: - the callback
Parameters:
-
tag
((optional) Symbol
) -- Used to apply this hook to only cassettes that match
def before_record(tag = nil, &block) super(tag_filter_from(tag), &block) end
def cassette_library_dir
-
(String)
- the directory to read cassettes from and write cassettes to
def cassette_library_dir VCR.cassette_persisters[:file_system].storage_location end
def cassette_library_dir=(dir)
- Note: - This is only necessary if you use the `:file_system`
Returns:
-
(void)
-
Parameters:
-
dir
(String
) -- the directory to read cassettes from and write cassettes to
def cassette_library_dir=(dir) VCR.cassette_persisters[:file_system].storage_location = dir end
def cassette_persisters
- Note: - Custom persisters must implement the following interface:
Returns:
-
(VCR::Cassette::Persisters)
- the cassette persister registry object.
def cassette_persisters VCR.cassette_persisters end
def cassette_serializers
- Note: - Custom serializers must implement the following interface:
Returns:
-
(VCR::Cassette::Serializers)
- the cassette serializer registry object.
def cassette_serializers VCR.cassette_serializers end
def configure_rspec_metadata!
Configures RSpec to use a VCR cassette for any example
def configure_rspec_metadata! unless @rspec_metadata_configured VCR::RSpec::Metadata.configure! @rspec_metadata_configured = true end end
def create_fiber_for(fiber_errors, hook_declaration, proc)
def create_fiber_for(fiber_errors, hook_declaration, proc) current_thread = Thread.current Fiber.new do |*args, &block| begin # JRuby Fiber runs in a separate thread, so we need to make this Fiber # use the context of the calling thread VCR.link_context(current_thread, Fiber.current) if RUBY_PLATFORM == 'java' proc.call(*args, &block) rescue StandardError => ex # Fiber errors get swallowed, so we re-raise the error in the parent # thread (see resume_fiber) fiber_errors[current_thread] = ex raise ensure VCR.unlink_context(Fiber.current) if RUBY_PLATFORM == 'java' end end end
def debug_logger=(value)
- Private: - (documented above)
def debug_logger=(value) @debug_logger = value if value @logger = Logger.new(value) else @logger = Logger::Null end end
def default_cassette_options=(overrides)
def default_cassette_options=(overrides) @default_cassette_options.merge!(overrides) end
def define_cassette_placeholder(placeholder, tag = nil, &block)
- Yieldreturn: - the string to replace
Other tags:
- Yieldparam: interaction - the HTTP interaction
Other tags:
- Yield: - block that determines what string to replace
Parameters:
-
tag
(Symbol
) -- Set this to apply this only to cassettes -
placeholder
(String
) -- The placeholder string.
def define_cassette_placeholder(placeholder, tag = nil, &block) before_record(tag) do |interaction| orig_text = call_block(block, interaction) log "before_record: replacing #{orig_text.inspect} with #{placeholder.inspect}" interaction.filter!(orig_text, placeholder) end before_playback(tag) do |interaction| orig_text = call_block(block, interaction) log "before_playback: replacing #{orig_text.inspect} with #{placeholder.inspect}" interaction.filter!(placeholder, orig_text) end end
def hook_into(*hooks)
-
(VCR::Errors::LibraryVersionTooLowError)
- when the version -
(ArgumentError)
- when given an unsupported library name.
Parameters:
-
hooks
(Array
) -- List of libraries. Valid values are
def hook_into(*hooks) hooks.each { |a| load_library_hook(a) } invoke_hook(:after_library_hooks_loaded) end
def ignore_hosts(*hosts)
- See: #ignore_request -
See: #ignore_localhost= -
Parameters:
-
hosts
(Array
) -- List of hosts to ignore
def ignore_hosts(*hosts) VCR.request_ignorer.ignore_hosts(*hosts) end
def ignore_localhost=(value)
- See: #ignore_request -
See: #ignore_hosts -
Parameters:
-
value
(Boolean
) -- the value to set
def ignore_localhost=(value) VCR.request_ignorer.ignore_localhost = value end
def ignore_request(&block)
- Yieldreturn: - whether or not to ignore the request
Other tags:
- Yieldparam: request - the HTTP request
Other tags:
- Yield: - the callback
def ignore_request(&block) VCR.request_ignorer.ignore_request(&block) end
def initialize
def initialize @allow_http_connections_when_no_cassette = nil @rspec_metadata_configured = false @default_cassette_options = { :record => :once, :record_on_error => true, :match_requests_on => RequestMatcherRegistry::DEFAULT_MATCHERS, :allow_unused_http_interactions => true, :drop_unused_requests => false, :serialize_with => :yaml, :persist_with => :file_system, :persister_options => {} } self.uri_parser = URI self.query_parser = CGI.method(:parse) self.debug_logger = nil register_built_in_hooks end
def load_library_hook(hook)
def load_library_hook(hook) file = "vcr/library_hooks/#{hook}" require file rescue LoadError => e raise e unless e.message.include?(file) # in case WebMock itself is not available raise ArgumentError.new("#{hook.inspect} is not a supported VCR HTTP library hook.") end
def log_prefix
def log_prefix "[VCR::Configuration] " end
def preserve_exact_body_bytes_for?(http_message)
- See: #preserve_exact_body_bytes -
Parameters:
-
http_message
(#body, #headers
) -- the `VCR::Request` or `VCR::Response` object being serialized
Returns:
-
(Boolean)
- whether or not the body of the given HTTP message should
def preserve_exact_body_bytes_for?(http_message) invoke_hook(:preserve_exact_body_bytes, http_message, VCR.current_cassette).any? end
def register_built_in_hooks
def register_built_in_hooks before_playback(:recompress_response) do |interaction| interaction.response.recompress if interaction.response.vcr_decompressed? end before_playback(:update_content_length_header) do |interaction| interaction.response.update_content_length_header end before_record(:decode_compressed_response) do |interaction| interaction.response.decompress if interaction.response.compressed? end preserve_exact_body_bytes do |http_message, cassette| cassette && cassette.tags.include?(:preserve_exact_body_bytes) end end
def register_request_matcher(name, &block)
- Yieldreturn: - whether or not these two requests should be considered
Other tags:
- Yieldparam: request_2 - The other request
Yieldparam: request_1 - One request
Other tags:
- Yield: - the request matcher
Parameters:
-
name
(Symbol
) -- the name of the request matcher
def register_request_matcher(name, &block) VCR.request_matchers.register(name, &block) end
def request_filter_from(object)
def request_filter_from(object) return object unless object.is_a?(Symbol) lambda { |arg| arg.send(object) } end
def resume_fiber(fiber, fiber_errors, response, hook_declaration)
def resume_fiber(fiber, fiber_errors, response, hook_declaration) raise fiber_errors[Thread.current] if fiber_errors[Thread.current] fiber.resume(response) rescue FiberError => ex raise Errors::AroundHTTPRequestHookError.new \ "Your around_http_request hook declared at #{hook_declaration}" \ " must call #proceed on the yielded request but did not. " \ "(actual error: #{ex.class}: #{ex.message})" end
def start_new_fiber_for(request, fibers, fiber_errors, hook_declaration, proc)
def start_new_fiber_for(request, fibers, fiber_errors, hook_declaration, proc) fiber = create_fiber_for(fiber_errors, hook_declaration, proc) fibers[Thread.current] = fiber fiber.resume(Request::FiberAware.new(request)) end
def stub_with(*adapters)
- See: #hook_into -
Deprecated:
- Use #hook_into instead.
def stub_with(*adapters) warn "WARNING: `VCR.configure { |c| c.stub_with ... }` is deprecated. Use `VCR.configure { |c| c.hook_into ... }` instead." hook_into(*adapters) end
def tag_filter_from(tag)
def tag_filter_from(tag) return lambda { true } unless tag lambda { |_, cassette| cassette.tags.include?(tag) } end
def unignore_hosts(*hosts)
- See: #ignore_hosts -
Parameters:
-
hosts
(Array
) -- List of hosts to unignore
def unignore_hosts(*hosts) VCR.request_ignorer.unignore_hosts(*hosts) end