| 1 |
""" |
|---|
| 2 |
Copyright (c) 2004, CherryPy Team (team@cherrypy.org) |
|---|
| 3 |
All rights reserved. |
|---|
| 4 |
|
|---|
| 5 |
Redistribution and use in source and binary forms, with or without modification, |
|---|
| 6 |
are permitted provided that the following conditions are met: |
|---|
| 7 |
|
|---|
| 8 |
* Redistributions of source code must retain the above copyright notice, |
|---|
| 9 |
this list of conditions and the following disclaimer. |
|---|
| 10 |
* Redistributions in binary form must reproduce the above copyright notice, |
|---|
| 11 |
this list of conditions and the following disclaimer in the documentation |
|---|
| 12 |
and/or other materials provided with the distribution. |
|---|
| 13 |
* Neither the name of the CherryPy Team nor the names of its contributors |
|---|
| 14 |
may be used to endorse or promote products derived from this software |
|---|
| 15 |
without specific prior written permission. |
|---|
| 16 |
|
|---|
| 17 |
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND |
|---|
| 18 |
ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED |
|---|
| 19 |
WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
|---|
| 20 |
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE |
|---|
| 21 |
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
|---|
| 22 |
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
|---|
| 23 |
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
|---|
| 24 |
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
|---|
| 25 |
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
|---|
| 26 |
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
|---|
| 27 |
""" |
|---|
| 28 |
|
|---|
| 29 |
import xmlrpclib |
|---|
| 30 |
from datetime import datetime |
|---|
| 31 |
import cherrypy |
|---|
| 32 |
|
|---|
| 33 |
class Root: |
|---|
| 34 |
def index(self): |
|---|
| 35 |
return "I'm a standard index!" |
|---|
| 36 |
index.exposed = True |
|---|
| 37 |
|
|---|
| 38 |
|
|---|
| 39 |
class XmlRpc: |
|---|
| 40 |
def return_single_item_list(self): |
|---|
| 41 |
return [42] |
|---|
| 42 |
return_single_item_list.exposed = True |
|---|
| 43 |
|
|---|
| 44 |
def return_string(self): |
|---|
| 45 |
return "here is a string" |
|---|
| 46 |
return_string.exposed = True |
|---|
| 47 |
|
|---|
| 48 |
def return_tuple(self): |
|---|
| 49 |
return ('here', 'is', 1, 'tuple') |
|---|
| 50 |
return_tuple.exposed = True |
|---|
| 51 |
|
|---|
| 52 |
def return_dict(self): |
|---|
| 53 |
return dict(a=1, b=2, c=3) |
|---|
| 54 |
return_dict.exposed = True |
|---|
| 55 |
|
|---|
| 56 |
def return_composite(self): |
|---|
| 57 |
return dict(a=1,z=26), 'hi', ['welcome', 'friend'] |
|---|
| 58 |
return_composite.exposed = True |
|---|
| 59 |
|
|---|
| 60 |
def return_int(self): |
|---|
| 61 |
return 42 |
|---|
| 62 |
return_int.exposed = True |
|---|
| 63 |
|
|---|
| 64 |
def return_float(self): |
|---|
| 65 |
return 3.14 |
|---|
| 66 |
return_float.exposed = True |
|---|
| 67 |
|
|---|
| 68 |
def return_datetime(self): |
|---|
| 69 |
return xmlrpclib.DateTime((2003, 10, 7, 8, 1, 0, 1, 280, -1)) |
|---|
| 70 |
return_datetime.exposed = True |
|---|
| 71 |
|
|---|
| 72 |
def return_boolean(self): |
|---|
| 73 |
return True |
|---|
| 74 |
return_boolean.exposed = True |
|---|
| 75 |
|
|---|
| 76 |
def test_argument_passing(self, num): |
|---|
| 77 |
return num * 2 |
|---|
| 78 |
test_argument_passing.exposed = True |
|---|
| 79 |
|
|---|
| 80 |
cherrypy.root = Root() |
|---|
| 81 |
cherrypy.root.xmlrpc = XmlRpc() |
|---|
| 82 |
|
|---|
| 83 |
import helper |
|---|
| 84 |
|
|---|
| 85 |
cherrypy.config.update({ |
|---|
| 86 |
'global': {'server.logToScreen': False, |
|---|
| 87 |
'server.environment': 'production', |
|---|
| 88 |
'server.showTracebacks': True, |
|---|
| 89 |
'server.socketHost': helper.CPWebCase.HOST, |
|---|
| 90 |
'server.socketPort': helper.CPWebCase.PORT, |
|---|
| 91 |
}, |
|---|
| 92 |
'/xmlrpc': |
|---|
| 93 |
{'xmlRpcFilter.on':True} |
|---|
| 94 |
}) |
|---|
| 95 |
|
|---|
| 96 |
|
|---|
| 97 |
|
|---|
| 98 |
class XmlRpcFilterTest(helper.CPWebCase): |
|---|
| 99 |
def testXmlRpcFilter(self): |
|---|
| 100 |
|
|---|
| 101 |
proxy = xmlrpclib.ServerProxy('http://localhost:%s/xmlrpc/' % (self.PORT)) |
|---|
| 102 |
|
|---|
| 103 |
self.assertEqual(proxy.return_single_item_list(), |
|---|
| 104 |
[42] |
|---|
| 105 |
) |
|---|
| 106 |
self.assertEqual(proxy.return_string(), |
|---|
| 107 |
"here is a string" |
|---|
| 108 |
) |
|---|
| 109 |
self.assertEqual(proxy.return_tuple(), |
|---|
| 110 |
list(('here', 'is', 1, 'tuple')) |
|---|
| 111 |
) |
|---|
| 112 |
self.assertEqual(proxy.return_dict(), |
|---|
| 113 |
{'a': 1, 'c': 3, 'b': 2} |
|---|
| 114 |
) |
|---|
| 115 |
self.assertEqual(proxy.return_composite(), |
|---|
| 116 |
[{'a': 1, 'z': 26}, 'hi', ['welcome', 'friend']] |
|---|
| 117 |
) |
|---|
| 118 |
self.assertEqual(proxy.return_int(), |
|---|
| 119 |
42 |
|---|
| 120 |
) |
|---|
| 121 |
self.assertEqual(proxy.return_float(), |
|---|
| 122 |
3.14 |
|---|
| 123 |
) |
|---|
| 124 |
self.assertEqual(proxy.return_datetime(), |
|---|
| 125 |
xmlrpclib.DateTime((2003, 10, 7, 8, 1, 0, 1, 280, -1)) |
|---|
| 126 |
) |
|---|
| 127 |
self.assertEqual(proxy.return_boolean(), |
|---|
| 128 |
True |
|---|
| 129 |
) |
|---|
| 130 |
self.assertEqual(proxy.test_argument_passing(22), |
|---|
| 131 |
22 * 2 |
|---|
| 132 |
) |
|---|
| 133 |
|
|---|
| 134 |
|
|---|
| 135 |
if __name__ == '__main__': |
|---|
| 136 |
from cherrypy import _cpwsgi |
|---|
| 137 |
serverClass = _cpwsgi.WSGIServer |
|---|
| 138 |
helper.testmain(serverClass) |
|---|
| 139 |
|
|---|