spec/client_spec.rb in dschn-tweetsy-0.1.4 vs spec/client_spec.rb in dschn-tweetsy-0.1.5

- old
+ new

@@ -2,10 +2,16 @@ 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 @@ -52,37 +58,86 @@ @base.should_receive(:request).and_return(Hpricot::XML(data)) @base.user('4243').name.should == 'John Nunemaker' end end + def create_error_response(message) + " + <?xml version=\"1.0\" encoding=\"UTF-8\"?> + <hash> + <request>/users/show/genevate.xml</request> + <error>#{message}</error> + </hash> + " + 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) + 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) + 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 \ No newline at end of file