Class: VCR::Configuration
- Inherits:
-
Object
- Object
- VCR::Configuration
- 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.
-
#cassette_library_dir ⇒ Object
The directory to read cassettes from and write cassettes to.
-
#debug_logger ⇒ Object
An object to log debug output to.
-
#default_cassette_options ⇒ Object
Default options to apply to every cassette.
Instance Method Summary collapse
-
#around_http_request(*filters) {|request| ... } ⇒ Object
Adds a callback that will be executed around each HTTP request.
- #before_playback(tag = nil, &block) ⇒ Object
- #before_record(tag = nil, &block) ⇒ Object
-
#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| ... }
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.
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.
118 119 120 |
# File 'lib/vcr/configuration.rb', line 118 def allow_http_connections_when_no_cassette=(value) @allow_http_connections_when_no_cassette = value end |
#cassette_library_dir ⇒ String #cassette_library_dir=(dir)
The directory to read cassettes from and write cassettes to.
22 23 24 |
# File 'lib/vcr/configuration.rb', line 22 def cassette_library_dir @cassette_library_dir end |
#debug_logger ⇒ #puts #debug_logger=(logger)
An object to log debug output to.
360 361 362 |
# File 'lib/vcr/configuration.rb', line 360 def debug_logger @debug_logger end |
#default_cassette_options ⇒ Hash #default_cassette_options=(options)
VCR#insert_cassette for the list of valid options.
Default options to apply to every cassette.
42 43 44 |
# File 'lib/vcr/configuration.rb', line 42 def end |
Instance Method Details
#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.
321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 |
# File 'lib/vcr/configuration.rb', line 321 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 fiber, hook_allowed, hook_decaration = nil, false, caller.first before_http_request(*filters) do |request| hook_allowed = true fiber = start_new_fiber_for(request, block) end after_http_request(lambda { hook_allowed }) do |request, response| resume_fiber(fiber, response, hook_decaration) end end |
#before_playback(tag = nil, &block) ⇒ Object
254 255 256 |
# File 'lib/vcr/configuration.rb', line 254 def before_playback(tag = nil, &block) super(filter_from(tag), &block) end |
#before_record(tag = nil, &block) ⇒ Object
226 227 228 |
# File 'lib/vcr/configuration.rb', line 226 def before_record(tag = nil, &block) super(filter_from(tag), &block) end |
#cassette_serializers ⇒ VCR::Cassette::Serializers
Custom serializers must implement the following interface:
file_extension # => Stringserialize(Hash) # => Stringdeserialize(String) # => Hash
Gets the registry of cassette serializers. Use it to register a custom serializer.
198 199 200 |
# File 'lib/vcr/configuration.rb', line 198 def cassette_serializers VCR.cassette_serializers end |
#configure_rspec_metadata! ⇒ Object
Configures RSpec to use a VCR cassette for any example
tagged with :vcr.
341 342 343 |
# File 'lib/vcr/configuration.rb', line 341 def VCR::RSpec::Metadata.configure! 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.
170 171 172 173 174 175 176 177 178 179 180 181 182 |
# File 'lib/vcr/configuration.rb', line 170 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.
63 64 65 66 |
# File 'lib/vcr/configuration.rb', line 63 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.
73 74 75 |
# File 'lib/vcr/configuration.rb', line 73 def ignore_hosts(*hosts) VCR.request_ignorer.ignore_hosts(*hosts) end |
#ignore_localhost=(value) ⇒ Object
Sets whether or not VCR should ignore localhost requests.
83 84 85 |
# File 'lib/vcr/configuration.rb', line 83 def ignore_localhost=(value) VCR.request_ignorer.ignore_localhost = value end |
#ignore_request {|request| ... } ⇒ Object
Defines what requests to ignore using a block.
101 102 103 |
# File 'lib/vcr/configuration.rb', line 101 def ignore_request(&block) VCR.request_ignorer.ignore_request(&block) end |
#preserve_exact_body_bytes {|http_message, cassette| ... }
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.
384 |
# File 'lib/vcr/configuration.rb', line 384 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.
390 391 392 |
# File 'lib/vcr/configuration.rb', line 390 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.
143 144 145 |
# File 'lib/vcr/configuration.rb', line 143 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.config { |c| c.stub_with ... }` is deprecated. Use `VCR.configure { |c| c.hook_into ... }` instead." hook_into(*adapters) end |