Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion google-apis-core/google-apis-core.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ Gem::Specification.new do |gem|
gem.require_paths = ['lib']

gem.required_ruby_version = '>= 3.2'
gem.add_runtime_dependency 'addressable', '~> 2.8', '>= 2.8.7'
gem.add_runtime_dependency 'addressable', '~> 2.9'
gem.add_runtime_dependency 'faraday', '~> 2.13'
gem.add_runtime_dependency 'faraday-follow_redirects', '~> 0.3'
gem.add_runtime_dependency 'googleauth', '~> 1.14'
Expand Down
5 changes: 3 additions & 2 deletions google-apis-core/lib/google/apis/core/download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,11 +81,12 @@ def execute_once(client, &block)

http_res = client.get(url.to_s, query, request_header) do |request|
request.options.on_data = proc do |chunk, _size, res|
# The on_data callback is only invoked on a successful response.
# Some Faraday adapters (e.g. Typhoeus) may not provide a response
# object in the callback, so we default to a 200 OK status.
# Note: Ignore non-2xx responses (redirects and 4xx/5xx error payloads)
# so error messages are not written to the download stream.
status = res ? res.status.to_i : 200
next if chunk.nil? || (status >= 300 && status < 400)
next if chunk.nil? || !(200..299).include?(status)

# HTTP 206 is Partial Content
download_offset ||= (status == 206 ? @offset : 0)
Expand Down
5 changes: 3 additions & 2 deletions google-apis-core/lib/google/apis/core/storage_download.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,12 @@ def execute_once(client, &block)

http_res = client.get(url.to_s, query, request_header) do |request|
request.options.on_data = proc do |chunk, _size, res|
# The on_data callback is only invoked on a successful response.
# Some Faraday adapters (e.g. Typhoeus) may not provide a response
# object in the callback, so we default to a 200 OK status.
# Note: Ignore non-2xx responses (redirects and 4xx/5xx error payloads)
# so error messages are not written to the download stream.
status = res ? res.status.to_i : 200
next if chunk.nil? || (status >= 300 && status < 400)
next if chunk.nil? || !(200..299).include?(status)

download_offset ||= (status == 206 ? @offset : 0)
download_offset += chunk.bytesize
Expand Down
18 changes: 18 additions & 0 deletions google-apis-core/spec/google/apis/core/download_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,24 @@ def write(data)
end
end

context 'when server responds with non-2xx status and error body' do
let(:dest) { StringIO.new }

it 'does not write error body to destination IO' do
command.options.retries = 0
response = Faraday::Response.new(status: 503, body: 'Service Unavailable')
expect(client).to receive(:get) do |_url, _params, _headers, &block|
request = Faraday::Request.new
request.options = Faraday::RequestOptions.new
block.call(request)
request.options.on_data.call('Service Unavailable', 19, response)
response
end
expect { command.execute(client) }.to raise_error(Google::Apis::ServerError)
expect(dest.string).to be_empty
end
end

context 'with pathname destination' do
let(:dest) { Pathname.new(File.join(Dir.mktmpdir, 'test-path.txt')) }
let(:received) do
Expand Down
18 changes: 18 additions & 0 deletions google-apis-core/spec/google/apis/core/storage_download_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,22 @@ def write(data)
expect(received).to eql 'Hello world'
end
end

context 'when server responds with non-2xx status and error body' do
let(:dest) { StringIO.new }

it 'does not write error body to destination IO' do
command.options.retries = 0
response = Faraday::Response.new(status: 503, body: 'Service Unavailable')
expect(client).to receive(:get) do |_url, _params, _headers, &block|
request = Faraday::Request.new
request.options = Faraday::RequestOptions.new
block.call(request)
request.options.on_data.call('Service Unavailable', 19, response)
response
end
expect { command.execute(client) }.to raise_error(Google::Apis::ServerError)
expect(dest.string).to be_empty
end
end
end
Loading