Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 1 | import base64 |
Fred Drake | ba613c3 | 2005-02-10 18:33:30 +0000 | [diff] [blame] | 2 | import datetime |
Skip Montanaro | 3e7bba9 | 2001-10-19 16:06:52 +0000 | [diff] [blame] | 3 | import sys |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 4 | import time |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 5 | import unittest |
| 6 | import xmlrpclib |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 7 | import SimpleXMLRPCServer |
| 8 | import threading |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 9 | import mimetools |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 10 | import httplib |
| 11 | import socket |
| 12 | import os |
Barry Warsaw | 04f357c | 2002-07-23 19:04:11 +0000 | [diff] [blame] | 13 | from test import test_support |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 14 | |
| 15 | alist = [{'astring': 'foo@bar.baz.spam', |
| 16 | 'afloat': 7283.43, |
Skip Montanaro | 3e7bba9 | 2001-10-19 16:06:52 +0000 | [diff] [blame] | 17 | 'anint': 2**20, |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 18 | 'ashortlong': 2, |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 19 | 'anotherlist': ['.zyx.41'], |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 20 | 'abase64': xmlrpclib.Binary(b"my dog has fleas"), |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 21 | 'boolean': False, |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 22 | 'unicode': '\u4000\u6000\u8000', |
| 23 | 'ukey\u4000': 'regular value', |
Fred Drake | ba613c3 | 2005-02-10 18:33:30 +0000 | [diff] [blame] | 24 | 'datetime1': xmlrpclib.DateTime('20050210T11:41:23'), |
| 25 | 'datetime2': xmlrpclib.DateTime( |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 26 | (2005, 2, 10, 11, 41, 23, 0, 1, -1)), |
Fred Drake | ba613c3 | 2005-02-10 18:33:30 +0000 | [diff] [blame] | 27 | 'datetime3': xmlrpclib.DateTime( |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 28 | datetime.datetime(2005, 2, 10, 11, 41, 23)), |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 29 | }] |
| 30 | |
| 31 | class XMLRPCTestCase(unittest.TestCase): |
| 32 | |
| 33 | def test_dump_load(self): |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 34 | dump = xmlrpclib.dumps((alist,)) |
| 35 | load = xmlrpclib.loads(dump) |
| 36 | self.assertEquals(alist, load[0][0]) |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 37 | |
Fred Drake | ba613c3 | 2005-02-10 18:33:30 +0000 | [diff] [blame] | 38 | def test_dump_bare_datetime(self): |
Skip Montanaro | 174dd22 | 2005-05-14 20:54:16 +0000 | [diff] [blame] | 39 | # This checks that an unwrapped datetime.date object can be handled |
| 40 | # by the marshalling code. This can't be done via test_dump_load() |
| 41 | # since with use_datetime set to 1 the unmarshaller would create |
| 42 | # datetime objects for the 'datetime[123]' keys as well |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 43 | dt = datetime.datetime(2005, 2, 10, 11, 41, 23) |
Fred Drake | ba613c3 | 2005-02-10 18:33:30 +0000 | [diff] [blame] | 44 | s = xmlrpclib.dumps((dt,)) |
Skip Montanaro | 174dd22 | 2005-05-14 20:54:16 +0000 | [diff] [blame] | 45 | (newdt,), m = xmlrpclib.loads(s, use_datetime=1) |
| 46 | self.assertEquals(newdt, dt) |
Fred Drake | ba613c3 | 2005-02-10 18:33:30 +0000 | [diff] [blame] | 47 | self.assertEquals(m, None) |
| 48 | |
Skip Montanaro | 174dd22 | 2005-05-14 20:54:16 +0000 | [diff] [blame] | 49 | (newdt,), m = xmlrpclib.loads(s, use_datetime=0) |
| 50 | self.assertEquals(newdt, xmlrpclib.DateTime('20050210T11:41:23')) |
| 51 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 52 | def test_datetime_before_1900(self): |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame^] | 53 | # same as before but with a date before 1900 |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 54 | dt = datetime.datetime(1, 2, 10, 11, 41, 23) |
| 55 | s = xmlrpclib.dumps((dt,)) |
| 56 | (newdt,), m = xmlrpclib.loads(s, use_datetime=1) |
| 57 | self.assertEquals(newdt, dt) |
| 58 | self.assertEquals(m, None) |
| 59 | |
| 60 | (newdt,), m = xmlrpclib.loads(s, use_datetime=0) |
| 61 | self.assertEquals(newdt, xmlrpclib.DateTime('00010210T11:41:23')) |
| 62 | |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 63 | def test_cmp_datetime_DateTime(self): |
| 64 | now = datetime.datetime.now() |
| 65 | dt = xmlrpclib.DateTime(now.timetuple()) |
| 66 | self.assert_(dt == now) |
| 67 | self.assert_(now == dt) |
| 68 | then = now + datetime.timedelta(seconds=4) |
| 69 | self.assert_(then >= dt) |
| 70 | self.assert_(dt < then) |
Skip Montanaro | 174dd22 | 2005-05-14 20:54:16 +0000 | [diff] [blame] | 71 | |
Andrew M. Kuchling | bdb3901 | 2005-12-04 19:11:17 +0000 | [diff] [blame] | 72 | def test_bug_1164912 (self): |
| 73 | d = xmlrpclib.DateTime() |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 74 | ((new_d,), dummy) = xmlrpclib.loads(xmlrpclib.dumps((d,), |
Andrew M. Kuchling | bdb3901 | 2005-12-04 19:11:17 +0000 | [diff] [blame] | 75 | methodresponse=True)) |
| 76 | self.assert_(isinstance(new_d.value, str)) |
| 77 | |
| 78 | # Check that the output of dumps() is still an 8-bit string |
| 79 | s = xmlrpclib.dumps((new_d,), methodresponse=True) |
| 80 | self.assert_(isinstance(s, str)) |
| 81 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 82 | def test_newstyle_class(self): |
| 83 | class T(object): |
| 84 | pass |
| 85 | t = T() |
| 86 | t.x = 100 |
| 87 | t.y = "Hello" |
| 88 | ((t2,), dummy) = xmlrpclib.loads(xmlrpclib.dumps((t,))) |
| 89 | self.assertEquals(t2, t.__dict__) |
| 90 | |
Skip Montanaro | 3e7bba9 | 2001-10-19 16:06:52 +0000 | [diff] [blame] | 91 | def test_dump_big_long(self): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 92 | self.assertRaises(OverflowError, xmlrpclib.dumps, (2**99,)) |
Skip Montanaro | 3e7bba9 | 2001-10-19 16:06:52 +0000 | [diff] [blame] | 93 | |
| 94 | def test_dump_bad_dict(self): |
| 95 | self.assertRaises(TypeError, xmlrpclib.dumps, ({(1,2,3): 1},)) |
| 96 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 97 | def test_dump_recursive_seq(self): |
| 98 | l = [1,2,3] |
| 99 | t = [3,4,5,l] |
| 100 | l.append(t) |
| 101 | self.assertRaises(TypeError, xmlrpclib.dumps, (l,)) |
| 102 | |
| 103 | def test_dump_recursive_dict(self): |
| 104 | d = {'1':1, '2':1} |
| 105 | t = {'3':3, 'd':d} |
| 106 | d['t'] = t |
| 107 | self.assertRaises(TypeError, xmlrpclib.dumps, (d,)) |
| 108 | |
Skip Montanaro | 3e7bba9 | 2001-10-19 16:06:52 +0000 | [diff] [blame] | 109 | def test_dump_big_int(self): |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 110 | if sys.maxsize > 2**31-1: |
Skip Montanaro | 3e7bba9 | 2001-10-19 16:06:52 +0000 | [diff] [blame] | 111 | self.assertRaises(OverflowError, xmlrpclib.dumps, |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 112 | (int(2**34),)) |
Skip Montanaro | 3e7bba9 | 2001-10-19 16:06:52 +0000 | [diff] [blame] | 113 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 114 | xmlrpclib.dumps((xmlrpclib.MAXINT, xmlrpclib.MININT)) |
| 115 | self.assertRaises(OverflowError, xmlrpclib.dumps, (xmlrpclib.MAXINT+1,)) |
| 116 | self.assertRaises(OverflowError, xmlrpclib.dumps, (xmlrpclib.MININT-1,)) |
| 117 | |
| 118 | def dummy_write(s): |
| 119 | pass |
| 120 | |
| 121 | m = xmlrpclib.Marshaller() |
| 122 | m.dump_int(xmlrpclib.MAXINT, dummy_write) |
| 123 | m.dump_int(xmlrpclib.MININT, dummy_write) |
| 124 | self.assertRaises(OverflowError, m.dump_int, xmlrpclib.MAXINT+1, dummy_write) |
| 125 | self.assertRaises(OverflowError, m.dump_int, xmlrpclib.MININT-1, dummy_write) |
| 126 | |
| 127 | |
Andrew M. Kuchling | 0b85203 | 2003-04-25 00:27:24 +0000 | [diff] [blame] | 128 | def test_dump_none(self): |
| 129 | value = alist + [None] |
| 130 | arg1 = (alist + [None],) |
| 131 | strg = xmlrpclib.dumps(arg1, allow_none=True) |
| 132 | self.assertEquals(value, |
| 133 | xmlrpclib.loads(strg)[0][0]) |
| 134 | self.assertRaises(TypeError, xmlrpclib.dumps, (arg1,)) |
| 135 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 136 | |
| 137 | class HelperTestCase(unittest.TestCase): |
| 138 | def test_escape(self): |
| 139 | self.assertEqual(xmlrpclib.escape("a&b"), "a&b") |
| 140 | self.assertEqual(xmlrpclib.escape("a<b"), "a<b") |
| 141 | self.assertEqual(xmlrpclib.escape("a>b"), "a>b") |
| 142 | |
| 143 | class FaultTestCase(unittest.TestCase): |
| 144 | def test_repr(self): |
| 145 | f = xmlrpclib.Fault(42, 'Test Fault') |
| 146 | self.assertEqual(repr(f), "<Fault 42: 'Test Fault'>") |
| 147 | self.assertEqual(repr(f), str(f)) |
| 148 | |
| 149 | def test_dump_fault(self): |
| 150 | f = xmlrpclib.Fault(42, 'Test Fault') |
| 151 | s = xmlrpclib.dumps((f,)) |
| 152 | (newf,), m = xmlrpclib.loads(s) |
| 153 | self.assertEquals(newf, {'faultCode': 42, 'faultString': 'Test Fault'}) |
| 154 | self.assertEquals(m, None) |
| 155 | |
| 156 | s = xmlrpclib.Marshaller().dumps(f) |
| 157 | self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, s) |
| 158 | |
Christian Heimes | eda9e2b | 2007-12-08 22:32:47 +0000 | [diff] [blame] | 159 | def test_dotted_attribute(self): |
| 160 | # this will raise AttirebuteError because code don't want us to use |
| 161 | # private methods |
| 162 | self.assertRaises(AttributeError, |
| 163 | SimpleXMLRPCServer.resolve_dotted_attribute, str, '__add') |
| 164 | |
| 165 | self.assert_(SimpleXMLRPCServer.resolve_dotted_attribute(str, 'title')) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 166 | |
| 167 | class DateTimeTestCase(unittest.TestCase): |
| 168 | def test_default(self): |
| 169 | t = xmlrpclib.DateTime() |
| 170 | |
| 171 | def test_time(self): |
| 172 | d = 1181399930.036952 |
| 173 | t = xmlrpclib.DateTime(d) |
| 174 | self.assertEqual(str(t), time.strftime("%Y%m%dT%H:%M:%S", time.localtime(d))) |
| 175 | |
| 176 | def test_time_tuple(self): |
| 177 | d = (2007,6,9,10,38,50,5,160,0) |
| 178 | t = xmlrpclib.DateTime(d) |
| 179 | self.assertEqual(str(t), '20070609T10:38:50') |
| 180 | |
| 181 | def test_time_struct(self): |
| 182 | d = time.localtime(1181399930.036952) |
| 183 | t = xmlrpclib.DateTime(d) |
| 184 | self.assertEqual(str(t), time.strftime("%Y%m%dT%H:%M:%S", d)) |
| 185 | |
| 186 | def test_datetime_datetime(self): |
| 187 | d = datetime.datetime(2007,1,2,3,4,5) |
| 188 | t = xmlrpclib.DateTime(d) |
| 189 | self.assertEqual(str(t), '20070102T03:04:05') |
| 190 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 191 | def test_repr(self): |
| 192 | d = datetime.datetime(2007,1,2,3,4,5) |
| 193 | t = xmlrpclib.DateTime(d) |
| 194 | val ="<DateTime '20070102T03:04:05' at %x>" % id(t) |
| 195 | self.assertEqual(repr(t), val) |
| 196 | |
| 197 | def test_decode(self): |
| 198 | d = ' 20070908T07:11:13 ' |
| 199 | t1 = xmlrpclib.DateTime() |
| 200 | t1.decode(d) |
| 201 | tref = xmlrpclib.DateTime(datetime.datetime(2007,9,8,7,11,13)) |
| 202 | self.assertEqual(t1, tref) |
| 203 | |
| 204 | t2 = xmlrpclib._datetime(d) |
| 205 | self.assertEqual(t1, tref) |
| 206 | |
| 207 | class BinaryTestCase(unittest.TestCase): |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 208 | |
| 209 | # XXX What should str(Binary(b"\xff")) return? I'm chosing "\xff" |
| 210 | # for now (i.e. interpreting the binary data as Latin-1-encoded |
| 211 | # text). But this feels very unsatisfactory. Perhaps we should |
| 212 | # only define repr(), and return r"Binary(b'\xff')" instead? |
| 213 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 214 | def test_default(self): |
| 215 | t = xmlrpclib.Binary() |
| 216 | self.assertEqual(str(t), '') |
| 217 | |
| 218 | def test_string(self): |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 219 | d = b'\x01\x02\x03abc123\xff\xfe' |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 220 | t = xmlrpclib.Binary(d) |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 221 | self.assertEqual(str(t), str(d, "latin-1")) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 222 | |
| 223 | def test_decode(self): |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 224 | d = b'\x01\x02\x03abc123\xff\xfe' |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 225 | de = base64.encodestring(d) |
| 226 | t1 = xmlrpclib.Binary() |
| 227 | t1.decode(de) |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 228 | self.assertEqual(str(t1), str(d, "latin-1")) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 229 | |
| 230 | t2 = xmlrpclib._binary(de) |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 231 | self.assertEqual(str(t2), str(d, "latin-1")) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 232 | |
| 233 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 234 | PORT = None |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 235 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 236 | # The evt is set twice. First when the server is ready to serve. |
| 237 | # Second when the server has been shutdown. The user must clear |
| 238 | # the event after it has been set the first time to catch the second set. |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 239 | def http_server(evt, numrequests): |
| 240 | class TestInstanceClass: |
| 241 | def div(self, x, y): |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 242 | return x // y |
| 243 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 244 | def _methodHelp(self, name): |
| 245 | if name == 'div': |
| 246 | return 'This is the div function' |
| 247 | |
| 248 | def my_function(): |
| 249 | '''This is my function''' |
| 250 | return True |
| 251 | |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 252 | class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer): |
| 253 | def get_request(self): |
| 254 | # Ensure the socket is always non-blocking. On Linux, socket |
| 255 | # attributes are not inherited like they are on *BSD and Windows. |
| 256 | s, port = self.socket.accept() |
| 257 | s.setblocking(True) |
| 258 | return s, port |
| 259 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 260 | try: |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 261 | serv = MyXMLRPCServer(("localhost", 0), |
| 262 | logRequests=False, bind_and_activate=False) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 263 | serv.server_bind() |
| 264 | global PORT |
| 265 | PORT = serv.socket.getsockname()[1] |
| 266 | serv.server_activate() |
| 267 | serv.register_introspection_functions() |
| 268 | serv.register_multicall_functions() |
| 269 | serv.register_function(pow) |
| 270 | serv.register_function(lambda x,y: x+y, 'add') |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 271 | serv.register_function(my_function) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 272 | serv.register_instance(TestInstanceClass()) |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 273 | evt.set() |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 274 | |
| 275 | # handle up to 'numrequests' requests |
| 276 | while numrequests > 0: |
| 277 | serv.handle_request() |
| 278 | numrequests -= 1 |
| 279 | |
| 280 | except socket.timeout: |
| 281 | pass |
| 282 | finally: |
| 283 | serv.socket.close() |
| 284 | PORT = None |
| 285 | evt.set() |
| 286 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 287 | # This function prevents errors like: |
| 288 | # <ProtocolError for localhost:57527/RPC2: 500 Internal Server Error> |
| 289 | def is_unavailable_exception(e): |
| 290 | '''Returns True if the given ProtocolError is the product of a server-side |
| 291 | exception caused by the 'temporarily unavailable' response sometimes |
| 292 | given by operations on non-blocking sockets.''' |
| 293 | |
| 294 | # sometimes we get a -1 error code and/or empty headers |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 295 | try: |
| 296 | if e.errcode == -1 or e.headers is None: |
| 297 | return True |
| 298 | exc_mess = e.headers.get('X-exception') |
| 299 | except AttributeError: |
| 300 | # Ignore socket.errors here. |
| 301 | exc_mess = str(e) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 302 | |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 303 | if exc_mess and 'temporarily unavailable' in exc_mess.lower(): |
| 304 | return True |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 305 | |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 306 | class SimpleServerTestCase(unittest.TestCase): |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 307 | def setUp(self): |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 308 | # enable traceback reporting |
| 309 | SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = True |
| 310 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 311 | self.evt = threading.Event() |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 312 | # start server thread to handle requests |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 313 | serv_args = (self.evt, 1) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 314 | threading.Thread(target=http_server, args=serv_args).start() |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 315 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 316 | # wait for the server to be ready |
| 317 | self.evt.wait() |
| 318 | self.evt.clear() |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 319 | |
| 320 | def tearDown(self): |
| 321 | # wait on the server thread to terminate |
Christian Heimes | 576bf65 | 2007-12-08 17:09:18 +0000 | [diff] [blame] | 322 | self.evt.wait(4.0) |
| 323 | if not self.evt.isSet(): |
| 324 | self.evt.set() |
| 325 | stop_serving() |
| 326 | raise RuntimeError("timeout reached, test has failed") |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 327 | |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 328 | # disable traceback reporting |
| 329 | SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = False |
| 330 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 331 | def test_simple1(self): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 332 | try: |
| 333 | p = xmlrpclib.ServerProxy('http://localhost:%d' % PORT) |
| 334 | self.assertEqual(p.pow(6,8), 6**8) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 335 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 336 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 337 | if not is_unavailable_exception(e): |
| 338 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 339 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 340 | |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 341 | # [ch] The test 404 is causing lots of false alarms. |
| 342 | def XXXtest_404(self): |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 343 | # send POST with httplib, it should return 404 header and |
| 344 | # 'Not Found' message. |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 345 | conn = httplib.HTTPConnection('localhost', PORT) |
Christian Heimes | 99d73f2 | 2007-12-08 16:13:06 +0000 | [diff] [blame] | 346 | conn.request('POST', '/this-is-not-valid') |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 347 | response = conn.getresponse() |
| 348 | conn.close() |
| 349 | |
| 350 | self.assertEqual(response.status, 404) |
| 351 | self.assertEqual(response.reason, 'Not Found') |
| 352 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 353 | def test_introspection1(self): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 354 | try: |
| 355 | p = xmlrpclib.ServerProxy('http://localhost:%d' % PORT) |
| 356 | meth = p.system.listMethods() |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 357 | expected_methods = set(['pow', 'div', 'my_function', 'add', |
| 358 | 'system.listMethods', 'system.methodHelp', |
| 359 | 'system.methodSignature', 'system.multicall']) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 360 | self.assertEqual(set(meth), expected_methods) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 361 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 362 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 363 | if not is_unavailable_exception(e): |
| 364 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 365 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 366 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 367 | |
| 368 | def test_introspection2(self): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 369 | try: |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 370 | # test _methodHelp() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 371 | p = xmlrpclib.ServerProxy('http://localhost:%d' % PORT) |
| 372 | divhelp = p.system.methodHelp('div') |
| 373 | self.assertEqual(divhelp, 'This is the div function') |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 374 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 375 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 376 | if not is_unavailable_exception(e): |
| 377 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 378 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 379 | |
| 380 | def test_introspection3(self): |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 381 | try: |
| 382 | # test native doc |
| 383 | p = xmlrpclib.ServerProxy('http://localhost:%d' % PORT) |
| 384 | myfunction = p.system.methodHelp('my_function') |
| 385 | self.assertEqual(myfunction, 'This is my function') |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 386 | except (xmlrpclib.ProtocolError, socket.error) as e: |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 387 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 388 | if not is_unavailable_exception(e): |
| 389 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 390 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 391 | |
| 392 | def test_introspection4(self): |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 393 | # the SimpleXMLRPCServer doesn't support signatures, but |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 394 | # at least check that we can try making the call |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 395 | try: |
| 396 | p = xmlrpclib.ServerProxy('http://localhost:%d' % PORT) |
| 397 | divsig = p.system.methodSignature('div') |
| 398 | self.assertEqual(divsig, 'signatures not supported') |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 399 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 400 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 401 | if not is_unavailable_exception(e): |
| 402 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 403 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 404 | |
| 405 | def test_multicall(self): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 406 | try: |
| 407 | p = xmlrpclib.ServerProxy('http://localhost:%d' % PORT) |
| 408 | multicall = xmlrpclib.MultiCall(p) |
| 409 | multicall.add(2,3) |
| 410 | multicall.pow(6,8) |
| 411 | multicall.div(127,42) |
| 412 | add_result, pow_result, div_result = multicall() |
| 413 | self.assertEqual(add_result, 2+3) |
| 414 | self.assertEqual(pow_result, 6**8) |
| 415 | self.assertEqual(div_result, 127//42) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 416 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 417 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 418 | if not is_unavailable_exception(e): |
| 419 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 420 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 421 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 422 | def test_non_existing_multicall(self): |
| 423 | try: |
| 424 | p = xmlrpclib.ServerProxy('http://localhost:%d' % PORT) |
| 425 | multicall = xmlrpclib.MultiCall(p) |
| 426 | multicall.this_is_not_exists() |
| 427 | result = multicall() |
| 428 | |
| 429 | # result.results contains; |
Martin v. Löwis | 250ad61 | 2008-04-07 05:43:42 +0000 | [diff] [blame] | 430 | # [{'faultCode': 1, 'faultString': '<class \'exceptions.Exception\'>:' |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 431 | # 'method "this_is_not_exists" is not supported'>}] |
| 432 | |
| 433 | self.assertEqual(result.results[0]['faultCode'], 1) |
| 434 | self.assertEqual(result.results[0]['faultString'], |
Martin v. Löwis | 250ad61 | 2008-04-07 05:43:42 +0000 | [diff] [blame] | 435 | '<class \'Exception\'>:method "this_is_not_exists" ' |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 436 | 'is not supported') |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 437 | except (xmlrpclib.ProtocolError, socket.error) as e: |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 438 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 439 | if not is_unavailable_exception(e): |
| 440 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 441 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 442 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 443 | def test_dotted_attribute(self): |
| 444 | # Raises an AttributeError because private methods are not allowed. |
| 445 | self.assertRaises(AttributeError, |
| 446 | SimpleXMLRPCServer.resolve_dotted_attribute, str, '__add') |
| 447 | |
| 448 | self.assert_(SimpleXMLRPCServer.resolve_dotted_attribute(str, 'title')) |
| 449 | # Get the test to run faster by sending a request with test_simple1. |
| 450 | # This avoids waiting for the socket timeout. |
| 451 | self.test_simple1() |
| 452 | |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 453 | # This is a contrived way to make a failure occur on the server side |
| 454 | # in order to test the _send_traceback_header flag on the server |
| 455 | class FailingMessageClass(mimetools.Message): |
| 456 | def __getitem__(self, key): |
| 457 | key = key.lower() |
| 458 | if key == 'content-length': |
| 459 | return 'I am broken' |
| 460 | return mimetools.Message.__getitem__(self, key) |
| 461 | |
| 462 | |
| 463 | class FailingServerTestCase(unittest.TestCase): |
| 464 | def setUp(self): |
| 465 | self.evt = threading.Event() |
| 466 | # start server thread to handle requests |
Guido van Rossum | 15c9746 | 2007-11-02 16:10:06 +0000 | [diff] [blame] | 467 | serv_args = (self.evt, 1) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 468 | threading.Thread(target=http_server, args=serv_args).start() |
| 469 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 470 | # wait for the server to be ready |
| 471 | self.evt.wait() |
| 472 | self.evt.clear() |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 473 | |
| 474 | def tearDown(self): |
| 475 | # wait on the server thread to terminate |
| 476 | self.evt.wait() |
| 477 | # reset flag |
| 478 | SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = False |
| 479 | # reset message class |
| 480 | SimpleXMLRPCServer.SimpleXMLRPCRequestHandler.MessageClass = mimetools.Message |
| 481 | |
| 482 | def test_basic(self): |
| 483 | # check that flag is false by default |
| 484 | flagval = SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header |
| 485 | self.assertEqual(flagval, False) |
| 486 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 487 | # enable traceback reporting |
| 488 | SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = True |
| 489 | |
| 490 | # test a call that shouldn't fail just as a smoke test |
| 491 | try: |
| 492 | p = xmlrpclib.ServerProxy('http://localhost:%d' % PORT) |
| 493 | self.assertEqual(p.pow(6,8), 6**8) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 494 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 495 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 496 | if not is_unavailable_exception(e): |
| 497 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 498 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 499 | |
| 500 | def test_fail_no_info(self): |
| 501 | # use the broken message class |
| 502 | SimpleXMLRPCServer.SimpleXMLRPCRequestHandler.MessageClass = FailingMessageClass |
| 503 | |
| 504 | try: |
| 505 | p = xmlrpclib.ServerProxy('http://localhost:%d' % PORT) |
| 506 | p.pow(6,8) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 507 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 508 | # ignore failures due to non-blocking socket 'unavailable' errors |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 509 | if not is_unavailable_exception(e) and hasattr(e, "headers"): |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 510 | # The two server-side error headers shouldn't be sent back in this case |
| 511 | self.assertTrue(e.headers.get("X-exception") is None) |
| 512 | self.assertTrue(e.headers.get("X-traceback") is None) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 513 | else: |
| 514 | self.fail('ProtocolError not raised') |
| 515 | |
| 516 | def test_fail_with_info(self): |
| 517 | # use the broken message class |
| 518 | SimpleXMLRPCServer.SimpleXMLRPCRequestHandler.MessageClass = FailingMessageClass |
| 519 | |
| 520 | # Check that errors in the server send back exception/traceback |
| 521 | # info when flag is set |
| 522 | SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = True |
| 523 | |
| 524 | try: |
| 525 | p = xmlrpclib.ServerProxy('http://localhost:%d' % PORT) |
| 526 | p.pow(6,8) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 527 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 528 | # ignore failures due to non-blocking socket 'unavailable' errors |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 529 | if not is_unavailable_exception(e) and hasattr(e, "headers"): |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 530 | # We should get error info in the response |
| 531 | expected_err = "invalid literal for int() with base 10: 'I am broken'" |
| 532 | self.assertEqual(e.headers.get("x-exception"), expected_err) |
| 533 | self.assertTrue(e.headers.get("x-traceback") is not None) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 534 | else: |
| 535 | self.fail('ProtocolError not raised') |
| 536 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 537 | class CGIHandlerTestCase(unittest.TestCase): |
| 538 | def setUp(self): |
| 539 | self.cgi = SimpleXMLRPCServer.CGIXMLRPCRequestHandler() |
| 540 | |
| 541 | def tearDown(self): |
| 542 | self.cgi = None |
| 543 | |
| 544 | def test_cgi_get(self): |
| 545 | os.environ['REQUEST_METHOD'] = 'GET' |
| 546 | # if the method is GET and no request_text is given, it runs handle_get |
| 547 | # get sysout output |
| 548 | tmp = sys.stdout |
| 549 | sys.stdout = open(test_support.TESTFN, "w") |
| 550 | self.cgi.handle_request() |
| 551 | sys.stdout.close() |
| 552 | sys.stdout = tmp |
| 553 | |
| 554 | # parse Status header |
| 555 | handle = open(test_support.TESTFN, "r").read() |
| 556 | status = handle.split()[1] |
| 557 | message = ' '.join(handle.split()[2:4]) |
| 558 | |
| 559 | self.assertEqual(status, '400') |
| 560 | self.assertEqual(message, 'Bad Request') |
| 561 | |
| 562 | os.remove(test_support.TESTFN) |
| 563 | os.environ['REQUEST_METHOD'] = '' |
| 564 | |
| 565 | def test_cgi_xmlrpc_response(self): |
| 566 | data = """<?xml version='1.0'?> |
| 567 | <methodCall> |
| 568 | <methodName>test_method</methodName> |
| 569 | <params> |
| 570 | <param> |
| 571 | <value><string>foo</string></value> |
| 572 | </param> |
| 573 | <param> |
| 574 | <value><string>bar</string></value> |
| 575 | </param> |
| 576 | </params> |
| 577 | </methodCall> |
| 578 | """ |
| 579 | open("xmldata.txt", "w").write(data) |
| 580 | tmp1 = sys.stdin |
| 581 | tmp2 = sys.stdout |
| 582 | |
| 583 | sys.stdin = open("xmldata.txt", "r") |
| 584 | sys.stdout = open(test_support.TESTFN, "w") |
| 585 | |
| 586 | self.cgi.handle_request() |
| 587 | |
| 588 | sys.stdin.close() |
| 589 | sys.stdout.close() |
| 590 | sys.stdin = tmp1 |
| 591 | sys.stdout = tmp2 |
| 592 | |
| 593 | # will respond exception, if so, our goal is achieved ;) |
| 594 | handle = open(test_support.TESTFN, "r").read() |
| 595 | |
| 596 | # start with 44th char so as not to get http header, we just need only xml |
| 597 | self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, handle[44:]) |
| 598 | |
| 599 | os.remove("xmldata.txt") |
| 600 | os.remove(test_support.TESTFN) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 601 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 602 | def test_main(): |
| 603 | xmlrpc_tests = [XMLRPCTestCase, HelperTestCase, DateTimeTestCase, |
| 604 | BinaryTestCase, FaultTestCase] |
| 605 | |
| 606 | # The test cases against a SimpleXMLRPCServer raise a socket error |
| 607 | # 10035 (WSAEWOULDBLOCK) in the server thread handle_request call when |
| 608 | # run on Windows. This only happens on the first test to run, but it |
| 609 | # fails every time and so these tests are skipped on win32 platforms. |
| 610 | if sys.platform != 'win32': |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 611 | xmlrpc_tests.append(SimpleServerTestCase) |
| 612 | xmlrpc_tests.append(FailingServerTestCase) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 613 | xmlrpc_tests.append(CGIHandlerTestCase) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 614 | |
| 615 | test_support.run_unittest(*xmlrpc_tests) |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 616 | |
| 617 | if __name__ == "__main__": |
| 618 | test_main() |