diff --git a/google-apis-core/lib/google/apis/core/base_service.rb b/google-apis-core/lib/google/apis/core/base_service.rb index 38723fc4d07..61fc3f9df5f 100644 --- a/google-apis-core/lib/google/apis/core/base_service.rb +++ b/google-apis-core/lib/google/apis/core/base_service.rb @@ -421,6 +421,10 @@ def make_storage_upload_command(method, path, options) template = Addressable::Template.new(root_url + upload_path + path) command = StorageUploadCommand.new(method, template, client_version: client_version) command.options = request_options.merge(options) + command.options.header = command.options.header&.dup || {} + unless command.options.header.any? { |k, _| k.to_s.casecmp('accept-encoding') == 0 } + command.options.header['Accept-Encoding'] = 'gzip' + end apply_command_defaults(command) command end @@ -459,6 +463,10 @@ def make_storage_download_command(method, path, options) template = Addressable::Template.new(root_url + base_path + path) command = StorageDownloadCommand.new(method, template, client_version: client_version) command.options = request_options.merge(options) + command.options.header = command.options.header&.dup || {} + unless command.options.header.any? { |k, _| k.to_s.casecmp('accept-encoding') == 0 } + command.options.header['Accept-Encoding'] = 'gzip' + end command.query['alt'] = 'media' apply_command_defaults(command) command diff --git a/google-apis-core/spec/google/apis/core/service_spec.rb b/google-apis-core/spec/google/apis/core/service_spec.rb index a4403f6cc3f..bd9e7801677 100644 --- a/google-apis-core/spec/google/apis/core/service_spec.rb +++ b/google-apis-core/spec/google/apis/core/service_spec.rb @@ -140,6 +140,10 @@ end.to raise_error(Google::Apis::UniverseDomainError) end + it 'should not include Accept-Encoding header' do + expect(command.options.header&.[]('Accept-Encoding')).to be_nil + end + include_examples 'with options' end @@ -173,6 +177,10 @@ expect(command.query).to include('alt' => 'media') end + it 'should not include Accept-Encoding header' do + expect(command.options.header&.[]('Accept-Encoding')).to be_nil + end + include_examples 'with options' end @@ -192,6 +200,18 @@ expect(command.query).to include('alt' => 'media') end + it 'should include Accept-Encoding header' do + expect(command.options.header['Accept-Encoding']).to eq('gzip') + end + + ['ACCEPT-ENCODING', 'Accept-Encoding', 'accept-encoding', :'ACCEPT-ENCODING', :'Accept-Encoding', :'accept-encoding'].each do |header_key| + it "should respect alternative capitalization/type of Accept-Encoding for #{header_key}" do + cmd = service.send(:make_storage_download_command, :get, 'zoo/animals', header: { header_key => 'identity' }) + expect(cmd.options.header[header_key]).to eq('identity') + expect(cmd.options.header.keys.count { |k| k.to_s.casecmp('accept-encoding') == 0 }).to eq(1) + end + end + include_examples 'with options' end @@ -207,6 +227,10 @@ expect(url).to eql 'https://www.googleapis.com/upload/zoo/animals' end + it 'should not include Accept-Encoding header' do + expect(command.options.header&.[]('Accept-Encoding')).to be_nil + end + include_examples 'with options' end @@ -222,6 +246,18 @@ expect(url).to eql 'https://www.googleapis.com/upload/zoo/animals' end + it 'should include Accept-Encoding header' do + expect(command.options.header['Accept-Encoding']).to eq('gzip') + end + + ['ACCEPT-ENCODING', 'Accept-Encoding', 'accept-encoding', :'ACCEPT-ENCODING', :'Accept-Encoding', :'accept-encoding'].each do |header_key| + it "should respect alternative capitalization/type of Accept-Encoding for #{header_key}" do + cmd = service.send(:make_storage_upload_command, :post, 'zoo/animals', header: { header_key => 'identity' }) + expect(cmd.options.header[header_key]).to eq('identity') + expect(cmd.options.header.keys.count { |k| k.to_s.casecmp('accept-encoding') == 0 }).to eq(1) + end + end + include_examples 'with options' end @@ -263,6 +299,12 @@ command expect(a_request(:put, upload_url)).to have_been_made.twice end + + it 'should include an accept-encoding header' do + command + expect(a_request(:put, upload_url).with { |req| req.headers['Accept-Encoding'] == 'gzip' }).to have_been_made.twice + end + end context 'not restart resumable upload if upload is completed' do before(:example) do @@ -286,6 +328,11 @@ command expect(a_request(:put, upload_url)).to have_been_made end + + it 'should include an accept-encoding header' do + command + expect(a_request(:put, upload_url).with { |req| req.headers['Accept-Encoding'] == 'gzip' }).to have_been_made + end end end diff --git a/google-apis-core/spec/google/apis/core/storage_download_spec.rb b/google-apis-core/spec/google/apis/core/storage_download_spec.rb index 2e28587a781..c60f17da16d 100644 --- a/google-apis-core/spec/google/apis/core/storage_download_spec.rb +++ b/google-apis-core/spec/google/apis/core/storage_download_spec.rb @@ -42,6 +42,12 @@ ).to have_been_made end + it 'should include an Accept-Encoding header for media downloads' do + command.execute(client) + expect(a_request(:get, 'https://www.googleapis.com/zoo/animals') + .with { |req| req.headers['Accept-Encoding'].include?('gzip') }).to have_been_made + end + it 'should receive content' do expect(received).to eql 'Hello world' end diff --git a/google-apis-core/spec/google/apis/core/storage_upload_spec.rb b/google-apis-core/spec/google/apis/core/storage_upload_spec.rb index 550ba987d3b..ad5e8192665 100644 --- a/google-apis-core/spec/google/apis/core/storage_upload_spec.rb +++ b/google-apis-core/spec/google/apis/core/storage_upload_spec.rb @@ -68,6 +68,12 @@ expect(a_request(:put, 'https://www.googleapis.com/zoo/animals') .with(body: 'Hello world')).to have_been_made end + + it 'should include an Accept-Encoding header in the outgoing request' do + command.execute(client) + expect(a_request(:put, 'https://www.googleapis.com/zoo/animals') + .with { |req| req.headers['Accept-Encoding'].include?('gzip') }).to have_been_made + end end context('with StringIO input') do