Skip to content

Commit

Permalink
Add vcr options support
Browse files Browse the repository at this point in the history
  • Loading branch information
MUTOgen committed Dec 14, 2024
1 parent b617bfb commit 99b53a3
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 19 deletions.
15 changes: 9 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -388,13 +388,13 @@ It can be used in two modes:

#### basic setup

Add your VCR configuration to your `cypress_helper.rb`
Add your VCR configuration to your `config/cypress_on_rails.rb`

```ruby
require 'vcr'
VCR.configure do |config|
config.hook_into :webmock
end
c.vcr_options = {
hook_into: :webmock,
default_cassette_options: { record: :once },
}
```

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

```ruby
c.vcr_record_mode = :once # Use to choose VCR record mode
c.vcr_options = {
hook_into: :webmock,
default_cassette_options: { record: :once },
}
```

Add to your `cypress/support/command.js`:
Expand Down
4 changes: 2 additions & 2 deletions lib/cypress_on_rails/configuration.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class Configuration
attr_accessor :use_vcr_use_cassette_middleware
attr_accessor :before_request
attr_accessor :logger
attr_accessor :vcr_record_mode
attr_accessor :vcr_options

# Attributes for backwards compatibility
def cypress_folder
Expand Down Expand Up @@ -37,7 +37,7 @@ def reset
self.use_vcr_use_cassette_middleware = false
self.before_request = -> (request) {}
self.logger = Logger.new(STDOUT)
self.vcr_record_mode = :new_episodes
self.vcr_options = {}
end

def tagged_logged
Expand Down
21 changes: 21 additions & 0 deletions lib/cypress_on_rails/vcr/middleware_helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,30 @@ def configure_vcr
require 'vcr'
VCR.configure do |config|
config.cassette_library_dir = cassette_library_dir
apply_vcr_options(config) if configuration.vcr_options.present?
end
VCR
end

def apply_vcr_options(config)
configuration.vcr_options.each do |option, value|
next if option.to_sym == :cassette_library_dir

apply_vcr_option(config, option, value)
end
end

def apply_vcr_option(config, option, value)
return unless config.respond_to?(option) || config.respond_to?("#{option}=")

if config.respond_to?("#{option}=")
config.send("#{option}=", value)
elsif value.is_a?(Array)
config.send(option, *value)
else
config.send(option, value)
end
end
end
end
end
2 changes: 1 addition & 1 deletion lib/cypress_on_rails/vcr/use_cassette_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def initialize_vcr
def handle_request_with_vcr(env)
request = Rack::Request.new(env)
cassette_name = fetch_request_cassette(request)
vcr.use_cassette(cassette_name, { record: configuration.vcr_record_mode }) do
vcr.use_cassette(cassette_name) do
logger.info "Handle request with cassette name: #{cassette_name}"
@app.call(env)
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ if defined?(CypressOnRails)
<% unless options.experimental %># <% end %> c.use_vcr_middleware = !Rails.env.production?
# Use this if you want to use use_cassette wrapper instead of manual insert/eject
# c.use_vcr_use_cassette_middleware = !Rails.env.production?
# c.vcr_record_mode = :once # Use to choose VCR record mode
# Pass custom VCR options
# c.vcr_options = {
# hook_into: :webmock,
# default_cassette_options: { record: :once },
# }
c.logger = Rails.logger

# If you want to enable a before_request logic, such as authentication, logging, sending metrics, etc.
Expand Down
5 changes: 4 additions & 1 deletion spec/cypress_on_rails/configuration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,22 +9,25 @@
expect(CypressOnRails.configuration.use_middleware?).to eq(true)
expect(CypressOnRails.configuration.logger).to_not be_nil
expect(CypressOnRails.configuration.before_request).to_not be_nil
expect(CypressOnRails.configuration.vcr_options).to eq({})
end

it 'can be configured' do
my_logger = Logger.new(STDOUT)
before_request_lambda = -> (_) { return [200, {}, ['hello world']] }
before_request_lambda = ->(_) { return [200, {}, ['hello world']] }
CypressOnRails.configure do |config|
config.api_prefix = '/api'
config.install_folder = 'my/path'
config.use_middleware = false
config.logger = my_logger
config.before_request = before_request_lambda
config.vcr_options = { hook_into: :webmock }
end
expect(CypressOnRails.configuration.api_prefix).to eq('/api')
expect(CypressOnRails.configuration.install_folder).to eq('my/path')
expect(CypressOnRails.configuration.use_middleware?).to eq(false)
expect(CypressOnRails.configuration.logger).to eq(my_logger)
expect(CypressOnRails.configuration.before_request).to eq(before_request_lambda)
expect(CypressOnRails.configuration.vcr_options).to eq(hook_into: :webmock)
end
end
12 changes: 4 additions & 8 deletions spec/cypress_on_rails/vcr/use_cassette_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,15 @@ def rack_input(json_value)
env['QUERY_STRING'] = 'operation=test'

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

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

expect(response).to eq([200, {}, ['app did /test/path']])
expect(vcr).to have_received(:use_cassette)
.with('/test/path', hash_including(record: :once))
expect(vcr).to have_received(:use_cassette).with('/test/path')
end

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

expect(response).to eq([200, {}, ['app did /graphql']])
expect(vcr).to have_received(:use_cassette)
.with('/graphql/test', hash_including(record: :new_episodes))
expect(vcr).to have_received(:use_cassette).with('/graphql/test')
end
end
end
Expand Down

0 comments on commit 99b53a3

Please sign in to comment.