Preserve Exact Body Bytes
Some HTTP servers are not well-behaved and respond with invalid data: the response body may not be encoded according to the encoding specified in the HTTP headers, or there may be bytes that are invalid for the given encoding. The YAML and JSON serializers are not generally designed to handle these cases gracefully, and you may get errors when the cassette is serialized or deserialized. Also, the encoding may not be preserved when round-tripped through the serializer.
VCR provides a configuration option to deal with cases like these. The preserve_exact_body_bytes
method accepts a block that VCR will use to determine if the body of the given request or response object should be base64 encoded in order to preserve the bytes exactly as-is. VCR does not do this by default, since base64-encoding the string removes the human readability of the cassette.
Alternately, if you want to force an entire cassette to preserve the exact body bytes, you can pass the :preserve_exact_body_bytes => true
cassette option when inserting your cassette.
Preserve exact bytes for response body with invalid encoding
Given a file named "preserve.rb" with:
When I run ruby preserve.rb
Then the output should contain exactly:
And the file "cassettes/example.json" should contain:
Preserve exact bytes for cassette with :preserve_exact_body_bytes
option
:preserve_exact_body_bytes
optionGiven a file named "preserve.rb" with:
When I run ruby preserve.rb
Then the file "cassettes/preserve_bytes.json" should contain:
And the file "cassettes/dont_preserve_bytes.json" should contain:
Last updated