Skip to content
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

Add support for on_data with faraday. #823

Merged
merged 1 commit into from
Mar 13, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
20 changes: 19 additions & 1 deletion lib/vcr/middleware/faraday.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,11 @@ def initialize(app, env)
end

def handle
# Faraday must be exlusive here in case another library hook is being used.
# Faraday must be exclusive here in case another library hook is being used.
# We don't want double recording/double playback.
VCR.library_hooks.exclusive_hook = :faraday
collect_chunks if env.request.stream_response?

super
ensure
response = defined?(@vcr_response) ? @vcr_response : nil
Expand Down Expand Up @@ -103,6 +105,7 @@ def on_stubbed_by_vcr_request
@vcr_response = stubbed_response

faraday_response = ::Faraday::Response.new
env.request.on_data.call(stubbed_response.body, stubbed_response.body.length) if env.request.stream_response?
faraday_response.finish(env)
env[:response] = faraday_response
end
Expand All @@ -111,6 +114,7 @@ def on_recordable_request
@has_on_complete_hook = true
response = app.call(env)
response.on_complete do
restore_body_from_chunks(env.request) if env.request.stream_response?
@vcr_response = response_for(response)
VCR.record_http_interaction(VCR::HTTPInteraction.new(vcr_request, @vcr_response))
invoke_after_request_hook(@vcr_response) if delay_finishing?
Expand All @@ -121,6 +125,20 @@ def invoke_after_request_hook(response)
super
VCR.library_hooks.exclusive_hook = nil
end

def collect_chunks
caller_on_data = env.request.on_data
chunks = ''
env.request.on_data = Proc.new do |chunk, overall_received_bytes|
chunks += chunk
env.request.instance_variable_set(:@chunked_body, chunks)
caller_on_data.call(chunk, overall_received_bytes)
end
end

def restore_body_from_chunks(request)
env[:body] = request.instance_variable_get(:@chunked_body)
end
end
end
end
Expand Down
29 changes: 29 additions & 0 deletions spec/lib/vcr/library_hooks/faraday_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,5 +42,34 @@
VCR::Middleware::Faraday
])
end

context 'when using on_data callback' do
def make_request
VCR.use_cassette('no_body') do
conn = Faraday.new(:url => "http://localhost:#{VCR::SinatraApp.port}") do |builder|
builder.request :url_encoded
builder.response :logger
builder.adapter :net_http
end
conn.get("localhost_test") do |request|
request.options.on_data = Proc.new do |chunk, overall_received_bytes|
on_data_capture.push [chunk, overall_received_bytes]
end
end
end
end

let(:on_data_capture) { [] }

it 'records and replays correctly' do
expect(on_data_capture).to receive(:push).exactly(2).times.with(["Localhost response", 18])

recorded = make_request
played_back = make_request

expect(recorded.body).to eq('Localhost response')
expect(played_back.body).to eq(recorded.body)
end
end
end