Allow retry on Signet::AuthorizationError (#611)

This commit is contained in:
Sai Cheemalapati authored and GitHub committed 2017-09-18 12:40:11 -07:00
1 parent bc4644ca5e
commit 2a4efbf3b9
2 files changed
+40 -2

No files matched your search

+39 -1
View File
@@ -19,6 +19,44 @@ RSpec.describe Google::Apis::Core::HttpCommand do
include TestHelpers
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
let(:command) do
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
let(:authorization) do
calls = 0
auth = object_double(Signet::OAuth2::Client.new)
auth = object_double(Signet::OAuth2::Client.new)
allow(auth).to receive(:apply!) do |header|
header['Authorization'] = sprintf('Bearer a_token_value_%d', calls)
calls += 1