From 33a4aea60d2fca1ff47853df57c634cc1ecd27e6 Mon Sep 17 00:00:00 2001 From: Mike Moore Date: Mon, 19 Aug 2019 15:16:51 -0600 Subject: [PATCH] fix: Use apply_request_options for batch auth header Use HttpCommand#apply_request_options to add the Authorization header for individual batch requests, as it supports both OAuth token strings and googleauth/signet objects. [pr #823, refs #822, closes #817] --- lib/google/apis/core/batch.rb | 4 +-- spec/google/apis/core/batch_spec.rb | 53 +++++++++++++++++++++++++++++ 2 files changed, 55 insertions(+), 2 deletions(-) diff --git a/lib/google/apis/core/batch.rb b/lib/google/apis/core/batch.rb index 3e55183a4..bf20051e3 100644 --- a/lib/google/apis/core/batch.rb +++ b/lib/google/apis/core/batch.rb @@ -163,6 +163,8 @@ module Google # the serialized request def to_part(call) call.prepare! + # This will add the Authorization header if needed. + call.apply_request_options(call.header) parts = [] parts << build_head(call) parts << build_body(call) unless call.body.nil? @@ -177,8 +179,6 @@ module Google call.header.each do |key, value| request_head << sprintf("\r\n%s: %s", key, value) end - token = call.options.authorization - request_head << "\r\nAuthorization: Bearer #{token}" unless token.nil? request_head << sprintf("\r\nHost: %s", call.url.host) request_head << "\r\n\r\n" StringIO.new(request_head) diff --git a/spec/google/apis/core/batch_spec.rb b/spec/google/apis/core/batch_spec.rb index 46e6e65de..bd15b4936 100644 --- a/spec/google/apis/core/batch_spec.rb +++ b/spec/google/apis/core/batch_spec.rb @@ -117,6 +117,59 @@ Host: www.googleapis.com Goodbye! --123abc-- +EOF + expect(a_request(:post, 'https://www.googleapis.com/batch').with(body: expected_body)).to have_been_made + end + + it 'should send content with authorization header when set' do + b = ->(_res, _err) {} + signet_auth = object_double(Signet::OAuth2::Client.new).tap do |a| + allow(a).to receive(:apply!) do |header| + header['Authorization'] = 'Bearer a_token_value' + end + end + command.add(get_command.tap { |c| c.options.authorization = 'oauthtoken' }, &b) + command.add(post_with_string_command.tap { |c| c.options.authorization = signet_auth }, &b) + command.add(post_with_io_command, &b) + command.execute(client) + + expected_body = < +Content-Length: 92 +Content-Transfer-Encoding: binary + +GET /zoo/animals/1? HTTP/1.1 +Authorization: Bearer oauthtoken +Host: www.googleapis.com + + +--123abc +Content-Type: application/http +Content-Id: +Content-Length: 133 +Content-Transfer-Encoding: binary + +POST /zoo/animals/2? HTTP/1.1 +Content-Type: text/plain +Authorization: Bearer a_token_value +Host: www.googleapis.com + +Hello world +--123abc +Content-Type: application/http +Content-Id: +Content-Length: 93 +Content-Transfer-Encoding: binary + +POST /zoo/animals/3? HTTP/1.1 +Content-Type: text/plain +Host: www.googleapis.com + +Goodbye! +--123abc-- + EOF expect(a_request(:post, 'https://www.googleapis.com/batch').with(body: expected_body)).to have_been_made end