Class: VCR::Configuration
- Inherits:
-
Object
- Object
- VCR::Configuration
- Includes:
- Hooks, Logger::Mixin, VariableArgsBlockCaller
- Defined in:
- lib/vcr/configuration.rb,
lib/vcr/deprecations.rb
Overview
Stores the VCR configuration.
Instance Attribute Summary collapse
-
#allow_http_connections_when_no_cassette ⇒ Object
writeonly
Determines how VCR treats HTTP requests that are made when no VCR cassette is in use.
-
#debug_logger ⇒ Object
An object to log debug output to.
-
#default_cassette_options ⇒ Object
Default options to apply to every cassette.
-
#logger ⇒ Object
readonly
Logger object that provides logging APIs and helper methods.
-
#query_parser ⇒ Object
Sets a parser for VCR to use when parsing query strings for request comparisons.
-
#uri_parser ⇒ Object
Sets a parser for VCR to use when parsing URIs.
Instance Method Summary collapse
-
#after_http_request(*filters) {|request, response| ... } ⇒ Object
Adds a callback that will be called with each HTTP request after it is complete.
- #allow_http_connections_when_no_cassette? ⇒ Boolean
-
#around_http_request(*filters) {|request| ... } ⇒ Object
Adds a callback that will be executed around each HTTP request.
-
#before_playback(tag = nil) {|interaction, cassette| ... } ⇒ Object
Adds a callback that will be called before a previously recorded HTTP interaction is loaded for playback.
-
#before_record(tag = nil) {|interaction, cassette| ... } ⇒ Object
Adds a callback that will be called before the recorded HTTP interactions are serialized and written to disk.
-
#cassette_library_dir ⇒ String
Gets the directory to read cassettes from and write cassettes to.
-
#cassette_library_dir=(dir) ⇒ void
Sets the directory to read cassettes from and writes cassettes to.
-
#cassette_persisters ⇒ VCR::Cassette::Persisters
Gets the registry of cassette persisters.
-
#cassette_serializers ⇒ VCR::Cassette::Serializers
Gets the registry of cassette serializers.
-
#configure_rspec_metadata! ⇒ Object
Configures RSpec to use a VCR cassette for any example tagged with ‘:vcr`.
-
#define_cassette_placeholder(placeholder, tag = nil) {|interaction| ... } ⇒ Object
(also: #filter_sensitive_data)
Sets up a #before_record and a #before_playback hook that will insert a placeholder string in the cassette in place of another string.
-
#hook_into(*hooks) ⇒ Object
Configures which libraries VCR will hook into to intercept HTTP requests.
-
#ignore_hosts(*hosts) ⇒ Object
(also: #ignore_host)
Specifies host(s) that VCR should ignore.
-
#ignore_localhost=(value) ⇒ Object
Sets whether or not VCR should ignore localhost requests.
-
#ignore_request {|request| ... } ⇒ Object
Defines what requests to ignore using a block.
-
#preserve_exact_body_bytes {|http_message, cassette| ... } ⇒ void
Sets a callback that determines whether or not to base64 encode the bytes of a request or response body during serialization in order to preserve them exactly.
-
#preserve_exact_body_bytes_for?(http_message) ⇒ Boolean
Whether or not the body of the given HTTP message should be base64 encoded during serialization in order to preserve the bytes exactly.
-
#register_request_matcher(name) {|request_1, request_2| ... } ⇒ Object
Registers a request matcher for later use.
-
#stub_with(*adapters) ⇒ Object
deprecated
Deprecated.
Use #hook_into instead.
Methods included from Logger::Mixin
#log, #request_summary, #response_summary
Methods included from VariableArgsBlockCaller
Methods included from Hooks
#clear_hooks, #has_hooks_for?, #hooks, included, #invoke_hook
Instance Attribute Details
#allow_http_connections_when_no_cassette? ⇒ Boolean (writeonly) #allow_http_connections_when_no_cassette= ⇒ Object (writeonly)
Determines how VCR treats HTTP requests that are made when no VCR cassette is in use. When set to ‘true`, requests made when there is no VCR cassette in use will be allowed. When set to `false` (the default), an Errors::UnhandledHTTPRequestError will be raised for any HTTP request made when there is no cassette in use.
122 123 124 |
# File 'lib/vcr/configuration.rb', line 122 def allow_http_connections_when_no_cassette=(value) @allow_http_connections_when_no_cassette = value end |
#debug_logger ⇒ #puts #debug_logger=(logger) ⇒ void
An object to log debug output to.
433 434 435 |
# File 'lib/vcr/configuration.rb', line 433 def debug_logger @debug_logger end |
#default_cassette_options ⇒ Hash #default_cassette_options=(options) ⇒ void
VCR#insert_cassette for the list of valid options.
Default options to apply to every cassette.
46 47 48 |
# File 'lib/vcr/configuration.rb', line 46 def @default_cassette_options end |
#logger ⇒ Object (readonly)
Logger object that provides logging APIs and helper methods.
447 448 449 |
# File 'lib/vcr/configuration.rb', line 447 def logger @logger end |
#query_parser ⇒ #call #query_parser= ⇒ Object
Sets a parser for VCR to use when parsing query strings for request comparisons. The new parser must implement a method ‘call` that returns an object which is both equalivant and consistent when given an HTTP query string of possibly differing value ordering.
-
‘#== # => Boolean`
The ‘#==` method must return true if both objects represent the same query string.
This defaults to ‘CGI.parse` from the ruby standard library.
144 145 146 |
# File 'lib/vcr/configuration.rb', line 144 def query_parser @query_parser end |
#uri_parser ⇒ #parse #uri_parser= ⇒ Object
Sets a parser for VCR to use when parsing URIs. The new parser must implement a method ‘parse` that returns an instance of the URI object. This URI object must implement the following interface:
-
‘scheme # => String`
-
‘host # => String`
-
‘port # => Fixnum`
-
‘path # => String`
-
‘query # => String`
-
‘#port=`
-
‘#query=`
-
‘#to_s # => String`
-
‘#== # => Boolean`
The ‘#==` method must return true if both URI objects represent the same URI.
This defaults to ‘URI` from the ruby standard library.
170 171 172 |
# File 'lib/vcr/configuration.rb', line 170 def uri_parser @uri_parser end |
Instance Method Details
#after_http_request(*filters) {|request, response| ... } ⇒ Object
Adds a callback that will be called with each HTTP request after it is complete.
359 360 361 |
# File 'lib/vcr/configuration.rb', line 359 def after_http_request(*filters) super(*filters.map { |f| request_filter_from(f) }) end |
#allow_http_connections_when_no_cassette? ⇒ Boolean
124 125 126 |
# File 'lib/vcr/configuration.rb', line 124 def allow_http_connections_when_no_cassette? !!@allow_http_connections_when_no_cassette end |
#around_http_request(*filters) {|request| ... } ⇒ Object
This method can only be used on ruby interpreters that support fibers (i.e. 1.9+). On 1.8 you can use separate ‘before_http_request` and `after_http_request` hooks.
You must call ‘request.proceed` or pass the request as a proc on to a method that yields to a block (i.e. `some_method(&request)`).
Adds a callback that will be executed around each HTTP request.
388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 |
# File 'lib/vcr/configuration.rb', line 388 def around_http_request(*filters, &block) require 'fiber' rescue LoadError raise Errors::NotSupportedError.new \ "VCR::Configuration#around_http_request requires fibers, " + "which are not available on your ruby intepreter." else fibers = {} hook_allowed, hook_decaration = false, caller.first before_http_request(*filters) do |request| hook_allowed = true fiber = start_new_fiber_for(request, block) fibers[Thread.current] = fiber end after_http_request(lambda { hook_allowed }) do |request, response| fiber = fibers.delete(Thread.current) resume_fiber(fiber, response, hook_decaration) end end |
#before_playback(tag = nil) {|interaction, cassette| ... } ⇒ Object
Adds a callback that will be called before a previously recorded HTTP interaction is loaded for playback.
318 319 320 |
# File 'lib/vcr/configuration.rb', line 318 def before_playback(tag = nil, &block) super(tag_filter_from(tag), &block) end |
#before_record(tag = nil) {|interaction, cassette| ... } ⇒ Object
Adds a callback that will be called before the recorded HTTP interactions are serialized and written to disk.
290 291 292 |
# File 'lib/vcr/configuration.rb', line 290 def before_record(tag = nil, &block) super(tag_filter_from(tag), &block) end |
#cassette_library_dir ⇒ String
Gets the directory to read cassettes from and write cassettes to.
15 16 17 |
# File 'lib/vcr/configuration.rb', line 15 def cassette_library_dir VCR.cassette_persisters[:file_system].storage_location end |
#cassette_library_dir=(dir) ⇒ void
This is only necessary if you use the ‘:file_system` cassette persister (the default).
This method returns an undefined value.
Sets the directory to read cassettes from and writes cassettes to.
30 31 32 |
# File 'lib/vcr/configuration.rb', line 30 def cassette_library_dir=(dir) VCR.cassette_persisters[:file_system].storage_location = dir end |
#cassette_persisters ⇒ VCR::Cassette::Persisters
262 263 264 |
# File 'lib/vcr/configuration.rb', line 262 def cassette_persisters VCR.cassette_persisters end |
#cassette_serializers ⇒ VCR::Cassette::Serializers
Custom serializers must implement the following interface:
-
‘file_extension # => String`
-
‘serialize(Hash) # => String`
-
‘deserialize(String) # => Hash`
Gets the registry of cassette serializers. Use it to register a custom serializer.
246 247 248 |
# File 'lib/vcr/configuration.rb', line 246 def cassette_serializers VCR.cassette_serializers end |
#configure_rspec_metadata! ⇒ Object
Configures RSpec to use a VCR cassette for any example tagged with ‘:vcr`.
411 412 413 414 415 416 |
# File 'lib/vcr/configuration.rb', line 411 def unless @rspec_metadata_configured VCR::RSpec::Metadata.configure! @rspec_metadata_configured = true end end |
#define_cassette_placeholder(placeholder, tag = nil) {|interaction| ... } ⇒ Object Also known as: filter_sensitive_data
Sets up a #before_record and a #before_playback hook that will insert a placeholder string in the cassette in place of another string. You can use this as a generic way to interpolate a variable into the cassette for a unique string. It’s particularly useful for unique sensitive strings like API keys and passwords.
218 219 220 221 222 223 224 225 226 227 228 229 230 |
# File 'lib/vcr/configuration.rb', line 218 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 #{placeholder.inspect} with #{orig_text.inspect}" interaction.filter!(placeholder, orig_text) end end |
#hook_into(*hooks) ⇒ Object
‘:fakeweb` and `:webmock` cannot both be used since they both monkey patch `Net::HTTP`. Otherwise, you can use any combination of these.
Configures which libraries VCR will hook into to intercept HTTP requests.
67 68 69 70 |
# File 'lib/vcr/configuration.rb', line 67 def hook_into(*hooks) hooks.each { |a| load_library_hook(a) } invoke_hook(:after_library_hooks_loaded) end |
#ignore_hosts(*hosts) ⇒ Object Also known as: ignore_host
Specifies host(s) that VCR should ignore.
77 78 79 |
# File 'lib/vcr/configuration.rb', line 77 def ignore_hosts(*hosts) VCR.request_ignorer.ignore_hosts(*hosts) end |
#ignore_localhost=(value) ⇒ Object
Sets whether or not VCR should ignore localhost requests.
87 88 89 |
# File 'lib/vcr/configuration.rb', line 87 def ignore_localhost=(value) VCR.request_ignorer.ignore_localhost = value end |
#ignore_request {|request| ... } ⇒ Object
Defines what requests to ignore using a block.
105 106 107 |
# File 'lib/vcr/configuration.rb', line 105 def ignore_request(&block) VCR.request_ignorer.ignore_request(&block) end |
#preserve_exact_body_bytes {|http_message, cassette| ... } ⇒ void
This is usually only necessary when the HTTP server returns a response with a non-standard encoding or with a body containing invalid bytes for the given encoding. Note that when you set this, and the block returns true, you sacrifice the human readability of the data in the cassette.
This method returns an undefined value.
Sets a callback that determines whether or not to base64 encode the bytes of a request or response body during serialization in order to preserve them exactly.
471 |
# File 'lib/vcr/configuration.rb', line 471 define_hook :preserve_exact_body_bytes |
#preserve_exact_body_bytes_for?(http_message) ⇒ Boolean
Returns whether or not the body of the given HTTP message should be base64 encoded during serialization in order to preserve the bytes exactly.
477 478 479 |
# File 'lib/vcr/configuration.rb', line 477 def preserve_exact_body_bytes_for?() invoke_hook(:preserve_exact_body_bytes, , VCR.current_cassette).any? end |
#register_request_matcher(name) {|request_1, request_2| ... } ⇒ Object
Registers a request matcher for later use.
191 192 193 |
# File 'lib/vcr/configuration.rb', line 191 def register_request_matcher(name, &block) VCR.request_matchers.register(name, &block) end |
#stub_with(*adapters) ⇒ Object
Use #hook_into instead.
26 27 28 29 |
# File 'lib/vcr/deprecations.rb', line 26 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 |