Skip to content

Commit

Permalink
Update VCR logic
Browse files Browse the repository at this point in the history
  • Loading branch information
MUTOgen committed Aug 18, 2024
1 parent b1f96d7 commit 7cfeae1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 2 deletions.
6 changes: 5 additions & 1 deletion lib/cypress_on_rails/vcr/base_middleware.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,11 +25,15 @@ def vcr
def configure_vcr
require 'vcr'
VCR.configure do |config|
config.cassette_library_dir = "#{configuration.install_folder}/fixtures/vcr_cassettes"
config.cassette_library_dir = cassette_library_dir
end
VCR
end

def cassette_library_dir
"#{configuration.install_folder}/fixtures/vcr_cassettes"
end

def raise_not_implemented
raise NotImplementedError,
'BaseMiddleware can not be initialized directly, use InsertEjectMiddleware or UseCassetteMiddleware'
Expand Down
10 changes: 9 additions & 1 deletion lib/cypress_on_rails/vcr/use_cassette_middleware.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
require 'cypress_on_rails/configuration'
require_relative 'base_middleware'

module CypressOnRails
Expand All @@ -11,6 +10,11 @@ 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

WebMock.enable! if defined?(WebMock)
vcr.turn_on!
request = Rack::Request.new(env)
Expand All @@ -23,6 +27,10 @@ def call(env)

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
28 changes: 28 additions & 0 deletions spec/cypress_on_rails/vcr/use_cassette_middleware_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,34 @@ def rack_input(json_value)
expect(vcr).to have_received(:use_cassette)
.with('/test/path', hash_including(record: :once))
end

context 'when VCR cassette library directory does not match' do
before do
allow(VCR.configuration).to receive(:cassette_library_dir).and_return('/different/path')
end

it 'returns the application response without using VCR' do
env['PATH_INFO'] = '/test/path'

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

context 'when VCR is not defined' do
before do
allow(subject).to receive(:vcr_defined?).and_return(false)
end

it 'returns the application response without error' do
env['PATH_INFO'] = '/graphql'
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))
end
end
end
end
end

0 comments on commit 7cfeae1

Please sign in to comment.