From 1d6137ff240a0c59eacd3bf35aafeaf115fade42 Mon Sep 17 00:00:00 2001 From: Kyle Drake Date: Mon, 8 Aug 2011 12:53:14 -0700 Subject: [PATCH] Show real response for JSON parser error. 0.9.19 --- lib/geoloqi/session.rb | 2 ++ lib/geoloqi/version.rb | 2 +- spec/geoloqi_spec.rb | 35 +++++++++++++++++++++++++++++++---- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/lib/geoloqi/session.rb b/lib/geoloqi/session.rb index ee3c319..1b17d31 100644 --- a/lib/geoloqi/session.rb +++ b/lib/geoloqi/session.rb @@ -56,6 +56,8 @@ def run(meth, path, query=nil) else fail end + rescue JSON::ParserError + raise Geoloqi::Error, "API returned invalid JSON. Status: #{response.status} Body: #{response.body}" end @config.use_hashie_mash ? Hashie::Mash.new(hash) : hash end diff --git a/lib/geoloqi/version.rb b/lib/geoloqi/version.rb index fd0bb47..a0ac63c 100644 --- a/lib/geoloqi/version.rb +++ b/lib/geoloqi/version.rb @@ -1,5 +1,5 @@ module Geoloqi def self.version - '0.9.18' + '0.9.19' end end diff --git a/spec/geoloqi_spec.rb b/spec/geoloqi_spec.rb index ea941d0..a017673 100644 --- a/spec/geoloqi_spec.rb +++ b/spec/geoloqi_spec.rb @@ -143,6 +143,18 @@ def api_url(path); "#{Geoloqi::API_URL}/#{Geoloqi::API_VERSION}/#{path}" end @session = Geoloqi::Session.new :access_token => ACCESS_TOKEN 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 stub_request(:post, api_url('play_record_at_geoloqi_hq')). with(:headers => auth_headers, :body => [{:artist => 'Television'}].to_json). @@ -328,6 +340,25 @@ def api_url(path); "#{Geoloqi::API_URL}/#{Geoloqi::API_VERSION}/#{path}" end expect { @session.auth[:access_token] == 'access_token1234' } expect { response['username'] == 'bulbasaurrulzok' } 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 describe 'with config and expired auth' do @@ -358,9 +389,5 @@ def api_url(path); "#{Geoloqi::API_URL}/#{Geoloqi::API_VERSION}/#{path}" end @session.get 'account/username' expect { @session.auth[:access_token] == 'access_token4567' } end - - it 'does not attempt to refresh for auth code expire' do - # IMPLEMENT - end end end