| 1 |
import test |
|---|
| 2 |
test.prefer_parent_path() |
|---|
| 3 |
import xmlrpclib |
|---|
| 4 |
|
|---|
| 5 |
|
|---|
| 6 |
def setup_server(): |
|---|
| 7 |
import cherrypy |
|---|
| 8 |
from cherrypy import _cptools |
|---|
| 9 |
|
|---|
| 10 |
class Root: |
|---|
| 11 |
def index(self): |
|---|
| 12 |
return "I'm a standard index!" |
|---|
| 13 |
index.exposed = True |
|---|
| 14 |
|
|---|
| 15 |
|
|---|
| 16 |
class XmlRpc(_cptools.XMLRPCController): |
|---|
| 17 |
|
|---|
| 18 |
def return_single_item_list(self): |
|---|
| 19 |
return [42] |
|---|
| 20 |
return_single_item_list.exposed = True |
|---|
| 21 |
|
|---|
| 22 |
def return_string(self): |
|---|
| 23 |
return "here is a string" |
|---|
| 24 |
return_string.exposed = True |
|---|
| 25 |
|
|---|
| 26 |
def return_tuple(self): |
|---|
| 27 |
return ('here', 'is', 1, 'tuple') |
|---|
| 28 |
return_tuple.exposed = True |
|---|
| 29 |
|
|---|
| 30 |
def return_dict(self): |
|---|
| 31 |
return dict(a=1, b=2, c=3) |
|---|
| 32 |
return_dict.exposed = True |
|---|
| 33 |
|
|---|
| 34 |
def return_composite(self): |
|---|
| 35 |
return dict(a=1,z=26), 'hi', ['welcome', 'friend'] |
|---|
| 36 |
return_composite.exposed = True |
|---|
| 37 |
|
|---|
| 38 |
def return_int(self): |
|---|
| 39 |
return 42 |
|---|
| 40 |
return_int.exposed = True |
|---|
| 41 |
|
|---|
| 42 |
def return_float(self): |
|---|
| 43 |
return 3.14 |
|---|
| 44 |
return_float.exposed = True |
|---|
| 45 |
|
|---|
| 46 |
def return_datetime(self): |
|---|
| 47 |
return xmlrpclib.DateTime((2003, 10, 7, 8, 1, 0, 1, 280, -1)) |
|---|
| 48 |
return_datetime.exposed = True |
|---|
| 49 |
|
|---|
| 50 |
def return_boolean(self): |
|---|
| 51 |
return True |
|---|
| 52 |
return_boolean.exposed = True |
|---|
| 53 |
|
|---|
| 54 |
def test_argument_passing(self, num): |
|---|
| 55 |
return num * 2 |
|---|
| 56 |
test_argument_passing.exposed = True |
|---|
| 57 |
|
|---|
| 58 |
root = Root() |
|---|
| 59 |
root.xmlrpc = XmlRpc() |
|---|
| 60 |
cherrypy.tree.mount(root) |
|---|
| 61 |
cherrypy.config.update({ |
|---|
| 62 |
'log_to_screen': False, |
|---|
| 63 |
'environment': 'production', |
|---|
| 64 |
'show_tracebacks': True, |
|---|
| 65 |
}) |
|---|
| 66 |
|
|---|
| 67 |
|
|---|
| 68 |
import helper |
|---|
| 69 |
|
|---|
| 70 |
class XmlRpcTest(helper.CPWebCase): |
|---|
| 71 |
def testXmlRpc(self): |
|---|
| 72 |
|
|---|
| 73 |
|
|---|
| 74 |
url = 'http://localhost:%s/xmlrpc/' % (self.PORT) |
|---|
| 75 |
proxy = xmlrpclib.ServerProxy(url) |
|---|
| 76 |
|
|---|
| 77 |
|
|---|
| 78 |
self.assertEqual(proxy.return_single_item_list(), [42]) |
|---|
| 79 |
self.assertNotEqual(proxy.return_single_item_list(), 'one bazillion') |
|---|
| 80 |
self.assertEqual(proxy.return_string(), "here is a string") |
|---|
| 81 |
self.assertEqual(proxy.return_tuple(), list(('here', 'is', 1, 'tuple'))) |
|---|
| 82 |
self.assertEqual(proxy.return_dict(), {'a': 1, 'c': 3, 'b': 2}) |
|---|
| 83 |
self.assertEqual(proxy.return_composite(), |
|---|
| 84 |
[{'a': 1, 'z': 26}, 'hi', ['welcome', 'friend']]) |
|---|
| 85 |
self.assertEqual(proxy.return_int(), 42) |
|---|
| 86 |
self.assertEqual(proxy.return_float(), 3.14) |
|---|
| 87 |
self.assertEqual(proxy.return_datetime(), |
|---|
| 88 |
xmlrpclib.DateTime((2003, 10, 7, 8, 1, 0, 1, 280, -1))) |
|---|
| 89 |
self.assertEqual(proxy.return_boolean(), True) |
|---|
| 90 |
self.assertEqual(proxy.test_argument_passing(22), 22 * 2) |
|---|
| 91 |
|
|---|
| 92 |
|
|---|
| 93 |
try: |
|---|
| 94 |
proxy.test_argument_passing({}) |
|---|
| 95 |
except Exception, x: |
|---|
| 96 |
self.assertEqual(x.__class__, xmlrpclib.Fault) |
|---|
| 97 |
self.assertEqual(x.faultString, ("unsupported operand type(s) " |
|---|
| 98 |
"for *: 'dict' and 'int'")) |
|---|
| 99 |
else: |
|---|
| 100 |
self.fail("Expected xmlrpclib.Fault") |
|---|
| 101 |
|
|---|
| 102 |
|
|---|
| 103 |
|
|---|
| 104 |
try: |
|---|
| 105 |
proxy.non_method() |
|---|
| 106 |
except Exception, x: |
|---|
| 107 |
self.assertEqual(x.__class__, xmlrpclib.Fault) |
|---|
| 108 |
self.assertEqual(x.faultString, 'method "non_method" is not supported') |
|---|
| 109 |
else: |
|---|
| 110 |
self.fail("Expected xmlrpclib.Fault") |
|---|
| 111 |
|
|---|
| 112 |
|
|---|
| 113 |
if __name__ == '__main__': |
|---|
| 114 |
setup_server() |
|---|
| 115 |
helper.testmain() |
|---|
| 116 |
|
|---|