Skip to content

Commit

Permalink
Don't try to encode to a nil encoding.
Browse files Browse the repository at this point in the history
This should allow cassettes recorded on 1.8 to work on 1.9.

Closes #149.
  • Loading branch information
myronmarston committed Mar 19, 2012
1 parent 4a3ed8b commit 45b3fcf
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 1 deletion.
2 changes: 1 addition & 1 deletion lib/vcr/structs.rb
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions spec/vcr/structs_spec.rb
Expand Up @@ -166,6 +166,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)
Expand Down

0 comments on commit 45b3fcf

Please sign in to comment.