spec/api_spec.rb in clickatell-0.4.1 vs spec/api_spec.rb in clickatell-0.5.0

- old
+ new

@@ -1,8 +1,14 @@ require File.dirname(__FILE__) + '/spec_helper' require File.dirname(__FILE__) + '/../lib/clickatell' +class FakeHttp + def start(&block) + yield self + end +end + module Clickatell describe "API Command" do before do @command = API::Command.new('cmdname') @@ -19,146 +25,143 @@ end end describe "Secure API Command" do before do - @command = API::Command.new('cmdname', :secure => true) + @command = API::Command.new('cmdname', 'http', :secure => true) end it "should use HTTPS" do url = @command.with_params(:param_one => 'abc', :param_two => '123') url.should == URI.parse("https://api.clickatell.com/http/cmdname?param_one=abc&param_two=123") end end describe "Command executor" do - it "should create an API command with auth params and send it via HTTP get, returning the raw http response" do + it "should create an API command with the given params" do executor = API::CommandExecutor.new(:session_id => '12345') - API::Command.should_receive(:new).with('cmdname', :secure => false).and_return(cmd=mock('command')) - uri = stub('uri', :host => 'example.com', :port => 80, :scheme => 'http', :path => '/foo/bar', :query => 'a=b') - cmd.should_receive(:with_params).with(:param_one => 'foo', :session_id => '12345').and_return(uri) - Net::HTTP.should_receive(:new).with('example.com', 80).and_return(transport=mock('http')) - transport.should_receive(:use_ssl=).with(false) - transport.should_receive(:start).and_yield(yielded_transport=mock('http')) - yielded_transport.should_receive(:get).with('/foo/bar?a=b').and_return(raw_response=mock('http response')) - executor.execute('cmdname', :param_one => 'foo').should == raw_response + executor.stubs(:get_response).returns([]) + API::Command.expects(:new).with('cmdname', 'http', :secure => false).returns(command = stub('Command')) + command.expects(:with_params).with(:param_one => 'foo', :session_id => '12345').returns(stub_everything('URI')) + executor.execute('cmdname', 'http', :param_one => 'foo') end - it "should create a secure API command and send command using HTTPS if secure is true" do - executor = API::CommandExecutor.new({:session_id => '12345'}, secure=true) - API::Command.should_receive(:new).with('cmdname', :secure => true).and_return(cmd=mock('command')) - uri = stub('uri', :host => 'example.com', :port => 443, :scheme => 'https', :path => '/foo/bar', :query => 'a=b') - cmd.should_receive(:with_params).with(:param_one => 'foo', :session_id => '12345').and_return(uri) - Net::HTTP.should_receive(:new).with('example.com', 443).and_return(transport=mock('http')) - transport.should_receive(:use_ssl=).with(true) - transport.should_receive(:start).and_yield(yielded_transport=mock('http')) - yielded_transport.should_receive(:get).with('/foo/bar?a=b').and_return(raw_response=mock('http response')) - executor.execute('cmdname', :param_one => 'foo').should == raw_response + it "should send the URI generated by the created command via HTTP get and return the response" do + executor = API::CommandExecutor.new(:session_id => '12345') + command_uri = URI.parse('http://clickatell.com:8080/path?foo=bar') + API::Command.stubs(:new).returns(command = stub('Command', :with_params => command_uri)) + Net::HTTP.stubs(:new).with(command_uri.host, command_uri.port).returns(http = FakeHttp.new) + http.stubs(:use_ssl=) + http.stubs(:get).with('/path?foo=bar').returns([response = stub('HTTP Response'), 'response body']) + executor.execute('cmdname', 'http').should == response end + + it "should send the command over a secure HTTPS connection if :secure option is set to true" do + executor = API::CommandExecutor.new({:session_id => '12345'}, secure = true) + Net::HTTP.stubs(:new).returns(http = mock('HTTP')) + http.expects(:use_ssl=).with(true) + http.stubs(:start).returns([]) + executor.execute('cmdname', 'http') + end end describe "API" do before do API.debug_mode = false API.secure_mode = false - API::CommandExecutor.should_receive(:new).with({:session_id => '1234'}, false, false).and_return(@executor = mock('command executor')) + @executor = mock('command executor') @api = API.new(:session_id => '1234') + API::CommandExecutor.stubs(:new).with({:session_id => '1234'}, false, false).returns(@executor) end - it "should return session_id for successful authentication" do - @executor.should_receive(:execute).with('auth', + it "should use the api_id, username and password to authenticate and return the new session id" do + @executor.expects(:execute).with('auth', 'http', :api_id => '1234', :user => 'joebloggs', :password => 'superpass' - ).and_return(response=mock('response')) - Response.should_receive(:parse).with(response).and_return('OK' => 'new_session_id') + ).returns(response = stub('response')) + Response.stubs(:parse).with(response).returns('OK' => 'new_session_id') @api.authenticate('1234', 'joebloggs', 'superpass').should == 'new_session_id' end - it "should support ping" do - @executor.should_receive(:execute).with('ping', :session_id => 'abcdefg').and_return(response=mock('response')) + it "should support ping, using the current session_id" do + @executor.expects(:execute).with('ping', 'http', :session_id => 'abcdefg').returns(response = stub('response')) @api.ping('abcdefg').should == response end - it "should support sending messages, returning the message id" do - @executor.should_receive(:execute).with('sendmsg', + it "should support sending messages to a specified number, returning the message id" do + @executor.expects(:execute).with('sendmsg', 'http', :to => '4477791234567', :text => 'hello world' - ).and_return(response=mock('response')) - Response.should_receive(:parse).with(response).and_return('ID' => 'message_id') + ).returns(response = stub('response')) + Response.stubs(:parse).with(response).returns('ID' => 'message_id') @api.send_message('4477791234567', 'hello world').should == 'message_id' end - it "should support sending messages with custom sender, passing the appropriate feature mask, returning the message id" do - @executor.should_receive(:execute).with('sendmsg', - :to => '4477791234567', - :text => 'hello world', - :from => 'LUKE', - :req_feat => '48' - ).and_return(response=mock('response')) - Response.should_receive(:parse).with(response).and_return('ID' => 'message_id') + it "should set the :from parameter and set the :req_feat to 48 when using a custom from string when sending a message" do + @executor.expects(:execute).with('sendmsg', 'http', has_entries(:from => 'LUKE', :req_feat => '48')).returns(response = stub('response')) + Response.stubs(:parse).with(response).returns('ID' => 'message_id') @api.send_message('4477791234567', 'hello world', :from => 'LUKE') end - it "should ignore any invalid parameters when sending message" do - @executor.should_receive(:execute).with('sendmsg', - :to => '4477791234567', - :text => 'hello world', - :from => 'LUKE', - :req_feat => '48' - ).and_return(response=mock('response')) - Response.stub!(:parse).and_return('ID' => 'foo') + it "should set the :mo flag to 1 when :set_mobile_originated is true when sending a message" do + @executor.expects(:execute).with('sendmsg', 'http', has_entry(:mo => '1')).returns(response=mock('response')) + Response.stubs(:parse).with(response).returns('ID' => 'message_id') + @api.send_message('4477791234567', 'hello world', :set_mobile_originated => true) + end + + it "should ignore any invalid parameters when sending a message" do + @executor.expects(:execute).with('sendmsg', 'http', Not(has_key(:any_old_param))).returns(response = stub('response')) + Response.stubs(:parse).returns('ID' => 'foo') @api.send_message('4477791234567', 'hello world', :from => 'LUKE', :any_old_param => 'test') end - it "should support message status query, returning message status" do - @executor.should_receive(:execute).with('querymsg', - :apimsgid => 'messageid' - ).and_return(response=mock('response')) - Response.should_receive(:parse).with(response).and_return('ID' => 'message_id', 'Status' => 'message_status') + it "should support message status query for a given message id, returning the message status" do + @executor.expects(:execute).with('querymsg', 'http', :apimsgid => 'messageid').returns(response = stub('response')) + Response.expects(:parse).with(response).returns('ID' => 'message_id', 'Status' => 'message_status') @api.message_status('messageid').should == 'message_status' end it "should support balance query, returning number of credits as a float" do - @executor.should_receive(:execute).with('getbalance', {}).and_return(response=mock('response')) - Response.should_receive(:parse).with(response).and_return('Credit' => '10.0') + @executor.expects(:execute).with('getbalance', 'http', {}).returns(response=mock('response')) + Response.stubs(:parse).with(response).returns('Credit' => '10.0') @api.account_balance.should == 10.0 end it "should raise an API::Error if the response parser raises" do - @executor.stub!(:execute) - Response.stub!(:parse).and_raise(Clickatell::API::Error.new('', '')) + @executor.stubs(:execute) + Response.stubs(:parse).raises(Clickatell::API::Error.new('', '')) proc { @api.account_balance }.should raise_error(Clickatell::API::Error) end end describe API, ' when authenticating' do it "should authenticate to retrieve a session_id and return a new API instance using that session id" do - API.stub!(:new).and_return(api=mock('api')) - api.should_receive(:authenticate).with('my_api_key', 'joebloggs', 'mypassword').and_return('new_session_id') - api.should_receive(:auth_options=).with(:session_id => 'new_session_id') + API.stubs(:new).returns(api = mock('api')) + api.stubs(:authenticate).with('my_api_key', 'joebloggs', 'mypassword').returns('new_session_id') + api.expects(:auth_options=).with(:session_id => 'new_session_id') API.authenticate('my_api_key', 'joebloggs', 'mypassword') end end describe API, ' with no authentication options set' do it "should build commands with no authentication options" do API.debug_mode = false API.secure_mode = false api = API.new - API::CommandExecutor.should_receive(:new).with({}, false, false).and_return(executor=mock('command executor')) - executor.stub!(:execute) + API::CommandExecutor.stubs(:new).with({}, false, false).returns(executor = stub('command executor')) + executor.stubs(:execute) api.ping('1234') end end describe API, ' in secure mode' do it "should execute commands securely" do API.debug_mode = false API.secure_mode = true api = API.new - API::CommandExecutor.should_receive(:new).with({}, true, false).and_return(executor=mock('command executor')) - executor.stub!(:execute) + API::CommandExecutor.expects(:new).with({}, true, false).returns(executor = stub('command executor')) + executor.stubs(:execute) api.ping('1234') end end describe "API Error" do \ No newline at end of file