require File.dirname(__FILE__) + '/spec_helper.rb' describe "Tweetsy::Client" do before do @base = Tweetsy::Client.new('foo', 'bar') end describe "on initialization" do it "should have empty rate_limit hash" do @base.rate_limits.should == {} end end describe "authenticated?" do it "should return true on successful authentication (200)" do Curl::Easy.stub!(:perform).and_return(mock(:result, :response_code => 200)) @base.authenticated?.should be_true end it "should return false on unsuccessful authentication (401)" do Curl::Easy.stub!(:perform).and_return(mock(:result, :response_code => 401)) @base.authenticated?.should be_false end it "should raise CantConnect on anything that's not 200 or 401" do Curl::Easy.stub!(:perform).and_return(mock(:result, :response_code => 502)) lambda { @base.authenticated? }.should raise_error(Tweetsy::CantConnect) end end describe "being initialized" do it "should require email and password" do lambda { Tweetsy::Client.new }.should raise_error(ArgumentError) end end describe "timelines" do it "should be able to retrieve friends timeline" do data = open(File.dirname(__FILE__) + '/fixtures/friends_timeline.xml').read @base.should_receive(:request).and_return(Hpricot::XML(data)) @base.friends_timeline.size.should == 3 end end describe "social graphs" do it "should be able to retrieve friend_ids" do data = open(File.dirname(__FILE__) + '/fixtures/friend_ids.xml').read @base.should_receive(:request).and_return(Hpricot::XML(data)) @base.friend_ids.size.should == 3 end end describe "users" do it "should be able to get single user" do data = open(File.dirname(__FILE__) + '/fixtures/user.xml').read @base.should_receive(:request).and_return(Hpricot::XML(data)) @base.user('4243').name.should == 'John Nunemaker' end end def create_error_response(message) " /users/show/genevate.xml #{message} " end describe "request" do it "should raise ArgumentError on bad arguments" do lambda { @base.request(:whoa, 'nada') }.should raise_error(ArgumentError) end it "should raise Unauthorized on 401" do response = mock(:response, :response_code => 401, :header_str => "", :body_str => create_error_response("test")) Curl::Easy.stub!(:perform).and_return(response) lambda { @base.request(:get, 'blah') }.should raise_error(Tweetsy::Unauthorized) lambda { @base.request(:get, 'blah') }.should raise_error(Tweetsy::CantConnect) end it "should raise BadRequest on 400" do response = mock(:response, :response_code => 400, :header_str => "", :body_str => create_error_response("test")) Curl::Easy.stub!(:perform).and_return(response) lambda { @base.request(:get, 'blah') }.should raise_error(Tweetsy::BadRequest) lambda { @base.request(:get, 'blah') }.should raise_error(Tweetsy::CantConnect) end it "should raise Forbidden on 403" do response = mock(:response, :response_code => 403, :header_str => "", :body_str => create_error_response("test")) Curl::Easy.stub!(:perform).and_return(response) lambda { @base.request(:get, 'blah') }.should raise_error(Tweetsy::Forbidden) lambda { @base.request(:get, 'blah') }.should raise_error(Tweetsy::CantConnect) end it "should raise CantConnect on anything that isn't 200 or 304 response" do [401, 503, 504, 505, nil].each do |code| response = mock(:response, :response_code => code, :header_str => "", :body_str => create_error_response("test")) Curl::Easy.stub!(:perform).and_return(response) lambda { @base.request(:get, 'blah') }.should raise_error(Tweetsy::CantConnect) end end it "should return rate limit status" do response = mock(:response, :body_str => "Hello World", :response_code => 200, :header_str => "X-RateLimit-Limit: 100\r\nX-RateLimit-Remaining: 98\r\nX-RateLimit-Reset: 12345678") Curl::Easy.stub!(:perform).and_return(response) @base.request(:get, 'blah') @base.rate_limits[:limit].should == 100 @base.rate_limits[:remaining].should == 98 @base.rate_limits[:reset].should == 12345678 end end end