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]
This commit is contained in:
parent
837cf00706
commit
33a4aea60d
|
@ -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)
|
||||
|
|
|
@ -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 = <<EOF.gsub(/\n/, "\r\n")
|
||||
--123abc
|
||||
Content-Type: application/http
|
||||
Content-Id: <ffe23d1b-e8f7-47f5-8c01-2a30cf8ecb8f+0>
|
||||
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: <ffe23d1b-e8f7-47f5-8c01-2a30cf8ecb8f+1>
|
||||
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: <ffe23d1b-e8f7-47f5-8c01-2a30cf8ecb8f+2>
|
||||
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
|
||||
|
|
Loading…
Reference in New Issue