Skip to content

Commit

Permalink
Default to false and add feature documentation
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanseys committed Oct 14, 2022
1 parent b3b58c2 commit bf38ee2
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 0 deletions.
87 changes: 87 additions & 0 deletions features/cassettes/drop_unused_requests.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
Feature: Drop Unused Requests

If set to true, this cassette option will cause VCR to drop any unused requests
from the cassette when a cassette is ejected. This is useful for reducing the
size of cassettes that contain a large number of requests that are not used.

The option defaults to false (mostly for backwards compatibility).

Background:
Given a file named "vcr_config.rb" with:
"""ruby
require 'vcr'
VCR.configure do |c|
c.hook_into :webmock
c.cassette_library_dir = 'cassettes'
end
"""
And a previously recorded cassette file "cassettes/example.yml" with:
"""
---
http_interactions:
- request:
method: get
uri: http://example.com/foo
body:
encoding: UTF-8
string: ""
headers: {}
response:
status:
code: 200
message: OK
headers:
Content-Length:
- "5"
body:
encoding: UTF-8
string: Existing Response
http_version: "1.1"
recorded_at: Tue, 01 Nov 2011 04:58:44 GMT
recorded_with: VCR 2.0.0
"""

Scenario: Unused requests are not dropped from the cassette by default
Given a file named "not_dropped_by_default.rb" with:
"""ruby
$server = start_sinatra_app do
get('/') { 'New Response' }
end
require 'vcr'
VCR.configure do |c|
c.hook_into :webmock
c.cassette_library_dir = 'cassettes'
end
VCR.use_cassette('example', :record => :all) do
puts Net::HTTP.get_response('localhost', '/', $server.port).body
end
"""
When I run `ruby not_dropped_by_default.rb`
Then the file "cassettes/example.yml" should contain "New Response"
And the file "cassettes/example.yml" should contain "Existing Response"

Scenario: Unused requests are dropped from the cassette when the option is set
Given a file named "drop_unused_requests_set.rb" with:
"""ruby
$server = start_sinatra_app do
get('/') { 'New Response' }
end
require 'vcr'
VCR.configure do |c|
c.hook_into :webmock
c.cassette_library_dir = 'cassettes'
end
VCR.use_cassette('example', :record => :all, :drop_unused_requests => true) do
puts Net::HTTP.get_response('localhost', '/', $server.port).body
end
"""
When I run `ruby drop_unused_requests_set.rb`
Then the file "cassettes/example.yml" should contain "New Response"
But the file "cassettes/example.yml" should not contain "Existing Response"
18 changes: 18 additions & 0 deletions features/configuration/default_cassette_options.feature
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,24 @@ Feature: default_cassette_options
When I run `ruby default_record_on_error.rb`
Then the output should contain "Record on error: true"

Scenario: `:drop_unused_requests` defaults to `false` when it has not been set
Given a file named "default_drop_unused_requests.rb" with:
"""ruby
require 'vcr'
VCR.configure do |c|
# not important for this example, but must be set to something
c.hook_into :webmock
c.cassette_library_dir = 'cassettes'
end
VCR.use_cassette('example') do
puts "Drop unused requests: #{VCR.current_cassette.drop_unused_requests}"
end
"""
When I run `ruby default_drop_unused_requests.rb`
Then the output should contain "Drop unused requests: false"

Scenario: cassettes can set their own options
Given a file named "default_cassette_options.rb" with:
"""ruby
Expand Down
3 changes: 3 additions & 0 deletions lib/vcr/cassette.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ class Cassette
# @return [Boolean, nil] Should outdated interactions be recorded back to file
attr_reader :clean_outdated_http_interactions

# @return [Boolean] Should unused requests be dropped from the cassette?
attr_reader :drop_unused_requests

# @return [Array<Symbol>] If set, {VCR::Configuration#before_record} and
# {VCR::Configuration#before_playback} hooks with a corresponding tag will apply.
attr_reader :tags
Expand Down
1 change: 1 addition & 0 deletions lib/vcr/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -495,6 +495,7 @@ def initialize
: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 => {}
Expand Down

0 comments on commit bf38ee2

Please sign in to comment.