feat: send x-goog-api-client header in all requests

This commit is contained in:
Daniel Azuma 2019-08-16 10:13:11 -07:00 committed by GitHub
parent eea7d16b5e
commit 8cf4330494
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 1 deletions

View File

@ -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

View File

@ -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

View File

@ -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: <b1981e17-f622-49af-b2eb-203308b1b17d+0>
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