Skip to content

Commit

Permalink
Add new :preserve_exact_body_bytes cassette config option.
Browse files Browse the repository at this point in the history
  • Loading branch information
myronmarston committed Feb 21, 2012
1 parent eb213a7 commit befc480
Show file tree
Hide file tree
Showing 5 changed files with 60 additions and 5 deletions.
39 changes: 38 additions & 1 deletion features/configuration/preserve_exact_body_bytes.feature
Expand Up @@ -11,7 +11,11 @@ Feature: Preserve Exact Body Bytes
VCR provides a configuration option to deal with cases like these. The `preserve_exact_body_bytes`
method accepts a block that VCR will use to determine if the body of the given request or response object
should be base64 encoded in order to preserve the bytes exactly as-is. VCR does not do this by
default, since base64-encoding the string removes the human readibility.
default, since base64-encoding the string removes the human readibility of the cassette.

Alternately, if you want to force an entire cassette to preserve the exact body bytes,
you can pass the `:preserve_exact_body_bytes => true` cassette option when inserting your
cassette.

Scenario: Preserve exact bytes for response body with invalid encoding
Given a file named "preserve.rb" with:
Expand Down Expand Up @@ -64,3 +68,36 @@ Feature: Preserve Exact Body Bytes
"body":{"encoding":"ASCII-8BIT","base64_string":"YWJjIPo=\n"}
"""

Scenario: Preserve exact bytes for cassette with `:preserve_exact_body_bytes` option
Given a file named "preserve.rb" with:
"""ruby
start_sinatra_app(:port => 7777) do
get('/') { "Hello World" }
end
require 'vcr'
VCR.configure do |c|
c.cassette_library_dir = 'cassettes'
c.hook_into :fakeweb
c.default_cassette_options = { :serialize_with => :json }
end
VCR.use_cassette('preserve_bytes', :preserve_exact_body_bytes => true) do
Net::HTTP.get_response(URI("http://localhost:7777/"))
end
VCR.use_cassette('dont_preserve_bytes') do
Net::HTTP.get_response(URI("http://localhost:7777/"))
end
"""
When I run `ruby preserve.rb`
Then the file "cassettes/preserve_bytes.json" should contain:
"""
"body":{"encoding":"US-ASCII","base64_string":"SGVsbG8gV29ybGQ=\n"}
"""
And the file "cassettes/dont_preserve_bytes.json" should contain:
"""
"body":{"encoding":"US-ASCII","string":"Hello World"}
"""

3 changes: 3 additions & 0 deletions lib/vcr.rb
Expand Up @@ -83,6 +83,9 @@ def current_cassette
# @option options :serialize_with [Symbol] Which serializer to use.
# Valid values are :yaml, :syck, :psych, :json or any registered
# custom serializer. Defaults to :yaml.
# @option options :preserve_exact_body_bytes [Boolean] Whether or not
# to base64 encode the bytes of the requests and responses for this cassette
# when serializing it. See also `VCR::Configuration#preserve_exact_body_bytes`.
#
# @return [VCR::Cassette] the inserted cassette
#
Expand Down
3 changes: 2 additions & 1 deletion lib/vcr/cassette.rb
Expand Up @@ -50,7 +50,7 @@ def initialize(name, options = {})
invalid_options = options.keys - [
:record, :erb, :match_requests_on, :re_record_interval, :tag, :tags,
:update_content_length_header, :allow_playback_repeats, :exclusive,
:serialize_with
:serialize_with, :preserve_exact_body_bytes
]

if invalid_options.size > 0
Expand All @@ -64,6 +64,7 @@ def initialize(name, options = {})
@re_record_interval = options[:re_record_interval]
@tags = Array(options.fetch(:tags) { options[:tag] })
@tags << :update_content_length_header if options[:update_content_length_header]
@tags << :preserve_exact_body_bytes if options[:preserve_exact_body_bytes]
@allow_playback_repeats = options[:allow_playback_repeats]
@exclusive = options[:exclusive]
@serializer = VCR.cassette_serializers[options[:serialize_with]]
Expand Down
4 changes: 3 additions & 1 deletion lib/vcr/configuration.rb
Expand Up @@ -441,7 +441,9 @@ def register_built_in_hooks
interaction.response.update_content_length_header
end

preserve_exact_body_bytes { false }
preserve_exact_body_bytes do |http_message, cassette|
cassette && cassette.tags.include?(:preserve_exact_body_bytes)
end
end
end

Expand Down
16 changes: 14 additions & 2 deletions spec/vcr/configuration_spec.rb
Expand Up @@ -212,8 +212,20 @@ def message_for(body)
stub(:body => body)
end

it "returns false by default" do
subject.preserve_exact_body_bytes_for?(message_for "string").should be_false
context "default hook" do
it "returns false when there is no current cassette" do
subject.preserve_exact_body_bytes_for?(message_for "string").should be_false
end

it "returns false when the current cassette has been created without the :preserve_exact_body_bytes option" do
VCR.insert_cassette('foo')
subject.preserve_exact_body_bytes_for?(message_for "string").should be_false
end

it 'returns true when the current cassette has been created with the :preserve_exact_body_bytes option' do
VCR.insert_cassette('foo', :preserve_exact_body_bytes => true)
subject.preserve_exact_body_bytes_for?(message_for "string").should be_true
end
end

it "returns true when the configured block returns true" do
Expand Down

0 comments on commit befc480

Please sign in to comment.