From d924f660fe198605ccf4de93f337f490a33a8131 Mon Sep 17 00:00:00 2001 From: Myron Marston Date: Thu, 3 May 2012 22:53:48 -0700 Subject: [PATCH] Raise an error if a response is recorded with an invalid body. This can occur when a non-standard Faraday stack is used, where a response-modifying middleware comes after the HTTP adapter. For #159. --- lib/vcr/structs.rb | 5 +++++ spec/vcr/structs_spec.rb | 6 ++++++ 2 files changed, 11 insertions(+) diff --git a/lib/vcr/structs.rb b/lib/vcr/structs.rb index dca25589..21b7d1db 100644 --- a/lib/vcr/structs.rb +++ b/lib/vcr/structs.rb @@ -60,6 +60,11 @@ def try_encode_string(string, encoding) def initialize(*args) super + + if body && !body.is_a?(String) + raise ArgumentError, "#{self.class} initialized with an invalid body: #{body.inspect}." + end + # Ensure that the body is a raw string, in case the string instance # has been subclassed or extended with additional instance variables # or attributes, so that it is serialized to YAML as a raw string. diff --git a/spec/vcr/structs_spec.rb b/spec/vcr/structs_spec.rb index 7ff90f73..7eebe22e 100644 --- a/spec/vcr/structs_spec.rb +++ b/spec/vcr/structs_spec.rb @@ -49,6 +49,12 @@ it 'converts nil to a blank string' do instance(nil).body.should eq("") end + + it 'raises an error if given another type of object as the body' do + expect { + instance(:a => "hash") + }.to raise_error(ArgumentError) + end end module VCR