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 |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 6 | import xmlrpc.client as xmlrpclib |
| 7 | import xmlrpc.server |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 8 | import threading |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 9 | import http.client |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 10 | import socket |
| 11 | import os |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 12 | import re |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 13 | from test import 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()) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame^] | 66 | self.assertTrue(dt == now) |
| 67 | self.assertTrue(now == dt) |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 68 | then = now + datetime.timedelta(seconds=4) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame^] | 69 | self.assertTrue(then >= dt) |
| 70 | self.assertTrue(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)) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame^] | 76 | self.assertTrue(isinstance(new_d.value, str)) |
Andrew M. Kuchling | bdb3901 | 2005-12-04 19:11:17 +0000 | [diff] [blame] | 77 | |
| 78 | # Check that the output of dumps() is still an 8-bit string |
| 79 | s = xmlrpclib.dumps((new_d,), methodresponse=True) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame^] | 80 | self.assertTrue(isinstance(s, str)) |
Andrew M. Kuchling | bdb3901 | 2005-12-04 19:11:17 +0000 | [diff] [blame] | 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)) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 115 | self.assertRaises(OverflowError, xmlrpclib.dumps, |
| 116 | (xmlrpclib.MAXINT+1,)) |
| 117 | self.assertRaises(OverflowError, xmlrpclib.dumps, |
| 118 | (xmlrpclib.MININT-1,)) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 119 | |
| 120 | def dummy_write(s): |
| 121 | pass |
| 122 | |
| 123 | m = xmlrpclib.Marshaller() |
| 124 | m.dump_int(xmlrpclib.MAXINT, dummy_write) |
| 125 | m.dump_int(xmlrpclib.MININT, dummy_write) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 126 | self.assertRaises(OverflowError, m.dump_int, |
| 127 | xmlrpclib.MAXINT+1, dummy_write) |
| 128 | self.assertRaises(OverflowError, m.dump_int, |
| 129 | xmlrpclib.MININT-1, dummy_write) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 130 | |
Andrew M. Kuchling | 0b85203 | 2003-04-25 00:27:24 +0000 | [diff] [blame] | 131 | def test_dump_none(self): |
| 132 | value = alist + [None] |
| 133 | arg1 = (alist + [None],) |
| 134 | strg = xmlrpclib.dumps(arg1, allow_none=True) |
| 135 | self.assertEquals(value, |
| 136 | xmlrpclib.loads(strg)[0][0]) |
| 137 | self.assertRaises(TypeError, xmlrpclib.dumps, (arg1,)) |
| 138 | |
Georg Brandl | c8dcfb6 | 2009-02-13 10:50:01 +0000 | [diff] [blame] | 139 | def test_get_host_info(self): |
| 140 | # see bug #3613, this raised a TypeError |
| 141 | transp = xmlrpc.client.Transport() |
| 142 | self.assertEquals(transp.get_host_info("user@host.tld"), |
| 143 | ('host.tld', |
| 144 | [('Authorization', 'Basic dXNlcg==')], {})) |
| 145 | |
| 146 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 147 | class HelperTestCase(unittest.TestCase): |
| 148 | def test_escape(self): |
| 149 | self.assertEqual(xmlrpclib.escape("a&b"), "a&b") |
| 150 | self.assertEqual(xmlrpclib.escape("a<b"), "a<b") |
| 151 | self.assertEqual(xmlrpclib.escape("a>b"), "a>b") |
| 152 | |
| 153 | class FaultTestCase(unittest.TestCase): |
| 154 | def test_repr(self): |
| 155 | f = xmlrpclib.Fault(42, 'Test Fault') |
| 156 | self.assertEqual(repr(f), "<Fault 42: 'Test Fault'>") |
| 157 | self.assertEqual(repr(f), str(f)) |
| 158 | |
| 159 | def test_dump_fault(self): |
| 160 | f = xmlrpclib.Fault(42, 'Test Fault') |
| 161 | s = xmlrpclib.dumps((f,)) |
| 162 | (newf,), m = xmlrpclib.loads(s) |
| 163 | self.assertEquals(newf, {'faultCode': 42, 'faultString': 'Test Fault'}) |
| 164 | self.assertEquals(m, None) |
| 165 | |
| 166 | s = xmlrpclib.Marshaller().dumps(f) |
| 167 | self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, s) |
| 168 | |
Christian Heimes | eda9e2b | 2007-12-08 22:32:47 +0000 | [diff] [blame] | 169 | def test_dotted_attribute(self): |
| 170 | # this will raise AttirebuteError because code don't want us to use |
| 171 | # private methods |
| 172 | self.assertRaises(AttributeError, |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 173 | xmlrpc.server.resolve_dotted_attribute, str, '__add') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame^] | 174 | self.assertTrue(xmlrpc.server.resolve_dotted_attribute(str, 'title')) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 175 | |
| 176 | class DateTimeTestCase(unittest.TestCase): |
| 177 | def test_default(self): |
| 178 | t = xmlrpclib.DateTime() |
| 179 | |
| 180 | def test_time(self): |
| 181 | d = 1181399930.036952 |
| 182 | t = xmlrpclib.DateTime(d) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 183 | self.assertEqual(str(t), |
| 184 | time.strftime("%Y%m%dT%H:%M:%S", time.localtime(d))) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 185 | |
| 186 | def test_time_tuple(self): |
| 187 | d = (2007,6,9,10,38,50,5,160,0) |
| 188 | t = xmlrpclib.DateTime(d) |
| 189 | self.assertEqual(str(t), '20070609T10:38:50') |
| 190 | |
| 191 | def test_time_struct(self): |
| 192 | d = time.localtime(1181399930.036952) |
| 193 | t = xmlrpclib.DateTime(d) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 194 | self.assertEqual(str(t), time.strftime("%Y%m%dT%H:%M:%S", d)) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 195 | |
| 196 | def test_datetime_datetime(self): |
| 197 | d = datetime.datetime(2007,1,2,3,4,5) |
| 198 | t = xmlrpclib.DateTime(d) |
| 199 | self.assertEqual(str(t), '20070102T03:04:05') |
| 200 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 201 | def test_repr(self): |
| 202 | d = datetime.datetime(2007,1,2,3,4,5) |
| 203 | t = xmlrpclib.DateTime(d) |
| 204 | val ="<DateTime '20070102T03:04:05' at %x>" % id(t) |
| 205 | self.assertEqual(repr(t), val) |
| 206 | |
| 207 | def test_decode(self): |
| 208 | d = ' 20070908T07:11:13 ' |
| 209 | t1 = xmlrpclib.DateTime() |
| 210 | t1.decode(d) |
| 211 | tref = xmlrpclib.DateTime(datetime.datetime(2007,9,8,7,11,13)) |
| 212 | self.assertEqual(t1, tref) |
| 213 | |
| 214 | t2 = xmlrpclib._datetime(d) |
| 215 | self.assertEqual(t1, tref) |
| 216 | |
| 217 | class BinaryTestCase(unittest.TestCase): |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 218 | |
| 219 | # XXX What should str(Binary(b"\xff")) return? I'm chosing "\xff" |
| 220 | # for now (i.e. interpreting the binary data as Latin-1-encoded |
| 221 | # text). But this feels very unsatisfactory. Perhaps we should |
| 222 | # only define repr(), and return r"Binary(b'\xff')" instead? |
| 223 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 224 | def test_default(self): |
| 225 | t = xmlrpclib.Binary() |
| 226 | self.assertEqual(str(t), '') |
| 227 | |
| 228 | def test_string(self): |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 229 | d = b'\x01\x02\x03abc123\xff\xfe' |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 230 | t = xmlrpclib.Binary(d) |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 231 | self.assertEqual(str(t), str(d, "latin-1")) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 232 | |
| 233 | def test_decode(self): |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 234 | d = b'\x01\x02\x03abc123\xff\xfe' |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 235 | de = base64.encodebytes(d) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 236 | t1 = xmlrpclib.Binary() |
| 237 | t1.decode(de) |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 238 | self.assertEqual(str(t1), str(d, "latin-1")) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 239 | |
| 240 | t2 = xmlrpclib._binary(de) |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 241 | self.assertEqual(str(t2), str(d, "latin-1")) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 242 | |
| 243 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 244 | ADDR = PORT = URL = None |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 245 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 246 | # The evt is set twice. First when the server is ready to serve. |
| 247 | # Second when the server has been shutdown. The user must clear |
| 248 | # 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] | 249 | def http_server(evt, numrequests): |
| 250 | class TestInstanceClass: |
| 251 | def div(self, x, y): |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 252 | return x // y |
| 253 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 254 | def _methodHelp(self, name): |
| 255 | if name == 'div': |
| 256 | return 'This is the div function' |
| 257 | |
| 258 | def my_function(): |
| 259 | '''This is my function''' |
| 260 | return True |
| 261 | |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 262 | class MyXMLRPCServer(xmlrpc.server.SimpleXMLRPCServer): |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 263 | def get_request(self): |
| 264 | # Ensure the socket is always non-blocking. On Linux, socket |
| 265 | # attributes are not inherited like they are on *BSD and Windows. |
| 266 | s, port = self.socket.accept() |
| 267 | s.setblocking(True) |
| 268 | return s, port |
| 269 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 270 | serv = MyXMLRPCServer(("localhost", 0), |
| 271 | logRequests=False, bind_and_activate=False) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 272 | try: |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 273 | serv.server_bind() |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 274 | global ADDR, PORT, URL |
| 275 | ADDR, PORT = serv.socket.getsockname() |
| 276 | #connect to IP address directly. This avoids socket.create_connection() |
| 277 | #trying to connect to to "localhost" using all address families, which |
| 278 | #causes slowdown e.g. on vista which supports AF_INET6. The server listens |
| 279 | #on AF_INET only. |
| 280 | URL = "http://%s:%d"%(ADDR, PORT) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 281 | serv.server_activate() |
| 282 | serv.register_introspection_functions() |
| 283 | serv.register_multicall_functions() |
| 284 | serv.register_function(pow) |
| 285 | serv.register_function(lambda x,y: x+y, 'add') |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 286 | serv.register_function(my_function) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 287 | serv.register_instance(TestInstanceClass()) |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 288 | evt.set() |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 289 | |
| 290 | # handle up to 'numrequests' requests |
| 291 | while numrequests > 0: |
| 292 | serv.handle_request() |
| 293 | numrequests -= 1 |
| 294 | |
| 295 | except socket.timeout: |
| 296 | pass |
| 297 | finally: |
| 298 | serv.socket.close() |
| 299 | PORT = None |
| 300 | evt.set() |
| 301 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 302 | # This function prevents errors like: |
| 303 | # <ProtocolError for localhost:57527/RPC2: 500 Internal Server Error> |
| 304 | def is_unavailable_exception(e): |
| 305 | '''Returns True if the given ProtocolError is the product of a server-side |
| 306 | exception caused by the 'temporarily unavailable' response sometimes |
| 307 | given by operations on non-blocking sockets.''' |
| 308 | |
| 309 | # sometimes we get a -1 error code and/or empty headers |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 310 | try: |
| 311 | if e.errcode == -1 or e.headers is None: |
| 312 | return True |
| 313 | exc_mess = e.headers.get('X-exception') |
| 314 | except AttributeError: |
| 315 | # Ignore socket.errors here. |
| 316 | exc_mess = str(e) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 317 | |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 318 | if exc_mess and 'temporarily unavailable' in exc_mess.lower(): |
| 319 | return True |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 320 | |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 321 | class SimpleServerTestCase(unittest.TestCase): |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 322 | def setUp(self): |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 323 | # enable traceback reporting |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 324 | xmlrpc.server.SimpleXMLRPCServer._send_traceback_header = True |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 325 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 326 | self.evt = threading.Event() |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 327 | # start server thread to handle requests |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 328 | serv_args = (self.evt, 1) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 329 | threading.Thread(target=http_server, args=serv_args).start() |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 330 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 331 | # wait for the server to be ready |
| 332 | self.evt.wait() |
| 333 | self.evt.clear() |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 334 | |
| 335 | def tearDown(self): |
| 336 | # wait on the server thread to terminate |
Christian Heimes | 576bf65 | 2007-12-08 17:09:18 +0000 | [diff] [blame] | 337 | self.evt.wait(4.0) |
Benjamin Peterson | 1e637b7 | 2008-06-11 21:55:48 +0000 | [diff] [blame] | 338 | if not self.evt.is_set(): |
Christian Heimes | 576bf65 | 2007-12-08 17:09:18 +0000 | [diff] [blame] | 339 | self.evt.set() |
| 340 | stop_serving() |
| 341 | raise RuntimeError("timeout reached, test has failed") |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 342 | |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 343 | # disable traceback reporting |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 344 | xmlrpc.server.SimpleXMLRPCServer._send_traceback_header = False |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 345 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 346 | def test_simple1(self): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 347 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 348 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 349 | self.assertEqual(p.pow(6,8), 6**8) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 350 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 351 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 352 | if not is_unavailable_exception(e): |
| 353 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 354 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 355 | |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 356 | def test_nonascii(self): |
| 357 | start_string = 'P\N{LATIN SMALL LETTER Y WITH CIRCUMFLEX}t' |
| 358 | end_string = 'h\N{LATIN SMALL LETTER O WITH HORN}n' |
| 359 | try: |
| 360 | p = xmlrpclib.ServerProxy(URL) |
| 361 | self.assertEqual(p.add(start_string, end_string), |
| 362 | start_string + end_string) |
| 363 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 364 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 365 | if not is_unavailable_exception(e): |
| 366 | # protocol error; provide additional information in test output |
| 367 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
| 368 | |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 369 | # [ch] The test 404 is causing lots of false alarms. |
| 370 | def XXXtest_404(self): |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 371 | # send POST with http.client, it should return 404 header and |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 372 | # 'Not Found' message. |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 373 | conn = httplib.client.HTTPConnection(ADDR, PORT) |
Christian Heimes | 99d73f2 | 2007-12-08 16:13:06 +0000 | [diff] [blame] | 374 | conn.request('POST', '/this-is-not-valid') |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 375 | response = conn.getresponse() |
| 376 | conn.close() |
| 377 | |
| 378 | self.assertEqual(response.status, 404) |
| 379 | self.assertEqual(response.reason, 'Not Found') |
| 380 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 381 | def test_introspection1(self): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 382 | expected_methods = set(['pow', 'div', 'my_function', 'add', |
| 383 | 'system.listMethods', 'system.methodHelp', |
| 384 | 'system.methodSignature', 'system.multicall']) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 385 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 386 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 387 | meth = p.system.listMethods() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 388 | self.assertEqual(set(meth), expected_methods) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 389 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 390 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 391 | if not is_unavailable_exception(e): |
| 392 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 393 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 394 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 395 | |
| 396 | def test_introspection2(self): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 397 | try: |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 398 | # test _methodHelp() |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 399 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 400 | divhelp = p.system.methodHelp('div') |
| 401 | self.assertEqual(divhelp, 'This is the div function') |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 402 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 403 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 404 | if not is_unavailable_exception(e): |
| 405 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 406 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 407 | |
| 408 | def test_introspection3(self): |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 409 | try: |
| 410 | # test native doc |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 411 | p = xmlrpclib.ServerProxy(URL) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 412 | myfunction = p.system.methodHelp('my_function') |
| 413 | self.assertEqual(myfunction, 'This is my function') |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 414 | except (xmlrpclib.ProtocolError, socket.error) as e: |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 415 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 416 | if not is_unavailable_exception(e): |
| 417 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 418 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 419 | |
| 420 | def test_introspection4(self): |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 421 | # the SimpleXMLRPCServer doesn't support signatures, but |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 422 | # at least check that we can try making the call |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 423 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 424 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 425 | divsig = p.system.methodSignature('div') |
| 426 | self.assertEqual(divsig, 'signatures not supported') |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 427 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 428 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 429 | if not is_unavailable_exception(e): |
| 430 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 431 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 432 | |
| 433 | def test_multicall(self): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 434 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 435 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 436 | multicall = xmlrpclib.MultiCall(p) |
| 437 | multicall.add(2,3) |
| 438 | multicall.pow(6,8) |
| 439 | multicall.div(127,42) |
| 440 | add_result, pow_result, div_result = multicall() |
| 441 | self.assertEqual(add_result, 2+3) |
| 442 | self.assertEqual(pow_result, 6**8) |
| 443 | self.assertEqual(div_result, 127//42) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 444 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 445 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 446 | if not is_unavailable_exception(e): |
| 447 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 448 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 449 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 450 | def test_non_existing_multicall(self): |
| 451 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 452 | p = xmlrpclib.ServerProxy(URL) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 453 | multicall = xmlrpclib.MultiCall(p) |
| 454 | multicall.this_is_not_exists() |
| 455 | result = multicall() |
| 456 | |
| 457 | # result.results contains; |
Martin v. Löwis | 250ad61 | 2008-04-07 05:43:42 +0000 | [diff] [blame] | 458 | # [{'faultCode': 1, 'faultString': '<class \'exceptions.Exception\'>:' |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 459 | # 'method "this_is_not_exists" is not supported'>}] |
| 460 | |
| 461 | self.assertEqual(result.results[0]['faultCode'], 1) |
| 462 | self.assertEqual(result.results[0]['faultString'], |
Martin v. Löwis | 250ad61 | 2008-04-07 05:43:42 +0000 | [diff] [blame] | 463 | '<class \'Exception\'>:method "this_is_not_exists" ' |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 464 | 'is not supported') |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 465 | except (xmlrpclib.ProtocolError, socket.error) as e: |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 466 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 467 | if not is_unavailable_exception(e): |
| 468 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 469 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 470 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 471 | def test_dotted_attribute(self): |
| 472 | # Raises an AttributeError because private methods are not allowed. |
| 473 | self.assertRaises(AttributeError, |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 474 | xmlrpc.server.resolve_dotted_attribute, str, '__add') |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 475 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame^] | 476 | self.assertTrue(xmlrpc.server.resolve_dotted_attribute(str, 'title')) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 477 | # Get the test to run faster by sending a request with test_simple1. |
| 478 | # This avoids waiting for the socket timeout. |
| 479 | self.test_simple1() |
| 480 | |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 481 | # This is a contrived way to make a failure occur on the server side |
| 482 | # in order to test the _send_traceback_header flag on the server |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 483 | class FailingMessageClass(http.client.HTTPMessage): |
| 484 | def get(self, key, failobj=None): |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 485 | key = key.lower() |
| 486 | if key == 'content-length': |
| 487 | return 'I am broken' |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 488 | return super().get(key, failobj) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 489 | |
| 490 | |
| 491 | class FailingServerTestCase(unittest.TestCase): |
| 492 | def setUp(self): |
| 493 | self.evt = threading.Event() |
| 494 | # start server thread to handle requests |
Guido van Rossum | 15c9746 | 2007-11-02 16:10:06 +0000 | [diff] [blame] | 495 | serv_args = (self.evt, 1) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 496 | threading.Thread(target=http_server, args=serv_args).start() |
| 497 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 498 | # wait for the server to be ready |
| 499 | self.evt.wait() |
| 500 | self.evt.clear() |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 501 | |
| 502 | def tearDown(self): |
| 503 | # wait on the server thread to terminate |
| 504 | self.evt.wait() |
| 505 | # reset flag |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 506 | xmlrpc.server.SimpleXMLRPCServer._send_traceback_header = False |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 507 | # reset message class |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 508 | default_class = http.client.HTTPMessage |
| 509 | xmlrpc.server.SimpleXMLRPCRequestHandler.MessageClass = default_class |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 510 | |
| 511 | def test_basic(self): |
| 512 | # check that flag is false by default |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 513 | flagval = xmlrpc.server.SimpleXMLRPCServer._send_traceback_header |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 514 | self.assertEqual(flagval, False) |
| 515 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 516 | # enable traceback reporting |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 517 | xmlrpc.server.SimpleXMLRPCServer._send_traceback_header = True |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 518 | |
| 519 | # test a call that shouldn't fail just as a smoke test |
| 520 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 521 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 522 | self.assertEqual(p.pow(6,8), 6**8) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 523 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 524 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 525 | if not is_unavailable_exception(e): |
| 526 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 527 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 528 | |
| 529 | def test_fail_no_info(self): |
| 530 | # use the broken message class |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 531 | xmlrpc.server.SimpleXMLRPCRequestHandler.MessageClass = FailingMessageClass |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 532 | |
| 533 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 534 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 535 | p.pow(6,8) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 536 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 537 | # ignore failures due to non-blocking socket 'unavailable' errors |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 538 | if not is_unavailable_exception(e) and hasattr(e, "headers"): |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 539 | # The two server-side error headers shouldn't be sent back in this case |
| 540 | self.assertTrue(e.headers.get("X-exception") is None) |
| 541 | self.assertTrue(e.headers.get("X-traceback") is None) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 542 | else: |
| 543 | self.fail('ProtocolError not raised') |
| 544 | |
| 545 | def test_fail_with_info(self): |
| 546 | # use the broken message class |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 547 | xmlrpc.server.SimpleXMLRPCRequestHandler.MessageClass = FailingMessageClass |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 548 | |
| 549 | # Check that errors in the server send back exception/traceback |
| 550 | # info when flag is set |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 551 | xmlrpc.server.SimpleXMLRPCServer._send_traceback_header = True |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 552 | |
| 553 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 554 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 555 | p.pow(6,8) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 556 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 557 | # ignore failures due to non-blocking socket 'unavailable' errors |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 558 | if not is_unavailable_exception(e) and hasattr(e, "headers"): |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 559 | # We should get error info in the response |
| 560 | expected_err = "invalid literal for int() with base 10: 'I am broken'" |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 561 | self.assertEqual(e.headers.get("X-exception"), expected_err) |
| 562 | self.assertTrue(e.headers.get("X-traceback") is not None) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 563 | else: |
| 564 | self.fail('ProtocolError not raised') |
| 565 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 566 | class CGIHandlerTestCase(unittest.TestCase): |
| 567 | def setUp(self): |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 568 | self.cgi = xmlrpc.server.CGIXMLRPCRequestHandler() |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 569 | |
| 570 | def tearDown(self): |
| 571 | self.cgi = None |
| 572 | |
| 573 | def test_cgi_get(self): |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 574 | with support.EnvironmentVarGuard() as env: |
Walter Dörwald | 155374d | 2009-05-01 19:58:58 +0000 | [diff] [blame] | 575 | env['REQUEST_METHOD'] = 'GET' |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 576 | # if the method is GET and no request_text is given, it runs handle_get |
| 577 | # get sysout output |
| 578 | tmp = sys.stdout |
| 579 | sys.stdout = open(support.TESTFN, "w") |
| 580 | self.cgi.handle_request() |
| 581 | sys.stdout.close() |
| 582 | sys.stdout = tmp |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 583 | |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 584 | # parse Status header |
| 585 | handle = open(support.TESTFN, "r").read() |
| 586 | status = handle.split()[1] |
| 587 | message = ' '.join(handle.split()[2:4]) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 588 | |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 589 | self.assertEqual(status, '400') |
| 590 | self.assertEqual(message, 'Bad Request') |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 591 | |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 592 | os.remove(support.TESTFN) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 593 | |
| 594 | def test_cgi_xmlrpc_response(self): |
| 595 | data = """<?xml version='1.0'?> |
| 596 | <methodCall> |
| 597 | <methodName>test_method</methodName> |
| 598 | <params> |
| 599 | <param> |
| 600 | <value><string>foo</string></value> |
| 601 | </param> |
| 602 | <param> |
| 603 | <value><string>bar</string></value> |
| 604 | </param> |
| 605 | </params> |
| 606 | </methodCall> |
| 607 | """ |
| 608 | open("xmldata.txt", "w").write(data) |
| 609 | tmp1 = sys.stdin |
| 610 | tmp2 = sys.stdout |
| 611 | |
| 612 | sys.stdin = open("xmldata.txt", "r") |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 613 | sys.stdout = open(support.TESTFN, "w") |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 614 | |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 615 | with support.EnvironmentVarGuard() as env: |
Walter Dörwald | 155374d | 2009-05-01 19:58:58 +0000 | [diff] [blame] | 616 | env['CONTENT_LENGTH'] = str(len(data)) |
Georg Brandl | c748506 | 2009-04-01 15:53:15 +0000 | [diff] [blame] | 617 | self.cgi.handle_request() |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 618 | |
| 619 | sys.stdin.close() |
| 620 | sys.stdout.close() |
| 621 | sys.stdin = tmp1 |
| 622 | sys.stdout = tmp2 |
| 623 | |
| 624 | # will respond exception, if so, our goal is achieved ;) |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 625 | handle = open(support.TESTFN, "r").read() |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 626 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 627 | # start with 44th char so as not to get http header, we just |
| 628 | # need only xml |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 629 | self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, handle[44:]) |
| 630 | |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 631 | # Also test the content-length returned by handle_request |
| 632 | # Using the same test method inorder to avoid all the datapassing |
| 633 | # boilerplate code. |
| 634 | # Test for bug: http://bugs.python.org/issue5040 |
| 635 | |
| 636 | content = handle[handle.find("<?xml"):] |
| 637 | |
| 638 | self.assertEquals( |
| 639 | int(re.search('Content-Length: (\d+)', handle).group(1)), |
| 640 | len(content)) |
| 641 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 642 | os.remove("xmldata.txt") |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 643 | os.remove(support.TESTFN) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 644 | |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 645 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 646 | def test_main(): |
| 647 | xmlrpc_tests = [XMLRPCTestCase, HelperTestCase, DateTimeTestCase, |
| 648 | BinaryTestCase, FaultTestCase] |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 649 | xmlrpc_tests.append(SimpleServerTestCase) |
| 650 | xmlrpc_tests.append(FailingServerTestCase) |
| 651 | xmlrpc_tests.append(CGIHandlerTestCase) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 652 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 653 | support.run_unittest(*xmlrpc_tests) |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 654 | |
| 655 | if __name__ == "__main__": |
| 656 | test_main() |