spec/api_spec.rb in clickatell-0.1.0 vs spec/api_spec.rb in clickatell-0.2.0
- old
+ new
@@ -22,11 +22,11 @@
url.should == URI.parse("https://api.clickatell.com/http/cmdname?param_one=abc¶m_two=123")
end
end
describe "Command executor" do
- it "should create an API command and send it via HTTP get" do
+ it "should create an API command and send it via HTTP get, returning the raw http response" do
API::Command.should_receive(:new).with('cmdname').and_return(cmd=mock('command'))
cmd.should_receive(:with_params).with(:param_one => 'foo').and_return(uri=mock('uri'))
Net::HTTP.should_receive(:get_response).with(uri).and_return(raw_response=mock('http response'))
API.send(:execute_command, 'cmdname', :param_one => 'foo').should == raw_response
end
@@ -90,9 +90,42 @@
:session_id => 'abcde',
:apimsgid => 'messageid'
).and_return(response=mock('response'))
Response.should_receive(:parse).with(response).and_return('ID' => 'message_id', 'Status' => 'message_status')
API.message_status('messageid', :session_id => 'abcde').should == 'message_status'
+ end
+
+ it "should support balance query with authentication, returning number of credits as a float" do
+ API.should_receive(:execute_command).with('getbalance',
+ :api_id => '1234',
+ :user => 'joebloggs',
+ :password => 'superpass'
+ ).and_return(response=mock('response'))
+ Response.should_receive(:parse).with(response).and_return('Credit' => '10.0')
+ API.account_balance(:username => 'joebloggs', :password => 'superpass', :api_key => '1234').should == 10.0
+ end
+
+ it "should support balance query with pre-auth, returning number of credits as a float" do
+ API.should_receive(:execute_command).with('getbalance',
+ :session_id => 'abcde'
+ ).and_return(response=mock('response'))
+ Response.should_receive(:parse).with(response).and_return('Credit' => '10.0')
+ API.account_balance(:session_id => 'abcde').should == 10.0
+ end
+
+ it "should raise an API::Error if the response parser raises" do
+ API.stub!(:execute_command)
+ Response.stub!(:parse).and_raise(Clickatell::API::Error.new('', ''))
+ proc { API.account_balance({}) }.should raise_error(Clickatell::API::Error)
+ end
+ end
+
+ describe "API Error" do
+ it "should parse http response string to create error" do
+ response_string = "ERR: 001, Authentication error"
+ error = Clickatell::API::Error.parse(response_string)
+ error.code.should == '001'
+ error.message.should == 'Authentication error'
end
end
end
\ No newline at end of file