Skip to content

Commit 69490b7

Browse files
fix(core): Handle nil response in on_data callback for Faraday streaming (#24235)
--------- Co-authored-by: Yoshi Automation Bot <[email protected]>
1 parent 317a05d commit 69490b7

File tree

4 files changed

+50
-2
lines changed

4 files changed

+50
-2
lines changed

google-apis-core/lib/google/apis/core/download.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,10 @@ def execute_once(client, &block)
8181

8282
http_res = client.get(url.to_s, query, request_header) do |request|
8383
request.options.on_data = proc do |chunk, _size, res|
84-
status = res.status.to_i
84+
# The on_data callback is only invoked on a successful response.
85+
# Some Faraday adapters (e.g. Typhoeus) may not provide a response
86+
# object in the callback, so we default to a 200 OK status.
87+
status = res ? res.status.to_i : 200
8588
next if chunk.nil? || (status >= 300 && status < 400)
8689

8790
# HTTP 206 is Partial Content

google-apis-core/lib/google/apis/core/storage_download.rb

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ def execute_once(client, &block)
4848

4949
http_res = client.get(url.to_s, query, request_header) do |request|
5050
request.options.on_data = proc do |chunk, _size, res|
51-
status = res.status.to_i
51+
# The on_data callback is only invoked on a successful response.
52+
# Some Faraday adapters (e.g. Typhoeus) may not provide a response
53+
# object in the callback, so we default to a 200 OK status.
54+
status = res ? res.status.to_i : 200
5255
next if chunk.nil? || (status >= 300 && status < 400)
5356

5457
download_offset ||= (status == 206 ? @offset : 0)

google-apis-core/spec/google/apis/core/download_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,27 @@ def write(data)
136136
end
137137
end
138138

139+
context 'with streaming response' do
140+
let(:dest) { Tempfile.new('test') }
141+
let(:received) do
142+
command.execute(client)
143+
dest.rewind
144+
dest.read
145+
end
146+
147+
it 'should receive content' do
148+
response = Faraday::Response.new(status: 200, body: 'Hello world')
149+
expect(client).to receive(:get) do |_url, _params, _headers, &block|
150+
request = Faraday::Request.new
151+
request.options = Faraday::RequestOptions.new
152+
block.call(request)
153+
request.options.on_data.call('Hello world', 11, nil)
154+
response
155+
end
156+
expect(received).to eql 'Hello world'
157+
end
158+
end
159+
139160
context 'with pathname destination' do
140161
let(:dest) { Pathname.new(File.join(Dir.mktmpdir, 'test-path.txt')) }
141162
let(:received) do

google-apis-core/spec/google/apis/core/storage_download_spec.rb

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -135,4 +135,25 @@ def write(data)
135135
end
136136
end
137137
end
138+
139+
context 'with streaming response' do
140+
let(:dest) { Tempfile.new('test') }
141+
let(:received) do
142+
command.execute(client)
143+
dest.rewind
144+
dest.read
145+
end
146+
147+
it 'should receive content' do
148+
response = Faraday::Response.new(status: 200, body: 'Hello world')
149+
expect(client).to receive(:get) do |_url, _params, _headers, &block|
150+
request = Faraday::Request.new
151+
request.options = Faraday::RequestOptions.new
152+
block.call(request)
153+
request.options.on_data.call('Hello world', 11, nil)
154+
response
155+
end
156+
expect(received).to eql 'Hello world'
157+
end
158+
end
138159
end

0 commit comments

Comments
 (0)