Skip to content

Commit a83078e

Browse files
storage(feat): Add accept encoding gzip for media operation only (#26530)
1 parent ae59d13 commit a83078e

4 files changed

Lines changed: 67 additions & 0 deletions

File tree

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,10 @@ def make_storage_upload_command(method, path, options)
421421
template = Addressable::Template.new(root_url + upload_path + path)
422422
command = StorageUploadCommand.new(method, template, client_version: client_version)
423423
command.options = request_options.merge(options)
424+
command.options.header = command.options.header&.dup || {}
425+
unless command.options.header.any? { |k, _| k.to_s.casecmp('accept-encoding') == 0 }
426+
command.options.header['Accept-Encoding'] = 'gzip'
427+
end
424428
apply_command_defaults(command)
425429
command
426430
end
@@ -459,6 +463,10 @@ def make_storage_download_command(method, path, options)
459463
template = Addressable::Template.new(root_url + base_path + path)
460464
command = StorageDownloadCommand.new(method, template, client_version: client_version)
461465
command.options = request_options.merge(options)
466+
command.options.header = command.options.header&.dup || {}
467+
unless command.options.header.any? { |k, _| k.to_s.casecmp('accept-encoding') == 0 }
468+
command.options.header['Accept-Encoding'] = 'gzip'
469+
end
462470
command.query['alt'] = 'media'
463471
apply_command_defaults(command)
464472
command

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

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,10 @@
140140
end.to raise_error(Google::Apis::UniverseDomainError)
141141
end
142142

143+
it 'should not include Accept-Encoding header' do
144+
expect(command.options.header&.[]('Accept-Encoding')).to be_nil
145+
end
146+
143147
include_examples 'with options'
144148
end
145149

@@ -173,6 +177,10 @@
173177
expect(command.query).to include('alt' => 'media')
174178
end
175179

180+
it 'should not include Accept-Encoding header' do
181+
expect(command.options.header&.[]('Accept-Encoding')).to be_nil
182+
end
183+
176184
include_examples 'with options'
177185
end
178186

@@ -192,6 +200,18 @@
192200
expect(command.query).to include('alt' => 'media')
193201
end
194202

203+
it 'should include Accept-Encoding header' do
204+
expect(command.options.header['Accept-Encoding']).to eq('gzip')
205+
end
206+
207+
['ACCEPT-ENCODING', 'Accept-Encoding', 'accept-encoding', :'ACCEPT-ENCODING', :'Accept-Encoding', :'accept-encoding'].each do |header_key|
208+
it "should respect alternative capitalization/type of Accept-Encoding for #{header_key}" do
209+
cmd = service.send(:make_storage_download_command, :get, 'zoo/animals', header: { header_key => 'identity' })
210+
expect(cmd.options.header[header_key]).to eq('identity')
211+
expect(cmd.options.header.keys.count { |k| k.to_s.casecmp('accept-encoding') == 0 }).to eq(1)
212+
end
213+
end
214+
195215
include_examples 'with options'
196216
end
197217

@@ -207,6 +227,10 @@
207227
expect(url).to eql 'https://www.googleapis.com/upload/zoo/animals'
208228
end
209229

230+
it 'should not include Accept-Encoding header' do
231+
expect(command.options.header&.[]('Accept-Encoding')).to be_nil
232+
end
233+
210234
include_examples 'with options'
211235
end
212236

@@ -222,6 +246,18 @@
222246
expect(url).to eql 'https://www.googleapis.com/upload/zoo/animals'
223247
end
224248

249+
it 'should include Accept-Encoding header' do
250+
expect(command.options.header['Accept-Encoding']).to eq('gzip')
251+
end
252+
253+
['ACCEPT-ENCODING', 'Accept-Encoding', 'accept-encoding', :'ACCEPT-ENCODING', :'Accept-Encoding', :'accept-encoding'].each do |header_key|
254+
it "should respect alternative capitalization/type of Accept-Encoding for #{header_key}" do
255+
cmd = service.send(:make_storage_upload_command, :post, 'zoo/animals', header: { header_key => 'identity' })
256+
expect(cmd.options.header[header_key]).to eq('identity')
257+
expect(cmd.options.header.keys.count { |k| k.to_s.casecmp('accept-encoding') == 0 }).to eq(1)
258+
end
259+
end
260+
225261
include_examples 'with options'
226262
end
227263

@@ -263,6 +299,12 @@
263299
command
264300
expect(a_request(:put, upload_url)).to have_been_made.twice
265301
end
302+
303+
it 'should include an accept-encoding header' do
304+
command
305+
expect(a_request(:put, upload_url).with { |req| req.headers['Accept-Encoding'] == 'gzip' }).to have_been_made.twice
306+
end
307+
266308
end
267309
context 'not restart resumable upload if upload is completed' do
268310
before(:example) do
@@ -286,6 +328,11 @@
286328
command
287329
expect(a_request(:put, upload_url)).to have_been_made
288330
end
331+
332+
it 'should include an accept-encoding header' do
333+
command
334+
expect(a_request(:put, upload_url).with { |req| req.headers['Accept-Encoding'] == 'gzip' }).to have_been_made
335+
end
289336
end
290337
end
291338

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,12 @@
4242
).to have_been_made
4343
end
4444

45+
it 'should include an Accept-Encoding header for media downloads' do
46+
command.execute(client)
47+
expect(a_request(:get, 'https://www.googleapis.com/zoo/animals')
48+
.with { |req| req.headers['Accept-Encoding'].include?('gzip') }).to have_been_made
49+
end
50+
4551
it 'should receive content' do
4652
expect(received).to eql 'Hello world'
4753
end

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

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,6 +68,12 @@
6868
expect(a_request(:put, 'https://www.googleapis.com/zoo/animals')
6969
.with(body: 'Hello world')).to have_been_made
7070
end
71+
72+
it 'should include an Accept-Encoding header in the outgoing request' do
73+
command.execute(client)
74+
expect(a_request(:put, 'https://www.googleapis.com/zoo/animals')
75+
.with { |req| req.headers['Accept-Encoding'].include?('gzip') }).to have_been_made
76+
end
7177
end
7278

7379
context('with StringIO input') do

0 commit comments

Comments
 (0)