Allow retry on Signet::AuthorizationError (#611)

This commit is contained in:
Sai Cheemalapati 2017-09-18 12:40:11 -07:00 committed by GitHub
parent bc4644ca5e
commit 2a4efbf3b9
2 changed files with 40 additions and 2 deletions

View File

@ -99,7 +99,7 @@ module Google
# NotFound, etc # NotFound, etc
auth_tries = (try == 1 && authorization_refreshable? ? 2 : 1) auth_tries = (try == 1 && authorization_refreshable? ? 2 : 1)
Retriable.retriable tries: auth_tries, Retriable.retriable tries: auth_tries,
on: [Google::Apis::AuthorizationError], on: [Google::Apis::AuthorizationError, Signet::AuthorizationError],
on_retry: proc { |*| refresh_authorization } do on_retry: proc { |*| refresh_authorization } do
execute_once(client).tap do |result| execute_once(client).tap do |result|
if block_given? if block_given?

View File

@ -19,6 +19,44 @@ RSpec.describe Google::Apis::Core::HttpCommand do
include TestHelpers include TestHelpers
include_context 'HTTP client' include_context 'HTTP client'
context('with credentials') do
let(:command) do
command = Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
command.options.authorization = Signet::OAuth2::Client.new({
:token_credential_uri => 'https://accounts.google.com/o/oauth2/token'
})
command
end
context('with one token error') do
before(:example) do
stub_request(:post, 'https://accounts.google.com/o/oauth2/token').to_return(
{ status: [500, 'Server error'] },
{ status: [200, ''], headers: { 'Content-Type' => 'application/json' }, body: '{"access_token": "foo"}' })
stub_request(:get, 'https://www.googleapis.com/zoo/animals').to_return(body: %(Hello world))
end
it 'should send credentials' do
result = command.execute(client)
expect(a_request(:get, 'https://www.googleapis.com/zoo/animals')
.with { |req| req.headers['Authorization'] == 'Bearer foo' }).to have_been_made
expect(result).to eql 'Hello world'
end
end
context('with two token errors') do
before(:example) do
stub_request(:post, 'https://accounts.google.com/o/oauth2/token').to_return(
{ status: [500, 'Server error'] },
{ status: [401, 'Unauthorized'] })
end
it 'should raise error' do
expect { command.execute(client) }.to raise_error(Signet::AuthorizationError)
end
end
end
context('with credentials') do context('with credentials') do
let(:command) do let(:command) do
command = Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals') command = Google::Apis::Core::HttpCommand.new(:get, 'https://www.googleapis.com/zoo/animals')
@ -29,7 +67,7 @@ RSpec.describe Google::Apis::Core::HttpCommand do
context('that are refreshable') do context('that are refreshable') do
let(:authorization) do let(:authorization) do
calls = 0 calls = 0
auth = object_double(Signet::OAuth2::Client.new) auth = object_double(Signet::OAuth2::Client.new)
allow(auth).to receive(:apply!) do |header| allow(auth).to receive(:apply!) do |header|
header['Authorization'] = sprintf('Bearer a_token_value_%d', calls) header['Authorization'] = sprintf('Bearer a_token_value_%d', calls)
calls += 1 calls += 1