Skip to content

Commit 99b53a3

Browse files
committed
Add vcr options support
1 parent b617bfb commit 99b53a3

File tree

7 files changed

+46
-19
lines changed

7 files changed

+46
-19
lines changed

README.md

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -388,13 +388,13 @@ It can be used in two modes:
388388

389389
#### basic setup
390390

391-
Add your VCR configuration to your `cypress_helper.rb`
391+
Add your VCR configuration to your `config/cypress_on_rails.rb`
392392

393393
```ruby
394-
require 'vcr'
395-
VCR.configure do |config|
396-
config.hook_into :webmock
397-
end
394+
c.vcr_options = {
395+
hook_into: :webmock,
396+
default_cassette_options: { record: :once },
397+
}
398398
```
399399

400400
Add to your `cypress/support/index.js`:
@@ -459,7 +459,10 @@ Add to your `config/cypress_on_rails.rb`:
459459
Adjust record mode in `config/cypress_on_rails.rb` if needed:
460460

461461
```ruby
462-
c.vcr_record_mode = :once # Use to choose VCR record mode
462+
c.vcr_options = {
463+
hook_into: :webmock,
464+
default_cassette_options: { record: :once },
465+
}
463466
```
464467

465468
Add to your `cypress/support/command.js`:

lib/cypress_on_rails/configuration.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ class Configuration
99
attr_accessor :use_vcr_use_cassette_middleware
1010
attr_accessor :before_request
1111
attr_accessor :logger
12-
attr_accessor :vcr_record_mode
12+
attr_accessor :vcr_options
1313

1414
# Attributes for backwards compatibility
1515
def cypress_folder
@@ -37,7 +37,7 @@ def reset
3737
self.use_vcr_use_cassette_middleware = false
3838
self.before_request = -> (request) {}
3939
self.logger = Logger.new(STDOUT)
40-
self.vcr_record_mode = :new_episodes
40+
self.vcr_options = {}
4141
end
4242

4343
def tagged_logged

lib/cypress_on_rails/vcr/middleware_helpers.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,9 +20,30 @@ def configure_vcr
2020
require 'vcr'
2121
VCR.configure do |config|
2222
config.cassette_library_dir = cassette_library_dir
23+
apply_vcr_options(config) if configuration.vcr_options.present?
2324
end
2425
VCR
2526
end
27+
28+
def apply_vcr_options(config)
29+
configuration.vcr_options.each do |option, value|
30+
next if option.to_sym == :cassette_library_dir
31+
32+
apply_vcr_option(config, option, value)
33+
end
34+
end
35+
36+
def apply_vcr_option(config, option, value)
37+
return unless config.respond_to?(option) || config.respond_to?("#{option}=")
38+
39+
if config.respond_to?("#{option}=")
40+
config.send("#{option}=", value)
41+
elsif value.is_a?(Array)
42+
config.send(option, *value)
43+
else
44+
config.send(option, value)
45+
end
46+
end
2647
end
2748
end
2849
end

lib/cypress_on_rails/vcr/use_cassette_middleware.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ def initialize_vcr
3838
def handle_request_with_vcr(env)
3939
request = Rack::Request.new(env)
4040
cassette_name = fetch_request_cassette(request)
41-
vcr.use_cassette(cassette_name, { record: configuration.vcr_record_mode }) do
41+
vcr.use_cassette(cassette_name) do
4242
logger.info "Handle request with cassette name: #{cassette_name}"
4343
@app.call(env)
4444
end

lib/generators/cypress_on_rails/templates/config/initializers/cypress_on_rails.rb.erb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,11 @@ if defined?(CypressOnRails)
88
<% unless options.experimental %># <% end %> c.use_vcr_middleware = !Rails.env.production?
99
# Use this if you want to use use_cassette wrapper instead of manual insert/eject
1010
# c.use_vcr_use_cassette_middleware = !Rails.env.production?
11-
# c.vcr_record_mode = :once # Use to choose VCR record mode
11+
# Pass custom VCR options
12+
# c.vcr_options = {
13+
# hook_into: :webmock,
14+
# default_cassette_options: { record: :once },
15+
# }
1216
c.logger = Rails.logger
1317

1418
# If you want to enable a before_request logic, such as authentication, logging, sending metrics, etc.

spec/cypress_on_rails/configuration_spec.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,22 +9,25 @@
99
expect(CypressOnRails.configuration.use_middleware?).to eq(true)
1010
expect(CypressOnRails.configuration.logger).to_not be_nil
1111
expect(CypressOnRails.configuration.before_request).to_not be_nil
12+
expect(CypressOnRails.configuration.vcr_options).to eq({})
1213
end
1314

1415
it 'can be configured' do
1516
my_logger = Logger.new(STDOUT)
16-
before_request_lambda = -> (_) { return [200, {}, ['hello world']] }
17+
before_request_lambda = ->(_) { return [200, {}, ['hello world']] }
1718
CypressOnRails.configure do |config|
1819
config.api_prefix = '/api'
1920
config.install_folder = 'my/path'
2021
config.use_middleware = false
2122
config.logger = my_logger
2223
config.before_request = before_request_lambda
24+
config.vcr_options = { hook_into: :webmock }
2325
end
2426
expect(CypressOnRails.configuration.api_prefix).to eq('/api')
2527
expect(CypressOnRails.configuration.install_folder).to eq('my/path')
2628
expect(CypressOnRails.configuration.use_middleware?).to eq(false)
2729
expect(CypressOnRails.configuration.logger).to eq(my_logger)
2830
expect(CypressOnRails.configuration.before_request).to eq(before_request_lambda)
31+
expect(CypressOnRails.configuration.vcr_options).to eq(hook_into: :webmock)
2932
end
3033
end

spec/cypress_on_rails/vcr/use_cassette_middleware_spec.rb

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -26,18 +26,15 @@ def rack_input(json_value)
2626
env['QUERY_STRING'] = 'operation=test'
2727

2828
expect(response).to eq([200, {}, ['app did /graphql']])
29-
expect(vcr).to have_received(:use_cassette)
30-
.with('/graphql/test', hash_including(record: :new_episodes))
29+
expect(vcr).to have_received(:use_cassette).with('/graphql/test')
3130
end
3231

3332
it 'returns the application response using default request path cassette' do
34-
allow(CypressOnRails).to receive(:configuration).and_return(double(vcr_record_mode: :once,
35-
logger: Logger.new(nil)))
33+
allow(CypressOnRails).to receive(:configuration).and_return(double(logger: Logger.new(nil)))
3634
env['PATH_INFO'] = '/test/path'
3735

3836
expect(response).to eq([200, {}, ['app did /test/path']])
39-
expect(vcr).to have_received(:use_cassette)
40-
.with('/test/path', hash_including(record: :once))
37+
expect(vcr).to have_received(:use_cassette).with('/test/path')
4138
end
4239

4340
context 'when VCR cassette library directory does not match' do
@@ -63,8 +60,7 @@ def rack_input(json_value)
6360
env['QUERY_STRING'] = 'operation=test'
6461

6562
expect(response).to eq([200, {}, ['app did /graphql']])
66-
expect(vcr).to have_received(:use_cassette)
67-
.with('/graphql/test', hash_including(record: :new_episodes))
63+
expect(vcr).to have_received(:use_cassette).with('/graphql/test')
6864
end
6965
end
7066
end

0 commit comments

Comments
 (0)