Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
Changelog
=========

- [fix] Fix recompressing response to happen after replacing sensitive data (#951) by @cgunther

## 6.3.1 (Aug 20, 2024)
[Full Changelog](https://github.com/vcr/vcr/compare/v6.3.0...v6.3.1)

Expand Down Expand Up @@ -42,12 +44,12 @@ Changelog
- [fix] Use `YAML.unsafe_load` if available to load cassette data (better compatibility with Psych 4.0). (#911) by @casperisfine
- [patch] Improve error message for syntax error in ERB-using cassettes (#909) by @sambostock
- [patch] Handle `use_cassette(..., erb: {})` (#908) by @sambostock
- [fix] Use fiber-local for `global_hook_disabled_requests` (#907) by @jhawthorn
- [fix] Use fiber-local for `global_hook_disabled_requests` (#907) by @jhawthorn
- [docs] Document the RSpec cassette name shorthand (#821) by @nicolasiensen
- [fix] Fix the behavior of the option re_record_interval "none" (#824) by @nicolasiensen
- [fix] Fix compatibility with frozen string literals (#832) by @casperisfine
- [fix] [Transforms ERB hash keys to symbol, in case string (#833) by @z1lk
- [fix] Support Cucumber-Ruby v4 and later (#845) by @brasmusson
- [fix] Support Cucumber-Ruby v4 and later (#845) by @brasmusson
- [patch] Extract `#vcr_cassette_name_for` (#882) by @dabroz
- [fix] Fix CI to use GitHub Actions (#883) by @bradshjg
- [new] Add `#localhost_ignored?` to request_ignorer (#895) by @ThHareau
Expand Down
75 changes: 75 additions & 0 deletions features/cassettes/recompress.feature
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
Feature: Recompress response

When the `:recompress_response` option is set to a truthy value, VCR will
recompress "gzip" and "deflate" response bodies that were previously
decompressed via `:decode_compressed_response` before playback.

Background:
Given a file named "recompress.rb" with:
"""ruby
require 'zlib'
require 'stringio'

$server = start_sinatra_app do
get('/') {
content = "The quick brown fox jumps over the lazy dog"
io = StringIO.new

writer = Zlib::GzipWriter.new(io)
writer << content
writer.close

headers['Content-Encoding'] = 'gzip'
io.string
}
end

require 'vcr'

VCR.configure do |c|
c.cassette_library_dir = 'cassettes'
c.hook_into :webmock
c.default_cassette_options = { :serialize_with => :syck }
c.filter_sensitive_data('<COLOR>') { 'brown' }
end

VCR.use_cassette(:recompress, :decode_compressed_response => true) do
Net::HTTP.start('localhost', $server.port) do |http|
http.get('/', 'accept-encoding' => 'identity')
end
end
"""

Scenario: The option is not set by default
When I append to file "recompress.rb":
"""ruby
VCR.use_cassette(:recompress) do
response = Net::HTTP.get_response('localhost', '/', $server.port)
puts "Content-Encoding: #{response['Content-Encoding'] || 'none'}"
end
"""
And I run `ruby recompress.rb`
Then it should pass with "Content-Encoding: none"

Scenario: The option is enabled
When I append to file "recompress.rb":
"""ruby
VCR.use_cassette(:recompress, :recompress_response => true) do
response = Net::HTTP.get_response('localhost', '/', $server.port)
puts "Content-Encoding: #{response['Content-Encoding'] || 'none'}"
end
"""
And I run `ruby recompress.rb`
Then it should pass with "Content-Encoding: gzip"

Scenario: The recompressing happens after replacing filtered sensitive data
When I append to file "recompress.rb":
"""ruby
VCR.use_cassette(:recompress, :recompress_response => true) do
VCR::Response.decompress(Net::HTTP.get_response('localhost', '/', $server.port).body, 'gzip') do |body|
puts body
end
end
"""
And I run `ruby recompress.rb`
Then it should pass with "The quick brown fox jumps over the lazy dog"
1 change: 1 addition & 0 deletions lib/vcr/cassette.rb
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,7 @@ def previously_recorded_interactions
@previously_recorded_interactions ||= if !raw_cassette_bytes.to_s.empty?
deserialized_hash['http_interactions'].map { |h| HTTPInteraction.from_hash(h) }.tap do |interactions|
invoke_hook(:before_playback, interactions)
invoke_hook(:recompress_response, interactions)

interactions.reject! do |i|
i.request.uri.is_a?(String) && VCR.request_ignorer.ignore?(i.request)
Expand Down
4 changes: 3 additions & 1 deletion lib/vcr/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -566,8 +566,10 @@ def request_filter_from(object)
lambda { |arg| arg.send(object) }
end

define_hook :recompress_response

def register_built_in_hooks
before_playback(:recompress_response) do |interaction|
recompress_response(tag_filter_from(:recompress_response)) do |interaction|
interaction.response.recompress if interaction.response.vcr_decompressed?
end

Expand Down
2 changes: 2 additions & 0 deletions spec/lib/vcr/cassette_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,8 @@ def stub_old_interactions(interactions)
it 'invokes the before_playback hooks' do
VCR.configuration.cassette_library_dir = "#{VCR::SPEC_ROOT}/fixtures/cassette_spec"

allow(VCR.configuration).to receive(:invoke_hook).and_return([false])

expect(VCR.configuration).to receive(:invoke_hook).with(
:before_playback,
an_instance_of(VCR::HTTPInteraction::HookAware),
Expand Down