2005-03-04 05:07:53 -05:00
require 'test/unit'
2005-03-22 08:09:44 -05:00
module Test # :nodoc:
module Unit # :nodoc:
2005-03-04 05:07:53 -05:00
class TestCase # :nodoc:
private
# invoke the specified API method
def invoke_direct ( method_name , * args )
prepare_request ( 'api' , 'api' , method_name , * args )
@controller . process ( @request , @response )
decode_rpc_response
end
alias_method :invoke , :invoke_direct
# invoke the specified API method on the specified service
def invoke_delegated ( service_name , method_name , * args )
prepare_request ( service_name . to_s , service_name , method_name , * args )
@controller . process ( @request , @response )
decode_rpc_response
end
# invoke the specified layered API method on the correct service
def invoke_layered ( service_name , method_name , * args )
prepare_request ( 'api' , service_name , method_name , * args )
@controller . process ( @request , @response )
decode_rpc_response
end
# ---------------------- internal ---------------------------
def prepare_request ( action , service_name , api_method_name , * args )
2005-08-14 13:27:28 -04:00
@request . recycle!
2005-03-04 05:07:53 -05:00
@request . request_parameters [ 'action' ] = action
@request . env [ 'REQUEST_METHOD' ] = 'POST'
@request . env [ 'HTTP_CONTENT_TYPE' ] = 'text/xml'
@request . env [ 'RAW_POST_DATA' ] = encode_rpc_call ( service_name , api_method_name , * args )
case protocol
2005-04-02 16:03:36 -05:00
when ActionWebService :: Protocol :: Soap :: SoapProtocol
2005-03-04 05:07:53 -05:00
soap_action = " / #{ @controller . controller_name } / #{ service_name } / #{ public_method_name ( service_name , api_method_name ) } "
@request . env [ 'HTTP_SOAPACTION' ] = soap_action
2005-04-02 16:03:36 -05:00
when ActionWebService :: Protocol :: XmlRpc :: XmlRpcProtocol
2005-03-04 05:07:53 -05:00
@request . env . delete ( 'HTTP_SOAPACTION' )
end
end
def encode_rpc_call ( service_name , api_method_name , * args )
case @controller . web_service_dispatching_mode
when :direct
api = @controller . class . web_service_api
when :delegated , :layered
api = @controller . web_service_object ( service_name . to_sym ) . class . web_service_api
end
2005-04-02 16:03:36 -05:00
protocol . register_api ( api )
2005-03-27 22:20:13 -05:00
method = api . api_methods [ api_method_name . to_sym ]
2006-06-27 15:41:14 -04:00
raise ArgumentError , " wrong number of arguments for rpc call ( #{ args . length } for #{ method . expects . length } ) " if method && method . expects && args . length != method . expects . length
2005-04-02 16:03:36 -05:00
protocol . encode_request ( public_method_name ( service_name , api_method_name ) , args . dup , method . expects )
2005-03-04 05:07:53 -05:00
end
def decode_rpc_response
2005-04-02 16:03:36 -05:00
public_method_name , return_value = protocol . decode_response ( @response . body )
2005-04-04 18:58:02 -04:00
exception = is_exception? ( return_value )
raise exception if exception
2005-04-02 16:03:36 -05:00
return_value
2005-03-04 05:07:53 -05:00
end
def public_method_name ( service_name , api_method_name )
public_name = service_api ( service_name ) . public_api_method_name ( api_method_name )
2005-05-08 15:34:54 -04:00
if @controller . web_service_dispatching_mode == :layered && protocol . is_a? ( ActionWebService :: Protocol :: XmlRpc :: XmlRpcProtocol )
2005-03-04 05:07:53 -05:00
'%s.%s' % [ service_name . to_s , public_name ]
else
public_name
end
end
def service_api ( service_name )
case @controller . web_service_dispatching_mode
when :direct
@controller . class . web_service_api
when :delegated , :layered
@controller . web_service_object ( service_name . to_sym ) . class . web_service_api
end
end
def protocol
2005-04-05 01:39:45 -04:00
if @protocol . nil?
2005-08-24 15:46:11 -04:00
@protocol || = ActionWebService :: Protocol :: Soap :: SoapProtocol . create ( @controller )
2005-04-05 01:39:45 -04:00
else
case @protocol
when :xmlrpc
2005-06-25 02:27:39 -04:00
@protocol = ActionWebService :: Protocol :: XmlRpc :: XmlRpcProtocol . create ( @controller )
2005-04-05 01:39:45 -04:00
when :soap
2005-06-25 02:27:39 -04:00
@protocol = ActionWebService :: Protocol :: Soap :: SoapProtocol . create ( @controller )
2005-04-05 01:39:45 -04:00
else
@protocol
end
end
2005-03-04 05:07:53 -05:00
end
def is_exception? ( obj )
case protocol
2005-04-04 18:58:02 -04:00
when :soap , ActionWebService :: Protocol :: Soap :: SoapProtocol
2005-03-04 05:07:53 -05:00
( obj . respond_to? ( :detail ) && obj . detail . respond_to? ( :cause ) && \
obj . detail . cause . is_a? ( Exception ) ) ? obj . detail . cause : nil
2005-04-04 18:58:02 -04:00
when :xmlrpc , ActionWebService :: Protocol :: XmlRpc :: XmlRpcProtocol
2005-03-04 05:07:53 -05:00
obj . is_a? ( XMLRPC :: FaultException ) ? obj : nil
end
end
end
end
end