Permalink
Browse files

Don't try to encode to a nil encoding.

This should allow cassettes recorded on 1.8 to work on 1.9.

Closes #149.
  • Loading branch information...
1 parent 73df359 commit be4ca6c2dbc42400a1f3b8d5c8ff384ff9625cf0 @myronmarston committed Mar 19, 2012
Showing with 11 additions and 1 deletion.
  1. +1 −1 lib/vcr/structs.rb
  2. +10 −0 spec/vcr/structs_spec.rb
View
2 lib/vcr/structs.rb
@@ -32,7 +32,7 @@ def force_encode_string(string, encoding)
end
def try_encode_string(string, encoding)
- return string if string.encoding.name == encoding
+ return string if encoding.nil? || string.encoding.name == encoding
string.encode(encoding)
rescue EncodingError => e
struct_type = name.split('::').last.downcase
View
10 spec/vcr/structs_spec.rb
@@ -162,6 +162,16 @@ def body_hash(key, value)
i.response.body.encoding.name.should eq("ISO-8859-1")
end
+ it 'does not attempt to encode the string when there is no encoding given (i.e. if the cassette was recorded on ruby 1.8)' do
+ string = 'foo'
+ string.force_encoding("ISO-8859-1")
+ hash['request']['body'] = { 'string' => string }
+
+ i = HTTPInteraction.from_hash(hash)
+ i.request.body.should eq('foo')
+ i.request.body.encoding.name.should eq("ISO-8859-1")
+ end
+
context 'when the string cannot be encoded as the original encoding' do
before do
Request.stub(:warn)

0 comments on commit be4ca6c

Please sign in to comment.