-
-
Notifications
You must be signed in to change notification settings - Fork 61
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
12 changed files
with
330 additions
and
253 deletions.
There are no files selected for viewing
This file contains 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 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 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 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,39 @@ | ||
require 'json' | ||
require 'rack' | ||
require 'cypress_on_rails/middleware_config' | ||
|
||
module CypressOnRails | ||
module Vcr | ||
# Base abstract Middleware | ||
class BaseMiddleware | ||
include MiddlewareConfig | ||
|
||
def initialize(_app, _vcr = nil) | ||
raise_not_implemented | ||
end | ||
|
||
def call(_env) | ||
raise_not_implemented | ||
end | ||
|
||
def vcr | ||
@vcr ||= configure_vcr | ||
end | ||
|
||
private | ||
|
||
def configure_vcr | ||
require 'vcr' | ||
VCR.configure do |config| | ||
config.cassette_library_dir = "#{configuration.install_folder}/fixtures/vcr_cassettes" | ||
end | ||
VCR | ||
end | ||
|
||
def raise_not_implemented | ||
raise NotImplementedError, | ||
'BaseMiddleware can not be initialized directly, use InsertEjectMiddleware or UseCassetteMiddleware' | ||
end | ||
end | ||
end | ||
end |
This file contains 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,71 @@ | ||
require_relative 'base_middleware' | ||
|
||
module CypressOnRails | ||
module Vcr | ||
# Middleware to handle vcr with insert/eject endpoints | ||
class InsertEjectMiddleware < BaseMiddleware | ||
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 } | ||
else | ||
do_first_call unless @first_call | ||
@app.call(env) | ||
end | ||
end | ||
|
||
private | ||
|
||
def handle_insert(req) | ||
WebMock.enable! if defined?(WebMock) | ||
vcr.turn_on! | ||
logger.info "vcr insert cassette: #{body}" | ||
body = parse_request_body(req) | ||
cassette_name, options = extract_cassette_info(body) | ||
vcr.insert_cassette(cassette_name, options) | ||
[201, { 'Content-Type' => 'application/json' }, [{ 'message': 'OK' }.to_json]] | ||
rescue LoadError, ArgumentError => e | ||
[501, { '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 | ||
|
||
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 | ||
[501, { '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 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,41 @@ | ||
require 'cypress_on_rails/configuration' | ||
require_relative 'base_middleware' | ||
|
||
module CypressOnRails | ||
module Vcr | ||
# Middleware to handle vcr with use_cassette | ||
class UseCassetteMiddleware < BaseMiddleware | ||
def initialize(app, vcr = nil) | ||
@app = app | ||
@vcr = vcr | ||
end | ||
|
||
def call(env) | ||
request = Rack::Request.new(env) | ||
cassette_name = fetch_request_cassette(request) | ||
vcr.use_cassette(cassette_name, { record: configuration.vcr_use_cassette_mode }) do | ||
logger.info "Handle request with cassette name: #{cassette_name}" | ||
@app.call(env) | ||
end | ||
end | ||
|
||
private | ||
|
||
def configuration | ||
CypressOnRails.configuration | ||
end | ||
|
||
def logger | ||
configuration.logger | ||
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 |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains 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
Oops, something went wrong.