Skip to content

Commit

Permalink
Show real response for JSON parser error. 0.9.19
Browse files Browse the repository at this point in the history
  • Loading branch information
kyledrake committed Aug 8, 2011
1 parent d5c9293 commit 1d6137f
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 5 deletions.
2 changes: 2 additions & 0 deletions lib/geoloqi/session.rb
Expand Up @@ -56,6 +56,8 @@ def run(meth, path, query=nil)
else else
fail fail
end end
rescue JSON::ParserError
raise Geoloqi::Error, "API returned invalid JSON. Status: #{response.status} Body: #{response.body}"
end end
@config.use_hashie_mash ? Hashie::Mash.new(hash) : hash @config.use_hashie_mash ? Hashie::Mash.new(hash) : hash
end end
Expand Down
2 changes: 1 addition & 1 deletion lib/geoloqi/version.rb
@@ -1,5 +1,5 @@
module Geoloqi module Geoloqi
def self.version def self.version
'0.9.18' '0.9.19'
end end
end end
35 changes: 31 additions & 4 deletions spec/geoloqi_spec.rb
Expand Up @@ -143,6 +143,18 @@ def api_url(path); "#{Geoloqi::API_URL}/#{Geoloqi::API_VERSION}/#{path}" end
@session = Geoloqi::Session.new :access_token => ACCESS_TOKEN @session = Geoloqi::Session.new :access_token => ACCESS_TOKEN
end end


it 'throws an exception on a hard request error' do
stub_request(:get, api_url('crashing_method')).
with(:headers => auth_headers).
to_return(:status => 500, :body => 'Something broke hard!')

expect { rescuing {Geoloqi::Session.new(:access_token => 'access_token1234').get('crashing_method')}.class == Geoloqi::Error }
expect {
rescuing {Geoloqi::Session.new(:access_token => 'access_token1234').get('crashing_method')}.message ==
"API returned invalid JSON. Status: 500 Body: Something broke hard!"
}
end

it 'successfully makes mock call with array' do it 'successfully makes mock call with array' do
stub_request(:post, api_url('play_record_at_geoloqi_hq')). stub_request(:post, api_url('play_record_at_geoloqi_hq')).
with(:headers => auth_headers, :body => [{:artist => 'Television'}].to_json). with(:headers => auth_headers, :body => [{:artist => 'Television'}].to_json).
Expand Down Expand Up @@ -328,6 +340,25 @@ def api_url(path); "#{Geoloqi::API_URL}/#{Geoloqi::API_VERSION}/#{path}" end
expect { @session.auth[:access_token] == 'access_token1234' } expect { @session.auth[:access_token] == 'access_token1234' }
expect { response['username'] == 'bulbasaurrulzok' } expect { response['username'] == 'bulbasaurrulzok' }
end end

it 'does not attempt to refresh for auth code expire' do
stub_request(:post, api_url('oauth/token')).
with(:body => {:client_id => CLIENT_ID,
:client_secret => CLIENT_SECRET,
:grant_type => "authorization_code",
:code => "1234",
:redirect_uri => "http://expired_code.example.com/"}.to_json).
to_return(:body => {:access_token => 'access_token1234',
:scope => nil,
:expires_in => '0',
:refresh_token => 'never_expires'}.to_json)

stub_request(:get, api_url('account/username?code=1234')).
with(:headers => auth_headers).
to_return(:status => 200, :body => {:points => [1,2]}.to_json)

# FINISH IMPLEMENTING
end
end end


describe 'with config and expired auth' do describe 'with config and expired auth' do
Expand Down Expand Up @@ -358,9 +389,5 @@ def api_url(path); "#{Geoloqi::API_URL}/#{Geoloqi::API_VERSION}/#{path}" end
@session.get 'account/username' @session.get 'account/username'
expect { @session.auth[:access_token] == 'access_token4567' } expect { @session.auth[:access_token] == 'access_token4567' }
end end

it 'does not attempt to refresh for auth code expire' do
# IMPLEMENT
end
end end
end end

0 comments on commit 1d6137f

Please sign in to comment.