Skip to content

Commit

Permalink
Allow multiple preserve_exact_body_bytes hooks.
Browse files Browse the repository at this point in the history
This will pave the way to make it easy to support a cassette option for this feature.
  • Loading branch information
myronmarston committed Feb 21, 2012
1 parent 814e482 commit 33cd528
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
6 changes: 2 additions & 4 deletions lib/vcr/configuration.rb
Expand Up @@ -393,16 +393,14 @@ def configure_rspec_metadata!
# 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.
def preserve_exact_body_bytes(&block)
@preserve_exact_body_bytes_block = block
end
define_hook :preserve_exact_body_bytes

# @return [Boolean] whether or not the body of the given HTTP message should
# be base64 encoded during serialization in order to preserve the bytes exactly.
# @param http_message [#body, #headers] the `VCR::Request` or `VCR::Response` object being serialized
# @see #preserve_exact_body_bytes
def preserve_exact_body_bytes_for?(http_message)
@preserve_exact_body_bytes_block.call(http_message, VCR.current_cassette)
invoke_hook(:preserve_exact_body_bytes, http_message, VCR.current_cassette).any?
end

private
Expand Down
2 changes: 2 additions & 0 deletions spec/vcr/cassette_spec.rb
Expand Up @@ -421,6 +421,8 @@ def stub_old_interactions(interactions)
cassette = VCR::Cassette.new('example', :tag => :foo)
cassette.stub!(:new_recorded_interactions).and_return(interactions)

VCR.configuration.stub(:invoke_hook).and_return([false])

interactions.each do |i|
VCR.configuration.should_receive(:invoke_hook).with(
:before_record,
Expand Down
8 changes: 8 additions & 0 deletions spec/vcr/configuration_spec.rb
Expand Up @@ -222,6 +222,14 @@ def message_for(body)
subject.preserve_exact_body_bytes_for?(message_for "b").should be_false
end

it "returns true when any of the registered blocks returns true" do
called_hooks = []
subject.preserve_exact_body_bytes { called_hooks << :hook_1; false }
subject.preserve_exact_body_bytes { called_hooks << :hook_2; true }
subject.preserve_exact_body_bytes_for?(message_for "a").should be_true
called_hooks.should eq([:hook_1, :hook_2])
end

it "invokes the configured hook with the http message and the current cassette" do
cassette = stub(:cassette)
VCR.should respond_to(:current_cassette)
Expand Down

0 comments on commit 33cd528

Please sign in to comment.