vcr_better_binary 0.1.0

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA256:
3
+ metadata.gz: 99824329525ac6e2443b2fdd9312b518fd59fd8c64d69ffc707675706dcde35a
4
+ data.tar.gz: b12e5a52879cf7d2080289417d93636e408eb407c20e8f39112c630d8840df3a
5
+ SHA512:
6
+ metadata.gz: 8d8de28648b2eb946c478de83baf90ee005f56b5c179e2633a4ce8dea11a1765f524cca27a191f9b28bd65b46f352f865ca60c8cf20b60ecfdb4811164ed4339
7
+ data.tar.gz: c981ae81c955320beeecb6afac34b80ee1eddd0df146135175d558b3ce99676b874310501ab923a9182fc5356da196b7c3f998d76cbe84bd2051665c0ed39e0d
@@ -0,0 +1,68 @@
1
+ ---
2
+ version: 2
3
+
4
+ #-------------------------------------------------------------------------------
5
+
6
+ base_job: &base_job
7
+ working_directory: ~/repo
8
+ steps:
9
+ - checkout
10
+
11
+ - restore_cache:
12
+ keys:
13
+ - v1-dependencies-{{ checksum "Gemfile.lock" }}
14
+ # fallback to using the latest cache if no exact match is found
15
+ - v1-dependencies
16
+
17
+ - run:
18
+ name: install dependencies
19
+ command: |
20
+ gem install bundler --version "~> 2.0"
21
+ bundle config set path 'vendor/bundle'
22
+ bundle install --jobs=4 --retry=3
23
+
24
+ - save_cache:
25
+ paths:
26
+ - ./vendor/bundle
27
+ key: v1-dependencies-{{ checksum "Gemfile.lock" }}
28
+
29
+ - run:
30
+ name: run tests
31
+ command: |
32
+ mkdir /tmp/test-results
33
+ bundle exec rspec --format progress \
34
+ --format RspecJunitFormatter \
35
+ --out /tmp/test-results/rspec.xml
36
+
37
+ # collect reports
38
+ - store_test_results:
39
+ path: /tmp/test-results
40
+ - store_artifacts:
41
+ path: /tmp/test-results
42
+ destination: test-results
43
+
44
+ #-------------------------------------------------------------------------------
45
+
46
+ jobs:
47
+ ruby-2.5:
48
+ <<: *base_job
49
+ docker:
50
+ - image: circleci/ruby:2.5
51
+ ruby-2.6:
52
+ <<: *base_job
53
+ docker:
54
+ - image: circleci/ruby:2.6
55
+ ruby-2.7:
56
+ <<: *base_job
57
+ docker:
58
+ - image: circleci/ruby:2.7
59
+
60
+ #-------------------------------------------------------------------------------
61
+
62
+ workflows:
63
+ version: 2
64
+ multiple-rubies:
65
+ jobs:
66
+ - ruby-2.5
67
+ - ruby-2.6
68
+ - ruby-2.7
@@ -0,0 +1,15 @@
1
+ /.bundle/
2
+ /.yardoc
3
+ /_yardoc/
4
+ /coverage/
5
+ /doc/
6
+ /pkg/
7
+ /spec/reports/
8
+ /tmp/
9
+
10
+ .ruby_version
11
+
12
+ # rspec state
13
+ spec/examples.txt
14
+
15
+ coverage
data/.rspec ADDED
@@ -0,0 +1,2 @@
1
+ --require spec_helper
2
+ --color
@@ -0,0 +1 @@
1
+ 2.7.0
data/Gemfile ADDED
@@ -0,0 +1,3 @@
1
+ source "https://rubygems.org"
2
+
3
+ gemspec
@@ -0,0 +1,67 @@
1
+ PATH
2
+ remote: .
3
+ specs:
4
+ vcr_better_binary (0.1.0)
5
+ vcr (~> 5.0)
6
+
7
+ GEM
8
+ remote: https://rubygems.org/
9
+ specs:
10
+ addressable (2.7.0)
11
+ public_suffix (>= 2.0.2, < 5.0)
12
+ coderay (1.1.2)
13
+ crack (0.4.3)
14
+ safe_yaml (~> 1.0.0)
15
+ diff-lcs (1.3)
16
+ docile (1.3.1)
17
+ hashdiff (1.0.1)
18
+ jet_black (0.6.0)
19
+ json (2.3.0)
20
+ method_source (1.0.0)
21
+ pry (0.13.1)
22
+ coderay (~> 1.1)
23
+ method_source (~> 1.0)
24
+ public_suffix (4.0.5)
25
+ rake (13.0.1)
26
+ rspec (3.9.0)
27
+ rspec-core (~> 3.9.0)
28
+ rspec-expectations (~> 3.9.0)
29
+ rspec-mocks (~> 3.9.0)
30
+ rspec-core (3.9.2)
31
+ rspec-support (~> 3.9.3)
32
+ rspec-expectations (3.9.2)
33
+ diff-lcs (>= 1.2.0, < 2.0)
34
+ rspec-support (~> 3.9.0)
35
+ rspec-mocks (3.9.1)
36
+ diff-lcs (>= 1.2.0, < 2.0)
37
+ rspec-support (~> 3.9.0)
38
+ rspec-support (3.9.3)
39
+ rspec_junit_formatter (0.4.1)
40
+ rspec-core (>= 2, < 4, != 2.12.0)
41
+ safe_yaml (1.0.5)
42
+ simplecov (0.16.1)
43
+ docile (~> 1.1)
44
+ json (>= 1.8, < 3)
45
+ simplecov-html (~> 0.10.0)
46
+ simplecov-html (0.10.2)
47
+ vcr (5.1.0)
48
+ webmock (3.8.3)
49
+ addressable (>= 2.3.6)
50
+ crack (>= 0.3.2)
51
+ hashdiff (>= 0.4.0, < 2.0.0)
52
+
53
+ PLATFORMS
54
+ ruby
55
+
56
+ DEPENDENCIES
57
+ jet_black
58
+ pry
59
+ rake
60
+ rspec
61
+ rspec_junit_formatter
62
+ simplecov
63
+ vcr_better_binary!
64
+ webmock
65
+
66
+ BUNDLED WITH
67
+ 2.1.4
@@ -0,0 +1,21 @@
1
+ The MIT License (MIT)
2
+
3
+ Copyright (c) 2020 Oliver Peate
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in
13
+ all copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
21
+ THE SOFTWARE.
@@ -0,0 +1,90 @@
1
+ # VCR: Better Binary Serializer
2
+
3
+ [![CircleCI](https://circleci.com/gh/odlp/vcr_better_binary.svg?style=shield)](https://circleci.com/gh/odlp/vcr_better_binary)
4
+
5
+ ## What
6
+
7
+ This gem is a [VCR] serializer which persists any binary data in the HTTP
8
+ request or response bodies outside the cassette.
9
+
10
+ [VCR]: https://github.com/vcr/vcr
11
+
12
+ ## Why
13
+
14
+ - Keeps the cassettes human readable
15
+ - You can `git diff` the cassette more easily (and git won't diff the binary
16
+ data stored elsewhere)
17
+ - Github won't collapse the diffs for cassettes in PRs (the default for large files)
18
+
19
+ ## How
20
+
21
+ Add the gem to your `Gemfile` and `bundle install`:
22
+
23
+ ```ruby
24
+ group :test do
25
+ gem "vcr_better_binary"
26
+ end
27
+ ```
28
+
29
+ Configure VCR to use the serializer:
30
+
31
+ ```ruby
32
+ # spec/support/vcr.rb (or wherever you configure VCR)
33
+
34
+ VCR.configure do |config|
35
+ config.cassette_serializers[:better_binary] = VcrBetterBinary::Serializer.new
36
+ config.default_cassette_options = { serialize_with: :better_binary } # or specify inline in 'VCR.use_cassette'
37
+ end
38
+ ```
39
+
40
+ When you re-record or delete a cassette there may be stale references leftover
41
+ in the storage directory.
42
+
43
+ Add a hook after all your tests have run to prevent unused data from building
44
+ up:
45
+
46
+ ```ruby
47
+ RSpec.configure do |config|
48
+ config.after(:suite) do
49
+ VcrBetterBinary::Serializer.new.prune_bin_data
50
+ end
51
+ end
52
+ ```
53
+
54
+ And you're set!
55
+
56
+ ## The end-result
57
+
58
+ When you record requests with binary data the resulting cassette will
59
+ look similar to this:
60
+
61
+ ```yaml
62
+ # Abridged example
63
+ http_interactions:
64
+ - request:
65
+ method: post
66
+ uri: https://example.com/upload
67
+ body:
68
+ encoding: ASCII-8BIT
69
+ bin_key: lymom-vudim-vunek-mobad-fepak-taset-zosyl-zuhaf-setag
70
+ response:
71
+ status:
72
+ code: 200
73
+ message: OK
74
+ body:
75
+ encoding: ASCII-8BIT
76
+ bin_key: xohog-badok-paneg-memek-tahum-degab-kasip-pefik-colol
77
+ ```
78
+
79
+ And the following files will have been persisted:
80
+
81
+ ```
82
+ spec/fixtures/vcr_cassettes
83
+ ├── my-cassette.yml
84
+ └── bin_data
85
+ ├── lymom-vudim-vunek-mobad-fepak-taset-zosyl-zuhaf-setag
86
+ └── xohog-badok-paneg-memek-tahum-degab-kasip-pefik-colol
87
+ ```
88
+
89
+ All remaining VCR functionality will operate as normal; the only adjustment is
90
+ the storage of binary data.
@@ -0,0 +1,6 @@
1
+ require "bundler/gem_tasks"
2
+ require "rspec/core/rake_task"
3
+
4
+ RSpec::Core::RakeTask.new(:spec)
5
+
6
+ task :default => :spec
@@ -0,0 +1,5 @@
1
+ require "vcr_better_binary/serializer"
2
+ require "vcr_better_binary/version"
3
+
4
+ module VcrBetterBinary
5
+ end
@@ -0,0 +1,41 @@
1
+ require "vcr"
2
+
3
+ module VcrBetterBinary
4
+ class Pruner
5
+ def prune_bin_data(bin_data_dir:, cassette_http_bodies:)
6
+ if in_git_repo? && no_cassette_changes?
7
+ return
8
+ end
9
+
10
+ in_use_keys = find_in_use_keys(cassette_http_bodies)
11
+
12
+ Dir.glob(File.expand_path("*", bin_data_dir)).each do |bin_file|
13
+ unless in_use_keys.include?(File.basename(bin_file))
14
+ File.delete(bin_file)
15
+ end
16
+ end
17
+ end
18
+
19
+ private
20
+
21
+ def find_in_use_keys(cassette_http_bodies)
22
+ cassette_http_bodies.each_with_object(Set.new) do |http_body, in_use_keys|
23
+ if http_body.key?(Serializer::BIN_KEY)
24
+ in_use_keys << http_body[Serializer::BIN_KEY]
25
+ end
26
+ end
27
+ end
28
+
29
+ def in_git_repo?
30
+ system("git -C '#{cassette_dir}' rev-parse --is-inside-work-tree > /dev/null 2>&1")
31
+ end
32
+
33
+ def no_cassette_changes?
34
+ `git -C '#{cassette_dir}' status --porcelain -- '#{cassette_dir}'`.empty?
35
+ end
36
+
37
+ def cassette_dir
38
+ VCR.configuration.cassette_library_dir
39
+ end
40
+ end
41
+ end
@@ -0,0 +1,115 @@
1
+ require "digest/bubblebabble"
2
+ require "fileutils"
3
+ require "vcr_better_binary/pruner"
4
+ require "vcr/cassette/serializers"
5
+
6
+ module VcrBetterBinary
7
+ class Serializer
8
+ BIN_KEY = "bin_key"
9
+
10
+ def initialize(base_serializer: VCR::Cassette::Serializers::YAML)
11
+ @base_serializer = base_serializer
12
+ end
13
+
14
+ def file_extension
15
+ base_serializer.file_extension
16
+ end
17
+
18
+ def serialize(data)
19
+ yield_http_bodies(data) do |body|
20
+ stash_binary_body_data(body)
21
+ end
22
+
23
+ base_serializer.serialize(data)
24
+ end
25
+
26
+ def deserialize(string)
27
+ data = base_serializer.deserialize(string)
28
+
29
+ yield_http_bodies(data) do |body|
30
+ restore_binary_body_data(body)
31
+ end
32
+
33
+ data
34
+ end
35
+
36
+ def prune_bin_data
37
+ Pruner.new.prune_bin_data(
38
+ bin_data_dir: bin_data_dir,
39
+ cassette_http_bodies: all_cassette_http_bodies
40
+ )
41
+ end
42
+
43
+ private
44
+
45
+ attr_reader :base_serializer
46
+
47
+ def yield_http_bodies(data)
48
+ data.fetch("http_interactions").each do |interaction|
49
+ request_body = interaction.dig("request", "body")
50
+ yield(request_body) unless request_body.nil?
51
+
52
+ response_body = interaction.dig("response", "body")
53
+ yield(response_body) unless response_body.nil?
54
+ end
55
+ end
56
+
57
+ def stash_binary_body_data(body)
58
+ return unless body["encoding"] == Encoding::BINARY.name
59
+ return if body["string"].nil? || body["string"].empty?
60
+
61
+ bin_key = filename_safe_digest(body["string"])
62
+ bin_storage_path = File.expand_path(bin_key, bin_data_dir)
63
+
64
+ write_binary_data(bin_storage_path, body["string"])
65
+ body[BIN_KEY] = bin_key
66
+ body.delete("string")
67
+
68
+ bin_key
69
+ end
70
+
71
+ def restore_binary_body_data(body)
72
+ return unless body.key?(BIN_KEY)
73
+
74
+ bin_storage_path = File.expand_path(body[BIN_KEY], bin_data_dir)
75
+ body["string"] = read_binary_data(bin_storage_path)
76
+ body.delete(BIN_KEY)
77
+ end
78
+
79
+ def bin_data_dir
80
+ File.expand_path("bin_data", cassette_dir).tap do |dir|
81
+ FileUtils.mkdir_p(dir)
82
+ end
83
+ end
84
+
85
+ def filename_safe_digest(content)
86
+ Digest::SHA256.bubblebabble(content)
87
+ end
88
+
89
+ def cassette_dir
90
+ VCR.configuration.cassette_library_dir
91
+ end
92
+
93
+ def read_binary_data(path)
94
+ File.open(path, "rb") { |file| file.read }
95
+ end
96
+
97
+ def write_binary_data(path, data)
98
+ File.open(path, "wb") { |file| file.write(data) }
99
+ end
100
+
101
+ def all_cassette_http_bodies
102
+ Enumerator::Lazy.new(cassette_paths) do |yielder, cassette_path|
103
+ data = base_serializer.deserialize(File.read(cassette_path))
104
+
105
+ yield_http_bodies(data) do |body|
106
+ yielder << body
107
+ end
108
+ end
109
+ end
110
+
111
+ def cassette_paths
112
+ Dir.glob(File.expand_path("*.#{file_extension}", cassette_dir))
113
+ end
114
+ end
115
+ end
@@ -0,0 +1,3 @@
1
+ module VcrBetterBinary
2
+ VERSION = "0.1.0"
3
+ end
@@ -0,0 +1,32 @@
1
+ require_relative "lib/vcr_better_binary/version"
2
+
3
+ Gem::Specification.new do |spec|
4
+ spec.name = "vcr_better_binary"
5
+ spec.version = VcrBetterBinary::VERSION
6
+ spec.authors = ["Oliver Peate"]
7
+
8
+ spec.summary = "Persist binary data outside your VCR cassettes"
9
+ spec.homepage = "https://github.com/odlp/vcr_better_binary"
10
+ spec.license = "MIT"
11
+ spec.required_ruby_version = Gem::Requirement.new(">= 2.5.0")
12
+
13
+ spec.metadata["homepage_uri"] = spec.homepage
14
+ # spec.metadata["source_code_uri"] = "TODO: Put your gem's public repo URL here."
15
+ # spec.metadata["changelog_uri"] = "TODO: Put your gem's CHANGELOG.md URL here."
16
+
17
+ spec.files = Dir.chdir(__dir__) do
18
+ `git ls-files -z`.split("\x0").reject { |f| f.match(%r{^(test|spec|features)/}) }
19
+ end
20
+
21
+ spec.require_paths = ["lib"]
22
+
23
+ spec.add_dependency "vcr", "~> 5.0"
24
+
25
+ spec.add_development_dependency "pry"
26
+ spec.add_development_dependency "rake"
27
+ spec.add_development_dependency "rspec"
28
+ spec.add_development_dependency "webmock"
29
+ spec.add_development_dependency "simplecov"
30
+ spec.add_development_dependency "jet_black"
31
+ spec.add_development_dependency "rspec_junit_formatter"
32
+ end
metadata ADDED
@@ -0,0 +1,169 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: vcr_better_binary
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.1.0
5
+ platform: ruby
6
+ authors:
7
+ - Oliver Peate
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2020-05-25 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: vcr
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5.0'
27
+ - !ruby/object:Gem::Dependency
28
+ name: pry
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - ">="
32
+ - !ruby/object:Gem::Version
33
+ version: '0'
34
+ type: :development
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - ">="
39
+ - !ruby/object:Gem::Version
40
+ version: '0'
41
+ - !ruby/object:Gem::Dependency
42
+ name: rake
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - ">="
46
+ - !ruby/object:Gem::Version
47
+ version: '0'
48
+ type: :development
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - ">="
53
+ - !ruby/object:Gem::Version
54
+ version: '0'
55
+ - !ruby/object:Gem::Dependency
56
+ name: rspec
57
+ requirement: !ruby/object:Gem::Requirement
58
+ requirements:
59
+ - - ">="
60
+ - !ruby/object:Gem::Version
61
+ version: '0'
62
+ type: :development
63
+ prerelease: false
64
+ version_requirements: !ruby/object:Gem::Requirement
65
+ requirements:
66
+ - - ">="
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ - !ruby/object:Gem::Dependency
70
+ name: webmock
71
+ requirement: !ruby/object:Gem::Requirement
72
+ requirements:
73
+ - - ">="
74
+ - !ruby/object:Gem::Version
75
+ version: '0'
76
+ type: :development
77
+ prerelease: false
78
+ version_requirements: !ruby/object:Gem::Requirement
79
+ requirements:
80
+ - - ">="
81
+ - !ruby/object:Gem::Version
82
+ version: '0'
83
+ - !ruby/object:Gem::Dependency
84
+ name: simplecov
85
+ requirement: !ruby/object:Gem::Requirement
86
+ requirements:
87
+ - - ">="
88
+ - !ruby/object:Gem::Version
89
+ version: '0'
90
+ type: :development
91
+ prerelease: false
92
+ version_requirements: !ruby/object:Gem::Requirement
93
+ requirements:
94
+ - - ">="
95
+ - !ruby/object:Gem::Version
96
+ version: '0'
97
+ - !ruby/object:Gem::Dependency
98
+ name: jet_black
99
+ requirement: !ruby/object:Gem::Requirement
100
+ requirements:
101
+ - - ">="
102
+ - !ruby/object:Gem::Version
103
+ version: '0'
104
+ type: :development
105
+ prerelease: false
106
+ version_requirements: !ruby/object:Gem::Requirement
107
+ requirements:
108
+ - - ">="
109
+ - !ruby/object:Gem::Version
110
+ version: '0'
111
+ - !ruby/object:Gem::Dependency
112
+ name: rspec_junit_formatter
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - ">="
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - ">="
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
125
+ description:
126
+ email:
127
+ executables: []
128
+ extensions: []
129
+ extra_rdoc_files: []
130
+ files:
131
+ - ".circleci/config.yml"
132
+ - ".gitignore"
133
+ - ".rspec"
134
+ - ".ruby-version"
135
+ - Gemfile
136
+ - Gemfile.lock
137
+ - LICENSE.txt
138
+ - README.md
139
+ - Rakefile
140
+ - lib/vcr_better_binary.rb
141
+ - lib/vcr_better_binary/pruner.rb
142
+ - lib/vcr_better_binary/serializer.rb
143
+ - lib/vcr_better_binary/version.rb
144
+ - vcr_better_binary.gemspec
145
+ homepage: https://github.com/odlp/vcr_better_binary
146
+ licenses:
147
+ - MIT
148
+ metadata:
149
+ homepage_uri: https://github.com/odlp/vcr_better_binary
150
+ post_install_message:
151
+ rdoc_options: []
152
+ require_paths:
153
+ - lib
154
+ required_ruby_version: !ruby/object:Gem::Requirement
155
+ requirements:
156
+ - - ">="
157
+ - !ruby/object:Gem::Version
158
+ version: 2.5.0
159
+ required_rubygems_version: !ruby/object:Gem::Requirement
160
+ requirements:
161
+ - - ">="
162
+ - !ruby/object:Gem::Version
163
+ version: '0'
164
+ requirements: []
165
+ rubygems_version: 3.1.2
166
+ signing_key:
167
+ specification_version: 4
168
+ summary: Persist binary data outside your VCR cassettes
169
+ test_files: []