require 'test_helper' require 'clickatell/api' class ClickatellApiTest < Test::Unit::TestCase context "Clickatell::API" do setup do @client = mock('client') @client.stubs(:session_id).returns('dummy_session_id') @api = Clickatell::API.new(@client) end context "#ping" do should "perform the ping command with the current session id" do @client.expects(:perform_command).with('ping', has_entry(:session_id => 'dummy_session_id')).returns(stub_response) @api.ping end should "return true when successful" do @client.stubs(:perform_command).with('ping', anything).returns(stub_response("OK:")) assert @api.ping end should "return false when failed" do @client.stubs(:perform_command).with('ping', anything).returns(stub_response("ERR:")) assert !@api.ping end end context "#send_message with a single recipient" do should "perform the sendmsg command with the correct options" do @client.expects(:perform_command).with('sendmsg', has_entries(:to => '07999000000', :text => 'testing')).returns(stub_response) @api.send_message('07999000000', 'testing') end should "allow a custom sender to be set" do @client.expects(:perform_command).with('sendmsg', has_entry(:from => 'SENDER')).returns(stub_response) @api.send_message('07999000000', 'testing', :from => 'SENDER') end should "set the req_feat parameter when specifying a custom sender" do @client.expects(:perform_command).with('sendmsg', has_entry(:req_feat => '48')).returns(stub_response) @api.send_message('07999000000', 'testing', :from => 'SENDER') end should "set the mobile originated flag if specified" do @client.expects(:perform_command).with('sendmsg', has_entry(:mo => '1')).returns(stub_response) @api.send_message('07999000000', 'testing', :set_mobile_originated => true) end should "return a single message ID" do @client.stubs(:perform_command).returns(stub_response(%{ ID: 99999123 })) assert_equal %w{99999123}, @api.send_message('07999000000', 'testing') end end context "#send_message with multiple recipients" do should "perform the sendmsg command with a comma-separated list of recipients" do @client.expects(:perform_command).with('sendmsg', has_entry(:to => '07999000000,07999000001,07999000002')).returns(stub_response) @api.send_message(%w{07999000000 07999000001 07999000002}, 'testing') end should "return an array of message IDs" do @client.stubs(:perform_command).returns(stub_response(%{ ID: 0000001 ID: 0000002 ID: 0000003 })) expected = %w{0000001 0000002 0000003} assert_equal expected, @api.send_message(%w{07999000000 07999000001 07999000002}, 'testing') end end end private def stub_response(body = '', code = 200) stub('Net::HTTP::Response', :code => 200, :body => body) end end