diff --git a/lib/google/apis/core/api_command.rb b/lib/google/apis/core/api_command.rb index 39919999d..8eddb7568 100644 --- a/lib/google/apis/core/api_command.rb +++ b/lib/google/apis/core/api_command.rb @@ -53,6 +53,10 @@ module Google # @return [void] def prepare! set_xgac + if options&.authorization.respond_to? :quota_project_id + quota_project_id = options.authorization.quota_project_id + header['X-Goog-User-Project'] = quota_project_id if quota_project_id + end if options && options.api_format_version header['X-Goog-Api-Format-Version'] = options.api_format_version.to_s end diff --git a/spec/google/apis/core/api_command_spec.rb b/spec/google/apis/core/api_command_spec.rb index 4303d5b0c..3acd62168 100644 --- a/spec/google/apis/core/api_command_spec.rb +++ b/spec/google/apis/core/api_command_spec.rb @@ -65,6 +65,17 @@ RSpec.describe Google::Apis::Core::ApiCommand do 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 + + it 'should not set the X-Goog-User-Project header if there is no quota_project' do + command.prepare! + expect(command.header['X-Goog-User-Project']).to be_nil + end + + it 'should set the X-Goog-User-Project to a given quota_project' do + command.options.authorization = OpenStruct.new quota_project_id: "b_project_id" + command.prepare! + expect(command.header['X-Goog-User-Project']).to eql "b_project_id" + end end context('with a request body') do diff --git a/spec/spec_helper.rb b/spec/spec_helper.rb index 879b2ebf6..424a9011f 100644 --- a/spec/spec_helper.rb +++ b/spec/spec_helper.rb @@ -43,6 +43,7 @@ require 'webmock/rspec' require 'fakefs/spec_helpers' require 'json_spec' require 'logging' +require 'ostruct' require 'rspec/logging_helper' require 'google/apis' require 'google/apis/core/base_service'