Permalink
Browse files

Just force encode to ASCII-8BIT.

Encoding to ASCII-8BIT is nonsensical since it just means "binary".
  • Loading branch information...
myronmarston committed Mar 23, 2012
1 parent be4ca6c commit 18984676ec06275816c9eb5f5b75628343acd03e
Showing with 19 additions and 0 deletions.
  1. +6 −0 lib/vcr/structs.rb
  2. +13 −0 spec/vcr/structs_spec.rb
View
@@ -33,6 +33,12 @@ def force_encode_string(string, encoding)
def try_encode_string(string, encoding)
return string if encoding.nil? || string.encoding.name == encoding
+
+ # ASCII-8BIT just means binary, so encoding to it is nonsensical
+ # and yet "\u00f6".encode("ASCII-8BIT") raises an error.
+ # Instead, we'll force encode it (essentially just tagging it as binary)
+ return string.force_encoding(encoding) if encoding == "ASCII-8BIT"
+
string.encode(encoding)
rescue EncodingError => e
struct_type = name.split('::').last.downcase
View
@@ -172,6 +172,19 @@ def body_hash(key, value)
i.request.body.encoding.name.should eq("ISO-8859-1")
end
+ it 'force encodes to ASCII-8BIT (since it just means "no encoding" or binary)' do
+ string = "\u00f6"
+ string.encode("UTF-8")
+ string.should be_valid_encoding
+ hash['request']['body'] = { 'string' => string, 'encoding' => 'ASCII-8BIT' }
+
+ Request.should_not_receive(:warn)
+ i = HTTPInteraction.from_hash(hash)
+ i.request.body.should eq(string)
+ i.request.body.bytes.to_a.should eq(string.bytes.to_a)
+ i.request.body.encoding.name.should eq("ASCII-8BIT")
+ end
+
context 'when the string cannot be encoded as the original encoding' do
before do
Request.stub(:warn)

0 comments on commit 1898467

Please sign in to comment.