From b07e6d2f5d902711f5c852a494b27d8357e735bb Mon Sep 17 00:00:00 2001 From: Christian Schmidt Date: Mon, 4 Nov 2024 22:59:58 +0100 Subject: [PATCH] Consume up to 1 MB of remaining response --- app/lib/request.rb | 1 + spec/lib/request_spec.rb | 20 ++++++++++++++------ 2 files changed, 15 insertions(+), 6 deletions(-) diff --git a/app/lib/request.rb b/app/lib/request.rb index fb07ecca08..c9fbf01970 100644 --- a/app/lib/request.rb +++ b/app/lib/request.rb @@ -113,6 +113,7 @@ class Request begin yield response if block_given? ensure + response.truncated_body if http_client.persistent? && !response.connection.finished_request? http_client.close unless http_client.persistent? && response.connection.finished_request? end end diff --git a/spec/lib/request_spec.rb b/spec/lib/request_spec.rb index 69aa028c48..f17cf637b9 100644 --- a/spec/lib/request_spec.rb +++ b/spec/lib/request_spec.rb @@ -99,30 +99,38 @@ RSpec.describe Request do end context 'with persistent connection' do - before { stub_request(:get, 'http://example.com').to_return(body: SecureRandom.random_bytes(100.kilobytes)) } + before { stub_request(:get, 'http://example.com').to_return(body: SecureRandom.random_bytes(2.megabytes)) } let(:http_client) { described_class.http_client.persistent('http://example.com') } let(:options) { { http_client: http_client } } - it 'leaves connection open after complete response' do + it 'leaves connection open after completely consumed response' do allow(http_client).to receive(:close) - subject.perform(&:truncated_body) + subject.perform { |response| response.truncated_body(3.megabytes) } expect(http_client).to_not have_received(:close) end - it 'closes connection after aborted response' do + it 'leaves connection open after nearly consumed response' do allow(http_client).to receive(:close) - subject.perform { |response| response.truncated_body(1.kilobyte) } + subject.perform { |response| response.truncated_body(1.8.megabytes) } + + expect(http_client).to_not have_received(:close) + end + + it 'closes connection after unconsumed response' do + allow(http_client).to receive(:close) + + subject.perform expect(http_client).to have_received(:close) end it 'yields response' do subject.perform do |response| - expect(response.body_with_limit.size).to eq 100.kilobytes + expect(response.body_with_limit(2.megabytes).size).to eq 2.megabytes end end end