@@ -295,6 +295,94 @@ def run_server():
295
295
self .assertEqual (p .method (), 5 )
296
296
self .assertEqual (p .method (), 5 )
297
297
298
+
299
+ class SimpleXMLRPCDispatcherTestCase (unittest .TestCase ):
300
+ class DispatchExc (Exception ):
301
+ """Raised inside the dispatched functions when checking for
302
+ chained exceptions"""
303
+
304
+ def test_call_registered_func (self ):
305
+ """Calls explicitly registered function"""
306
+ # Makes sure any exception raised inside the function has no other
307
+ # exception chained to it
308
+
309
+ exp_params = 1 , 2 , 3
310
+
311
+ def dispatched_func (* params ):
312
+ raise self .DispatchExc (params )
313
+
314
+ dispatcher = xmlrpc .server .SimpleXMLRPCDispatcher ()
315
+ dispatcher .register_function (dispatched_func )
316
+ with self .assertRaises (self .DispatchExc ) as exc_ctx :
317
+ dispatcher ._dispatch ('dispatched_func' , exp_params )
318
+ self .assertEqual (exc_ctx .exception .args , (exp_params ,))
319
+ self .assertIsNone (exc_ctx .exception .__cause__ )
320
+ self .assertIsNone (exc_ctx .exception .__context__ )
321
+
322
+ def test_call_instance_func (self ):
323
+ """Calls a registered instance attribute as a function"""
324
+ # Makes sure any exception raised inside the function has no other
325
+ # exception chained to it
326
+
327
+ exp_params = 1 , 2 , 3
328
+
329
+ class DispatchedClass :
330
+ def dispatched_func (self , * params ):
331
+ raise SimpleXMLRPCDispatcherTestCase .DispatchExc (params )
332
+
333
+ dispatcher = xmlrpc .server .SimpleXMLRPCDispatcher ()
334
+ dispatcher .register_instance (DispatchedClass ())
335
+ with self .assertRaises (self .DispatchExc ) as exc_ctx :
336
+ dispatcher ._dispatch ('dispatched_func' , exp_params )
337
+ self .assertEqual (exc_ctx .exception .args , (exp_params ,))
338
+ self .assertIsNone (exc_ctx .exception .__cause__ )
339
+ self .assertIsNone (exc_ctx .exception .__context__ )
340
+
341
+ def test_call_dispatch_func (self ):
342
+ """Calls the registered instance's `_dispatch` function"""
343
+ # Makes sure any exception raised inside the function has no other
344
+ # exception chained to it
345
+
346
+ exp_method = 'method'
347
+ exp_params = 1 , 2 , 3
348
+
349
+ class TestInstance :
350
+ def _dispatch (self , method , params ):
351
+ raise SimpleXMLRPCDispatcherTestCase .DispatchExc (
352
+ method , params )
353
+
354
+ dispatcher = xmlrpc .server .SimpleXMLRPCDispatcher ()
355
+ dispatcher .register_instance (TestInstance ())
356
+ with self .assertRaises (self .DispatchExc ) as exc_ctx :
357
+ dispatcher ._dispatch (exp_method , exp_params )
358
+ self .assertEqual (exc_ctx .exception .args , (exp_method , exp_params ))
359
+ self .assertIsNone (exc_ctx .exception .__cause__ )
360
+ self .assertIsNone (exc_ctx .exception .__context__ )
361
+
362
+ def test_registered_func_is_none (self ):
363
+ """Calls explicitly registered function which is None"""
364
+
365
+ dispatcher = xmlrpc .server .SimpleXMLRPCDispatcher ()
366
+ dispatcher .register_function (None , name = 'method' )
367
+ with self .assertRaises (Exception , expected_regex = 'method' ):
368
+ dispatcher ._dispatch ('method' , ('param' ,))
369
+
370
+ def test_instance_has_no_func (self ):
371
+ """Attempts to call nonexistent function on a registered instance"""
372
+
373
+ dispatcher = xmlrpc .server .SimpleXMLRPCDispatcher ()
374
+ dispatcher .register_instance (object ())
375
+ with self .assertRaises (Exception , expected_regex = 'method' ):
376
+ dispatcher ._dispatch ('method' , ('param' ,))
377
+
378
+ def test_cannot_locate_func (self ):
379
+ """Calls a function that the dispatcher cannot locate"""
380
+
381
+ dispatcher = xmlrpc .server .SimpleXMLRPCDispatcher ()
382
+ with self .assertRaises (Exception , expected_regex = 'method' ):
383
+ dispatcher ._dispatch ('method' , ('param' ,))
384
+
385
+
298
386
class HelperTestCase (unittest .TestCase ):
299
387
def test_escape (self ):
300
388
self .assertEqual (xmlrpclib .escape ("a&b" ), "a&b" )
@@ -1265,7 +1353,7 @@ def test_main():
1265
1353
KeepaliveServerTestCase1 , KeepaliveServerTestCase2 ,
1266
1354
GzipServerTestCase , GzipUtilTestCase ,
1267
1355
MultiPathServerTestCase , ServerProxyTestCase , FailingServerTestCase ,
1268
- CGIHandlerTestCase )
1356
+ CGIHandlerTestCase , SimpleXMLRPCDispatcherTestCase )
1269
1357
1270
1358
1271
1359
if __name__ == "__main__" :
0 commit comments