From 8cf4330494fd55da9d193a1e0fcdedeea3371dca Mon Sep 17 00:00:00 2001 From: Daniel Azuma Date: Fri, 16 Aug 2019 10:13:11 -0700 Subject: [PATCH] feat: send x-goog-api-client header in all requests --- lib/google/apis/core/api_command.rb | 12 ++++++++++ spec/google/apis/core/api_command_spec.rb | 27 +++++++++++++++++++++++ spec/google/apis/core/service_spec.rb | 11 ++++++++- 3 files changed, 49 insertions(+), 1 deletion(-) diff --git a/lib/google/apis/core/api_command.rb b/lib/google/apis/core/api_command.rb index 9262f87dc..1d0e82053 100644 --- a/lib/google/apis/core/api_command.rb +++ b/lib/google/apis/core/api_command.rb @@ -52,6 +52,7 @@ module Google # # @return [void] def prepare! + set_xgac if options && options.api_format_version header['X-Goog-Api-Format-Version'] = options.api_format_version.to_s end @@ -125,6 +126,17 @@ module Google private + def set_xgac + old_xgac = header + .find_all { |k, v| k.downcase == 'x-goog-api-client' } + .map { |(a, b)| b } + .join(' ') + xgac = "gl-ruby/#{RUBY_VERSION} gdcl/#{Google::Apis::VERSION}" + xgac = old_xgac.empty? ? xgac : "#{old_xgac} #{xgac}" + header.delete_if { |k, v| k.downcase == 'x-goog-api-client' } + header['X-Goog-Api-Client'] = xgac + end + # Attempt to parse a JSON error message # @param [String] body # HTTP response body diff --git a/spec/google/apis/core/api_command_spec.rb b/spec/google/apis/core/api_command_spec.rb index 6b57863bb..df6d9be3e 100644 --- a/spec/google/apis/core/api_command_spec.rb +++ b/spec/google/apis/core/api_command_spec.rb @@ -32,6 +32,33 @@ RSpec.describe Google::Apis::Core::ApiCommand do end end + let(:x_goog_api_client_value) { "gl-ruby/#{RUBY_VERSION} gdcl/#{Google::Apis::VERSION}" } + + context('with preparation') do + let(:command) do + Google::Apis::Core::ApiCommand.new(:get, 'https://www.googleapis.com/zoo/animals') + end + + it 'should set X-Goog-Api-Client header if none is set' do + command.prepare! + expect(command.header['X-Goog-Api-Client']).to eql x_goog_api_client_value + end + + it 'should append to x-goog-api-client header with case difference' do + command.header['x-goog-api-client'] = "foo/1.2.3" + command.prepare! + expect(command.header['X-Goog-Api-Client']).to eql "foo/1.2.3 #{x_goog_api_client_value}" + end + + it 'should append to multiple x-goog-api-client headers' do + command.header['x-goog-api-client'] = "foo/1.2.3" + command.header['X-Goog-Api-Client'] = "bar/4.5.6" + command.prepare! + expect(command.header['X-Goog-Api-Client']).to eql "foo/1.2.3 bar/4.5.6 #{x_goog_api_client_value}" + expect(command.header['x-goog-api-client']).to be nil + end + end + context('with a request body') do let(:command) do request = model_class.new diff --git a/spec/google/apis/core/service_spec.rb b/spec/google/apis/core/service_spec.rb index abb80ce28..39709f947 100644 --- a/spec/google/apis/core/service_spec.rb +++ b/spec/google/apis/core/service_spec.rb @@ -21,6 +21,7 @@ RSpec.describe Google::Apis::Core::BaseService do include TestHelpers let(:service) { Google::Apis::Core::BaseService.new('https://www.googleapis.com/', '') } + let(:x_goog_api_client_value) { "gl-ruby/#{RUBY_VERSION} gdcl/#{Google::Apis::VERSION}" } before do Google::Apis::ClientOptions.default.application_name = 'test' @@ -122,6 +123,13 @@ RSpec.describe Google::Apis::Core::BaseService do expect(url).to eql 'https://www.googleapis.com/zoo/animals' end + it 'should send the command with x-goog-api-client header' do + stub_request(:get, 'https://www.googleapis.com/zoo/animals').to_return(body: '') + service.send(:execute_or_queue_command, command) + expected_headers = {'X-Goog-Api-Client': x_goog_api_client_value} + expect(a_request(:get, 'https://www.googleapis.com/zoo/animals').with(headers: expected_headers)).to have_been_made + end + include_examples 'with options' end @@ -269,10 +277,11 @@ EOF --outer Content-Type: application/http Content-Id: -Content-Length: 303 +Content-Length: 349 Content-Transfer-Encoding: binary POST /upload/zoo/animals? HTTP/1.1 +X-Goog-Api-Client: #{x_goog_api_client_value} Content-Type: multipart/related; boundary=inner X-Goog-Upload-Protocol: multipart Host: www.googleapis.com