@@ -343,6 +343,94 @@ def run_server():
343343 self .assertEqual (p .method (), 5 )
344344 self .assertEqual (p .method (), 5 )
345345
346+
347+ class SimpleXMLRPCDispatcherTestCase (unittest .TestCase ):
348+ class DispatchExc (Exception ):
349+ """Raised inside the dispatched functions when checking for
350+ chained exceptions"""
351+
352+ def test_call_registered_func (self ):
353+ """Calls explicitly registered function"""
354+ # Makes sure any exception raised inside the function has no other
355+ # exception chained to it
356+
357+ exp_params = 1 , 2 , 3
358+
359+ def dispatched_func (* params ):
360+ raise self .DispatchExc (params )
361+
362+ dispatcher = xmlrpc .server .SimpleXMLRPCDispatcher ()
363+ dispatcher .register_function (dispatched_func )
364+ with self .assertRaises (self .DispatchExc ) as exc_ctx :
365+ dispatcher ._dispatch ('dispatched_func' , exp_params )
366+ self .assertEqual (exc_ctx .exception .args , (exp_params ,))
367+ self .assertIsNone (exc_ctx .exception .__cause__ )
368+ self .assertIsNone (exc_ctx .exception .__context__ )
369+
370+ def test_call_instance_func (self ):
371+ """Calls a registered instance attribute as a function"""
372+ # Makes sure any exception raised inside the function has no other
373+ # exception chained to it
374+
375+ exp_params = 1 , 2 , 3
376+
377+ class DispatchedClass :
378+ def dispatched_func (self , * params ):
379+ raise SimpleXMLRPCDispatcherTestCase .DispatchExc (params )
380+
381+ dispatcher = xmlrpc .server .SimpleXMLRPCDispatcher ()
382+ dispatcher .register_instance (DispatchedClass ())
383+ with self .assertRaises (self .DispatchExc ) as exc_ctx :
384+ dispatcher ._dispatch ('dispatched_func' , exp_params )
385+ self .assertEqual (exc_ctx .exception .args , (exp_params ,))
386+ self .assertIsNone (exc_ctx .exception .__cause__ )
387+ self .assertIsNone (exc_ctx .exception .__context__ )
388+
389+ def test_call_dispatch_func (self ):
390+ """Calls the registered instance's `_dispatch` function"""
391+ # Makes sure any exception raised inside the function has no other
392+ # exception chained to it
393+
394+ exp_method = 'method'
395+ exp_params = 1 , 2 , 3
396+
397+ class TestInstance :
398+ def _dispatch (self , method , params ):
399+ raise SimpleXMLRPCDispatcherTestCase .DispatchExc (
400+ method , params )
401+
402+ dispatcher = xmlrpc .server .SimpleXMLRPCDispatcher ()
403+ dispatcher .register_instance (TestInstance ())
404+ with self .assertRaises (self .DispatchExc ) as exc_ctx :
405+ dispatcher ._dispatch (exp_method , exp_params )
406+ self .assertEqual (exc_ctx .exception .args , (exp_method , exp_params ))
407+ self .assertIsNone (exc_ctx .exception .__cause__ )
408+ self .assertIsNone (exc_ctx .exception .__context__ )
409+
410+ def test_registered_func_is_none (self ):
411+ """Calls explicitly registered function which is None"""
412+
413+ dispatcher = xmlrpc .server .SimpleXMLRPCDispatcher ()
414+ dispatcher .register_function (None , name = 'method' )
415+ with self .assertRaises (Exception , expected_regex = 'method' ):
416+ dispatcher ._dispatch ('method' , ('param' ,))
417+
418+ def test_instance_has_no_func (self ):
419+ """Attempts to call nonexistent function on a registered instance"""
420+
421+ dispatcher = xmlrpc .server .SimpleXMLRPCDispatcher ()
422+ dispatcher .register_instance (object ())
423+ with self .assertRaises (Exception , expected_regex = 'method' ):
424+ dispatcher ._dispatch ('method' , ('param' ,))
425+
426+ def test_cannot_locate_func (self ):
427+ """Calls a function that the dispatcher cannot locate"""
428+
429+ dispatcher = xmlrpc .server .SimpleXMLRPCDispatcher ()
430+ with self .assertRaises (Exception , expected_regex = 'method' ):
431+ dispatcher ._dispatch ('method' , ('param' ,))
432+
433+
346434class HelperTestCase (unittest .TestCase ):
347435 def test_escape (self ):
348436 self .assertEqual (xmlrpclib .escape ("a&b" ), "a&b" )
@@ -1313,7 +1401,7 @@ def test_main():
13131401 KeepaliveServerTestCase1 , KeepaliveServerTestCase2 ,
13141402 GzipServerTestCase , GzipUtilTestCase ,
13151403 MultiPathServerTestCase , ServerProxyTestCase , FailingServerTestCase ,
1316- CGIHandlerTestCase )
1404+ CGIHandlerTestCase , SimpleXMLRPCDispatcherTestCase )
13171405
13181406
13191407if __name__ == "__main__" :
0 commit comments