Skip to content

Commit

Permalink
Merge pull request #460 from joaolrpaulo/fix-no-method-error-when-rai…
Browse files Browse the repository at this point in the history
…se_errors-set-to-false

Fix client `fetch_token` throwing `NoMethodError` when raise_errors is set to false
  • Loading branch information
pboling committed Nov 19, 2019
2 parents 8f422e1 + 0b9cbb7 commit 3bef9fe
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 0 deletions.
3 changes: 3 additions & 0 deletions lib/oauth2/client.rb
Expand Up @@ -149,7 +149,10 @@ def get_token(params, access_token_opts = {}, access_token_class = AccessToken)
if options[:raise_errors] && !response_contains_token
error = Error.new(response)
raise(error)
elsif !response_contains_token
return nil
end

build_access_token(response, access_token_opts, access_token_class)
end

Expand Down
28 changes: 28 additions & 0 deletions spec/oauth2/client_spec.rb
Expand Up @@ -400,6 +400,34 @@
expect(token.response.parsed).to eq('access_token' => 'the-token')
end

context 'when the :raise_errors flag is set to false' do
context 'when the request body is nil' do
it 'returns a nil :access_token' do
client = stubbed_client(:raise_errors => false) do |stub|
stub.post('/oauth/token') do
[500, {'Content-Type' => 'application/json'}, nil]
end
end

expect(client.get_token({})).to eq(nil)
end
end

context 'when the request body is not nil' do
it 'returns the parsed :access_token from body' do
client = stubbed_client do |stub|
stub.post('/oauth/token') do
[200, {'Content-Type' => 'application/json'}, MultiJson.encode('access_token' => 'the-token')]
end
end

token = client.get_token({})
expect(token.response).to be_a OAuth2::Response
expect(token.response.parsed).to eq('access_token' => 'the-token')
end
end
end

it 'forwards given token parameters' do
client = stubbed_client(:auth_scheme => :request_body) do |stub|
stub.post('/oauth/token', 'arbitrary' => 'parameter', 'client_id' => 'abc', 'client_secret' => 'def') do |env|
Expand Down

0 comments on commit 3bef9fe

Please sign in to comment.