Skip to content

Commit

Permalink
Changes cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
MUTOgen committed Sep 8, 2024
1 parent 7cfeae1 commit 42b941c
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 16 deletions.
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_use_cassette_mode
attr_accessor :vcr_record_mode

# 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_use_cassette_mode = :new_episodes
self.vcr_record_mode = :new_episodes
end

def tagged_logged
Expand Down
33 changes: 22 additions & 11 deletions lib/cypress_on_rails/vcr/use_cassette_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,27 +10,38 @@ def initialize(app, vcr = nil)
end

def call(env)
vcr_initialized = vcr_defined? &&
VCR.configuration.cassette_library_dir.present? &&
VCR.configuration.cassette_library_dir != cassette_library_dir
return @app.call(env) if vcr_initialized
return @app.call(env) if should_not_use_vcr?

initialize_vcr
handle_request_with_vcr(env)
end

private

def vcr_defined?
defined?(VCR) != nil
end

def should_not_use_vcr?
vcr_defined? &&
VCR.configuration.cassette_library_dir.present? &&
VCR.configuration.cassette_library_dir != cassette_library_dir
end

def initialize_vcr
WebMock.enable! if defined?(WebMock)
vcr.turn_on!
end

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_use_cassette_mode }) do
vcr.use_cassette(cassette_name, { record: configuration.vcr_record_mode }) do
logger.info "Handle request with cassette name: #{cassette_name}"
@app.call(env)
end
end

private

def vcr_defined?
defined?(VCR) != nil
end

def fetch_request_cassette(request)
if request.path.start_with?('/graphql') && request.params.key?('operation')
"#{request.path}/#{request.params['operation']}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ 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_use_cassette_mode = :once # Use to choose VCR record mode (:new_episodes by default)
# c.vcr_record_mode = :once # Use to choose VCR record mode
c.logger = Rails.logger

# If you want to enable a before_request logic, such as authentication, logging, sending metrics, etc.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Cypress.Commands.add('appFixtures', function (options) {
// The next is optional
// beforeEach(() => {
// cy.app('clean') // have a look at cypress/app_commands/clean.rb
// cy.mockGraphQL() // for GraphQL usage, see cypress/support/commands.rb
// cy.mockGraphQL() // for GraphQL usage with use_cassette, see cypress/support/commands.rb
// });

// comment this out if you do not want to attempt to log additional info on test fail
Expand Down
2 changes: 1 addition & 1 deletion spec/cypress_on_rails/vcr/use_cassette_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def rack_input(json_value)
end

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

Expand Down

0 comments on commit 42b941c

Please sign in to comment.