test/test_handler.rb in rest-graph-1.4.0 vs test/test_handler.rb in rest-graph-1.4.1

- old
+ new

@@ -6,51 +6,83 @@ end require 'json' describe RestGraph do - before do - @id = lambda{ |obj| obj } - @error = '{"error":{"type":"Exception","message":"(#2500)"}}' - @error_hash = JSON.parse(@error) - + after do reset_webmock - stub_request(:get, 'https://graph.facebook.com/me'). - to_return(:body => @error) + RR.verify end - it 'would call error_handler if error occurred' do - RestGraph.new(:error_handler => @id).get('me').should == @error_hash - end + describe 'log handler' do + it 'would log whenever doing network request' do + stub_request(:get, 'https://graph.facebook.com/me'). + to_return(:body => '{}') - it 'would raise ::RestGraph::Error in default error_handler' do - begin - RestGraph.new.get('me') - rescue ::RestGraph::Error => e - e.message.should == @error_hash + mock(Time).now{ 666 } + mock(Time).now{ 999 } + + logger = [] + rg = RestGraph.new(:log_handler => lambda{ |e| + logger << [e.duration, e.url] }) + rg.get('me') + + logger.last.should == [333, 'https://graph.facebook.com/me'] end end -end -describe RestGraph do - before do - reset_webmock - end + describe 'with Graph API' do + before do + @id = lambda{ |obj| obj } + @error = '{"error":{"type":"Exception","message":"(#2500)"}}' + @error_hash = JSON.parse(@error) - after do - RR.verify + stub_request(:get, 'https://graph.facebook.com/me'). + to_return(:body => @error) + end + + it 'would call error_handler if error occurred' do + RestGraph.new(:error_handler => @id).get('me').should == @error_hash + end + + it 'would raise ::RestGraph::Error in default error_handler' do + begin + RestGraph.new.get('me') + rescue ::RestGraph::Error => e + e.message.should == @error_hash + end + end end - it 'would log whenever doing network request' do - stub_request(:get, 'https://graph.facebook.com/me'). - to_return(:body => '{}') + describe 'with FQL API' do + # Example of an actual response (without newline) + # {"error_code":603,"error_msg":"Unknown table: bad_table", + # "request_args":[{"key":"method","value":"fql.query"}, + # {"key":"format","value":"json"}, + # {"key":"query","value": + # "SELECT name FROM bad_table WHERE uid=12345"}]} + before do + @id = lambda{ |obj| obj } + @fql_error = '{"error_code":603,"error_msg":"Unknown table: bad"}' + @fql_error_hash = JSON.parse(@fql_error) - mock(Time).now{ 666 } - mock(Time).now{ 999 } + @bad_fql_query = 'SELECT name FROM bad_table WHERE uid="12345"' + bad_fql_request = "https://api.facebook.com/method/fql.query?" \ + "format=json&query=#{CGI.escape(@bad_fql_query)}" - logger = [] - rg = RestGraph.new(:log_handler => lambda{ |d, u| logger << [d, u] }) - rg.get('me') + stub_request(:get, bad_fql_request).to_return(:body => @fql_error) + end - logger.last.should == [333, 'https://graph.facebook.com/me'] + it 'would call error_handler if error occurred' do + RestGraph.new(:error_handler => @id).fql(@bad_fql_query). + should == @fql_error_hash + end + + it 'would raise ::RestGraph::Error in default error_handler' do + begin + RestGraph.new.fql(@bad_fql_query) + rescue ::RestGraph::Error => e + e.message.should == @fql_error_hash + end + end end end