https://github.com/odlp/vcr_better_binary
Persist binary data outside your VCR cassettes
https://github.com/odlp/vcr_better_binary
vcr vcr-serializer
Last synced: 11 months ago
JSON representation
Persist binary data outside your VCR cassettes
- Host: GitHub
- URL: https://github.com/odlp/vcr_better_binary
- Owner: odlp
- License: mit
- Created: 2020-05-22T18:49:32.000Z (about 6 years ago)
- Default Branch: main
- Last Pushed: 2021-07-13T11:28:27.000Z (almost 5 years ago)
- Last Synced: 2025-06-26T06:18:22.981Z (12 months ago)
- Topics: vcr, vcr-serializer
- Language: Ruby
- Homepage:
- Size: 38.1 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
# VCR: Better Binary Serializer
[](https://circleci.com/gh/odlp/vcr_better_binary)
## What
This gem is a [VCR] serializer which persists any binary data in the HTTP
request or response bodies outside the cassette.
[VCR]: https://github.com/vcr/vcr
## Why
- Keeps the cassettes human readable
- You can `git diff` the cassette more easily (and git won't diff the binary
data stored elsewhere)
- Github won't collapse the diffs for cassettes in PRs (the default for large files)
You can read more about this gem in the following [blog post](https://oliverpeate.com/learning-by-making-small-things/).
## How
Add the gem to your `Gemfile` and `bundle install`:
```ruby
group :test do
gem "vcr_better_binary"
end
```
Configure VCR to use the serializer:
```ruby
# spec/support/vcr.rb (or wherever you configure VCR)
VCR.configure do |config|
config.cassette_serializers[:better_binary] = VcrBetterBinary::Serializer.new
config.default_cassette_options = { serialize_with: :better_binary } # or specify inline in 'VCR.use_cassette'
end
```
When you re-record or delete a cassette there may be stale references leftover
in the storage directory.
Add a hook after all your tests have run to prevent unused data from building
up:
```ruby
RSpec.configure do |config|
config.after(:suite) do
VcrBetterBinary::Serializer.new.prune_bin_data
end
end
```
And you're set!
## The end-result
When you record requests with binary data the resulting cassette will
look similar to this:
```yaml
# Abridged example
http_interactions:
- request:
method: post
uri: https://example.com/upload
body:
encoding: ASCII-8BIT
bin_key: lymom-vudim-vunek-mobad-fepak-taset-zosyl-zuhaf-setag
response:
status:
code: 200
message: OK
body:
encoding: ASCII-8BIT
bin_key: xohog-badok-paneg-memek-tahum-degab-kasip-pefik-colol
```
And the following files will have been persisted:
```
spec/fixtures/vcr_cassettes
├── my-cassette.yml
└── bin_data
├── lymom-vudim-vunek-mobad-fepak-taset-zosyl-zuhaf-setag
└── xohog-badok-paneg-memek-tahum-degab-kasip-pefik-colol
```
All remaining VCR functionality will operate as normal; the only adjustment is
the storage of binary data.
## Advanced configuration
You can adjust the underlying serializer to save your cassettes in different
formats, e.g:
```ruby
# JSON cassettes
VcrBetterBinary::Serializer.new(base_serializer: VCR::Cassette::Serializers::JSON)
```