-
-
Notifications
You must be signed in to change notification settings - Fork 60
VCR use_cassette middleware #167
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MUTOgen
wants to merge
22
commits into
master
Choose a base branch
from
new-vcr-middleware
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
3aab613
Add VCR cassette wrapper
MUTOgen 5267075
Add VCR wrapper
MUTOgen 0cfdc73
Update VCR wrapper
MUTOgen bf7959c
Add VCR wrapper require call
MUTOgen d033476
Refactor use_cassette middleware
MUTOgen 966f517
Move logger
MUTOgen 4b86fc3
Typo in path
MUTOgen ec48a89
Remove unused methods
MUTOgen e115b3a
Cleanup comments
MUTOgen 1d29664
Update VCR logic
MUTOgen 634315c
Changes cleanup
MUTOgen 928485e
Update readme
MUTOgen d78c5c6
Update usage readme
MUTOgen 1317fda
Update middlewares
MUTOgen a522721
Update rescue logic
MUTOgen 97986ba
Update init method
MUTOgen 1bf9143
remove init
MUTOgen 719db64
Add vcr options support
MUTOgen 3ebbd6e
Update rails 5.2 setup
MUTOgen 63bdcc8
Revert vcr init before run
MUTOgen 46e4d91
Add configurable cassette_library_dir
MUTOgen add5a9d
Reset changes for CI
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
require_relative 'middleware_helpers' | ||
|
||
module CypressOnRails | ||
module Vcr | ||
# Middleware to handle vcr with insert/eject endpoints | ||
class InsertEjectMiddleware | ||
include MiddlewareHelpers | ||
|
||
def initialize(app, vcr = nil) | ||
@app = app | ||
@vcr = vcr | ||
@first_call = false | ||
end | ||
|
||
def call(env) | ||
request = Rack::Request.new(env) | ||
if request.path.start_with?('/__e2e__/vcr/insert') | ||
configuration.tagged_logged { handle_insert(request) } | ||
elsif request.path.start_with?('/__e2e__/vcr/eject') | ||
configuration.tagged_logged { handle_eject } | ||
MUTOgen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
else | ||
do_first_call unless @first_call | ||
@app.call(env) | ||
end | ||
end | ||
MUTOgen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
private | ||
|
||
def handle_insert(req) | ||
WebMock.enable! if defined?(WebMock) | ||
vcr.turn_on! | ||
body = parse_request_body(req) | ||
logger.info "vcr insert cassette: #{body}" | ||
cassette_name, options = extract_cassette_info(body) | ||
vcr.insert_cassette(cassette_name, options) | ||
[201, { 'Content-Type' => 'application/json' }, [{ 'message': 'OK' }.to_json]] | ||
rescue JSON::ParserError => e | ||
[400, { 'Content-Type' => 'application/json' }, [{ 'message': e.message }.to_json]] | ||
rescue LoadError, ArgumentError => e | ||
[500, { 'Content-Type' => 'application/json' }, [{ 'message': e.message }.to_json]] | ||
end | ||
|
||
def parse_request_body(req) | ||
JSON.parse(req.body.read) | ||
end | ||
|
||
def extract_cassette_info(body) | ||
cassette_name = body[0] | ||
options = (body[1] || {}).symbolize_keys | ||
options[:record] = options[:record].to_sym if options[:record] | ||
options[:match_requests_on] = options[:match_requests_on].map(&:to_sym) if options[:match_requests_on] | ||
options[:serialize_with] = options[:serialize_with].to_sym if options[:serialize_with] | ||
options[:persist_with] = options[:persist_with].to_sym if options[:persist_with] | ||
[cassette_name, options] | ||
end | ||
MUTOgen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def handle_eject | ||
logger.info 'vcr eject cassette' | ||
vcr.eject_cassette | ||
do_first_call | ||
[201, { 'Content-Type' => 'application/json' }, [{ 'message': 'OK' }.to_json]] | ||
rescue LoadError, ArgumentError => e | ||
[500, { 'Content-Type' => 'application/json' }, [{ 'message': e.message }.to_json]] | ||
end | ||
|
||
def do_first_call | ||
@first_call = true | ||
vcr.turn_off! | ||
WebMock.disable! if defined?(WebMock) | ||
rescue LoadError | ||
# nop | ||
end | ||
end | ||
end | ||
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
require 'cypress_on_rails/middleware_config' | ||
|
||
module CypressOnRails | ||
module Vcr | ||
# Provides helper methods for VCR middlewares | ||
module MiddlewareHelpers | ||
include MiddlewareConfig | ||
|
||
def vcr | ||
@vcr ||= configure_vcr | ||
end | ||
|
||
def cassette_library_dir | ||
configuration.vcr_options&.fetch(:cassette_library_dir) do | ||
"#{configuration.install_folder}/fixtures/vcr_cassettes" | ||
end | ||
end | ||
|
||
private | ||
|
||
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 | ||
MUTOgen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
require_relative 'middleware_helpers' | ||
|
||
module CypressOnRails | ||
module Vcr | ||
# Middleware to handle vcr with use_cassette | ||
class UseCassetteMiddleware | ||
MUTOgen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
include MiddlewareHelpers | ||
|
||
MUTOgen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
def initialize(app, vcr = nil) | ||
@app = app | ||
@vcr = vcr | ||
end | ||
MUTOgen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
def call(env) | ||
return @app.call(env) if should_not_use_vcr? | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a workaround to skip behavior if VCR is already in use (rspec, for example) |
||
|
||
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? && | ||
MUTOgen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
VCR.configuration.cassette_library_dir != cassette_library_dir | ||
MUTOgen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
def initialize_vcr | ||
WebMock.enable! if defined?(WebMock) | ||
vcr.turn_on! | ||
MUTOgen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
end | ||
|
||
def handle_request_with_vcr(env) | ||
request = Rack::Request.new(env) | ||
cassette_name = fetch_request_cassette(request) | ||
vcr.use_cassette(cassette_name) do | ||
logger.info "Handle request with cassette name: #{cassette_name}" | ||
MUTOgen marked this conversation as resolved.
Show resolved
Hide resolved
|
||
@app.call(env) | ||
end | ||
end | ||
|
||
def fetch_request_cassette(request) | ||
if request.path.start_with?('/graphql') && request.params.key?('operation') | ||
"#{request.path}/#{request.params['operation']}" | ||
else | ||
request.path | ||
end | ||
end | ||
end | ||
end | ||
end |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.