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 |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 8 | import http.client |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 9 | import socket |
| 10 | import os |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 11 | import re |
R. David Murray | 0548ce0 | 2009-10-26 08:24:14 +0000 | [diff] [blame] | 12 | import io |
| 13 | import contextlib |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 14 | from test import support |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 15 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 16 | try: |
| 17 | import threading |
| 18 | except ImportError: |
| 19 | threading = None |
| 20 | |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 21 | alist = [{'astring': 'foo@bar.baz.spam', |
| 22 | 'afloat': 7283.43, |
Skip Montanaro | 3e7bba9 | 2001-10-19 16:06:52 +0000 | [diff] [blame] | 23 | 'anint': 2**20, |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 24 | 'ashortlong': 2, |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 25 | 'anotherlist': ['.zyx.41'], |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 26 | 'abase64': xmlrpclib.Binary(b"my dog has fleas"), |
Guido van Rossum | e7ba495 | 2007-06-06 23:52:48 +0000 | [diff] [blame] | 27 | 'boolean': False, |
Guido van Rossum | ef87d6e | 2007-05-02 19:09:54 +0000 | [diff] [blame] | 28 | 'unicode': '\u4000\u6000\u8000', |
| 29 | 'ukey\u4000': 'regular value', |
Fred Drake | ba613c3 | 2005-02-10 18:33:30 +0000 | [diff] [blame] | 30 | 'datetime1': xmlrpclib.DateTime('20050210T11:41:23'), |
| 31 | 'datetime2': xmlrpclib.DateTime( |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 32 | (2005, 2, 10, 11, 41, 23, 0, 1, -1)), |
Fred Drake | ba613c3 | 2005-02-10 18:33:30 +0000 | [diff] [blame] | 33 | 'datetime3': xmlrpclib.DateTime( |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 34 | datetime.datetime(2005, 2, 10, 11, 41, 23)), |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 35 | }] |
| 36 | |
| 37 | class XMLRPCTestCase(unittest.TestCase): |
| 38 | |
| 39 | def test_dump_load(self): |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 40 | dump = xmlrpclib.dumps((alist,)) |
| 41 | load = xmlrpclib.loads(dump) |
| 42 | self.assertEquals(alist, load[0][0]) |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 43 | |
Fred Drake | ba613c3 | 2005-02-10 18:33:30 +0000 | [diff] [blame] | 44 | def test_dump_bare_datetime(self): |
Skip Montanaro | 174dd22 | 2005-05-14 20:54:16 +0000 | [diff] [blame] | 45 | # This checks that an unwrapped datetime.date object can be handled |
| 46 | # by the marshalling code. This can't be done via test_dump_load() |
| 47 | # since with use_datetime set to 1 the unmarshaller would create |
| 48 | # datetime objects for the 'datetime[123]' keys as well |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 49 | dt = datetime.datetime(2005, 2, 10, 11, 41, 23) |
Fred Drake | ba613c3 | 2005-02-10 18:33:30 +0000 | [diff] [blame] | 50 | s = xmlrpclib.dumps((dt,)) |
Skip Montanaro | 174dd22 | 2005-05-14 20:54:16 +0000 | [diff] [blame] | 51 | (newdt,), m = xmlrpclib.loads(s, use_datetime=1) |
| 52 | self.assertEquals(newdt, dt) |
Fred Drake | ba613c3 | 2005-02-10 18:33:30 +0000 | [diff] [blame] | 53 | self.assertEquals(m, None) |
| 54 | |
Skip Montanaro | 174dd22 | 2005-05-14 20:54:16 +0000 | [diff] [blame] | 55 | (newdt,), m = xmlrpclib.loads(s, use_datetime=0) |
| 56 | self.assertEquals(newdt, xmlrpclib.DateTime('20050210T11:41:23')) |
| 57 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 58 | def test_datetime_before_1900(self): |
Christian Heimes | 81ee3ef | 2008-05-04 22:42:01 +0000 | [diff] [blame] | 59 | # same as before but with a date before 1900 |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 60 | dt = datetime.datetime(1, 2, 10, 11, 41, 23) |
| 61 | s = xmlrpclib.dumps((dt,)) |
| 62 | (newdt,), m = xmlrpclib.loads(s, use_datetime=1) |
| 63 | self.assertEquals(newdt, dt) |
| 64 | self.assertEquals(m, None) |
| 65 | |
| 66 | (newdt,), m = xmlrpclib.loads(s, use_datetime=0) |
| 67 | self.assertEquals(newdt, xmlrpclib.DateTime('00010210T11:41:23')) |
| 68 | |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 69 | def test_cmp_datetime_DateTime(self): |
| 70 | now = datetime.datetime.now() |
| 71 | dt = xmlrpclib.DateTime(now.timetuple()) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 72 | self.assertTrue(dt == now) |
| 73 | self.assertTrue(now == dt) |
Christian Heimes | 05e8be1 | 2008-02-23 18:30:17 +0000 | [diff] [blame] | 74 | then = now + datetime.timedelta(seconds=4) |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 75 | self.assertTrue(then >= dt) |
| 76 | self.assertTrue(dt < then) |
Skip Montanaro | 174dd22 | 2005-05-14 20:54:16 +0000 | [diff] [blame] | 77 | |
Andrew M. Kuchling | bdb3901 | 2005-12-04 19:11:17 +0000 | [diff] [blame] | 78 | def test_bug_1164912 (self): |
| 79 | d = xmlrpclib.DateTime() |
Tim Peters | 536cf99 | 2005-12-25 23:18:31 +0000 | [diff] [blame] | 80 | ((new_d,), dummy) = xmlrpclib.loads(xmlrpclib.dumps((d,), |
Andrew M. Kuchling | bdb3901 | 2005-12-04 19:11:17 +0000 | [diff] [blame] | 81 | methodresponse=True)) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 82 | self.assertIsInstance(new_d.value, str) |
Andrew M. Kuchling | bdb3901 | 2005-12-04 19:11:17 +0000 | [diff] [blame] | 83 | |
| 84 | # Check that the output of dumps() is still an 8-bit string |
| 85 | s = xmlrpclib.dumps((new_d,), methodresponse=True) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 86 | self.assertIsInstance(s, str) |
Andrew M. Kuchling | bdb3901 | 2005-12-04 19:11:17 +0000 | [diff] [blame] | 87 | |
Thomas Wouters | 89f507f | 2006-12-13 04:49:30 +0000 | [diff] [blame] | 88 | def test_newstyle_class(self): |
| 89 | class T(object): |
| 90 | pass |
| 91 | t = T() |
| 92 | t.x = 100 |
| 93 | t.y = "Hello" |
| 94 | ((t2,), dummy) = xmlrpclib.loads(xmlrpclib.dumps((t,))) |
| 95 | self.assertEquals(t2, t.__dict__) |
| 96 | |
Skip Montanaro | 3e7bba9 | 2001-10-19 16:06:52 +0000 | [diff] [blame] | 97 | def test_dump_big_long(self): |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 98 | self.assertRaises(OverflowError, xmlrpclib.dumps, (2**99,)) |
Skip Montanaro | 3e7bba9 | 2001-10-19 16:06:52 +0000 | [diff] [blame] | 99 | |
| 100 | def test_dump_bad_dict(self): |
| 101 | self.assertRaises(TypeError, xmlrpclib.dumps, ({(1,2,3): 1},)) |
| 102 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 103 | def test_dump_recursive_seq(self): |
| 104 | l = [1,2,3] |
| 105 | t = [3,4,5,l] |
| 106 | l.append(t) |
| 107 | self.assertRaises(TypeError, xmlrpclib.dumps, (l,)) |
| 108 | |
| 109 | def test_dump_recursive_dict(self): |
| 110 | d = {'1':1, '2':1} |
| 111 | t = {'3':3, 'd':d} |
| 112 | d['t'] = t |
| 113 | self.assertRaises(TypeError, xmlrpclib.dumps, (d,)) |
| 114 | |
Skip Montanaro | 3e7bba9 | 2001-10-19 16:06:52 +0000 | [diff] [blame] | 115 | def test_dump_big_int(self): |
Christian Heimes | a37d4c6 | 2007-12-04 23:02:19 +0000 | [diff] [blame] | 116 | if sys.maxsize > 2**31-1: |
Skip Montanaro | 3e7bba9 | 2001-10-19 16:06:52 +0000 | [diff] [blame] | 117 | self.assertRaises(OverflowError, xmlrpclib.dumps, |
Guido van Rossum | e2a383d | 2007-01-15 16:59:06 +0000 | [diff] [blame] | 118 | (int(2**34),)) |
Skip Montanaro | 3e7bba9 | 2001-10-19 16:06:52 +0000 | [diff] [blame] | 119 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 120 | xmlrpclib.dumps((xmlrpclib.MAXINT, xmlrpclib.MININT)) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 121 | self.assertRaises(OverflowError, xmlrpclib.dumps, |
| 122 | (xmlrpclib.MAXINT+1,)) |
| 123 | self.assertRaises(OverflowError, xmlrpclib.dumps, |
| 124 | (xmlrpclib.MININT-1,)) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 125 | |
| 126 | def dummy_write(s): |
| 127 | pass |
| 128 | |
| 129 | m = xmlrpclib.Marshaller() |
| 130 | m.dump_int(xmlrpclib.MAXINT, dummy_write) |
| 131 | m.dump_int(xmlrpclib.MININT, dummy_write) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 132 | self.assertRaises(OverflowError, m.dump_int, |
| 133 | xmlrpclib.MAXINT+1, dummy_write) |
| 134 | self.assertRaises(OverflowError, m.dump_int, |
| 135 | xmlrpclib.MININT-1, dummy_write) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 136 | |
Andrew M. Kuchling | 0b85203 | 2003-04-25 00:27:24 +0000 | [diff] [blame] | 137 | def test_dump_none(self): |
| 138 | value = alist + [None] |
| 139 | arg1 = (alist + [None],) |
| 140 | strg = xmlrpclib.dumps(arg1, allow_none=True) |
| 141 | self.assertEquals(value, |
| 142 | xmlrpclib.loads(strg)[0][0]) |
| 143 | self.assertRaises(TypeError, xmlrpclib.dumps, (arg1,)) |
| 144 | |
Georg Brandl | c8dcfb6 | 2009-02-13 10:50:01 +0000 | [diff] [blame] | 145 | def test_get_host_info(self): |
| 146 | # see bug #3613, this raised a TypeError |
| 147 | transp = xmlrpc.client.Transport() |
| 148 | self.assertEquals(transp.get_host_info("user@host.tld"), |
| 149 | ('host.tld', |
| 150 | [('Authorization', 'Basic dXNlcg==')], {})) |
| 151 | |
Senthil Kumaran | 8ce1f1f | 2010-11-18 15:00:53 +0000 | [diff] [blame] | 152 | def test_ssl_presence(self): |
Senthil Kumaran | 6a0b5c4 | 2010-11-18 17:08:48 +0000 | [diff] [blame] | 153 | try: |
| 154 | import ssl |
Antoine Pitrou | 98b644f | 2010-11-19 20:07:52 +0000 | [diff] [blame] | 155 | except ImportError: |
| 156 | has_ssl = False |
Senthil Kumaran | 8ce1f1f | 2010-11-18 15:00:53 +0000 | [diff] [blame] | 157 | else: |
Senthil Kumaran | 6a0b5c4 | 2010-11-18 17:08:48 +0000 | [diff] [blame] | 158 | has_ssl = True |
Senthil Kumaran | 8ce1f1f | 2010-11-18 15:00:53 +0000 | [diff] [blame] | 159 | try: |
| 160 | xmlrpc.client.ServerProxy('https://localhost:9999').bad_function() |
Senthil Kumaran | 6a0b5c4 | 2010-11-18 17:08:48 +0000 | [diff] [blame] | 161 | except NotImplementedError: |
| 162 | self.assertFalse(has_ssl, "xmlrpc client's error with SSL support") |
| 163 | except socket.error: |
| 164 | self.assertTrue(has_ssl) |
Ezio Melotti | e961593 | 2010-01-24 19:26:24 +0000 | [diff] [blame] | 165 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 166 | class HelperTestCase(unittest.TestCase): |
| 167 | def test_escape(self): |
| 168 | self.assertEqual(xmlrpclib.escape("a&b"), "a&b") |
| 169 | self.assertEqual(xmlrpclib.escape("a<b"), "a<b") |
| 170 | self.assertEqual(xmlrpclib.escape("a>b"), "a>b") |
| 171 | |
| 172 | class FaultTestCase(unittest.TestCase): |
| 173 | def test_repr(self): |
| 174 | f = xmlrpclib.Fault(42, 'Test Fault') |
| 175 | self.assertEqual(repr(f), "<Fault 42: 'Test Fault'>") |
| 176 | self.assertEqual(repr(f), str(f)) |
| 177 | |
| 178 | def test_dump_fault(self): |
| 179 | f = xmlrpclib.Fault(42, 'Test Fault') |
| 180 | s = xmlrpclib.dumps((f,)) |
| 181 | (newf,), m = xmlrpclib.loads(s) |
| 182 | self.assertEquals(newf, {'faultCode': 42, 'faultString': 'Test Fault'}) |
| 183 | self.assertEquals(m, None) |
| 184 | |
| 185 | s = xmlrpclib.Marshaller().dumps(f) |
| 186 | self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, s) |
| 187 | |
Christian Heimes | eda9e2b | 2007-12-08 22:32:47 +0000 | [diff] [blame] | 188 | def test_dotted_attribute(self): |
| 189 | # this will raise AttirebuteError because code don't want us to use |
| 190 | # private methods |
| 191 | self.assertRaises(AttributeError, |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 192 | xmlrpc.server.resolve_dotted_attribute, str, '__add') |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 193 | self.assertTrue(xmlrpc.server.resolve_dotted_attribute(str, 'title')) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 194 | |
| 195 | class DateTimeTestCase(unittest.TestCase): |
| 196 | def test_default(self): |
| 197 | t = xmlrpclib.DateTime() |
| 198 | |
| 199 | def test_time(self): |
| 200 | d = 1181399930.036952 |
| 201 | t = xmlrpclib.DateTime(d) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 202 | self.assertEqual(str(t), |
| 203 | time.strftime("%Y%m%dT%H:%M:%S", time.localtime(d))) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 204 | |
| 205 | def test_time_tuple(self): |
| 206 | d = (2007,6,9,10,38,50,5,160,0) |
| 207 | t = xmlrpclib.DateTime(d) |
| 208 | self.assertEqual(str(t), '20070609T10:38:50') |
| 209 | |
| 210 | def test_time_struct(self): |
| 211 | d = time.localtime(1181399930.036952) |
| 212 | t = xmlrpclib.DateTime(d) |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 213 | 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] | 214 | |
| 215 | def test_datetime_datetime(self): |
| 216 | d = datetime.datetime(2007,1,2,3,4,5) |
| 217 | t = xmlrpclib.DateTime(d) |
| 218 | self.assertEqual(str(t), '20070102T03:04:05') |
| 219 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 220 | def test_repr(self): |
| 221 | d = datetime.datetime(2007,1,2,3,4,5) |
| 222 | t = xmlrpclib.DateTime(d) |
| 223 | val ="<DateTime '20070102T03:04:05' at %x>" % id(t) |
| 224 | self.assertEqual(repr(t), val) |
| 225 | |
| 226 | def test_decode(self): |
| 227 | d = ' 20070908T07:11:13 ' |
| 228 | t1 = xmlrpclib.DateTime() |
| 229 | t1.decode(d) |
| 230 | tref = xmlrpclib.DateTime(datetime.datetime(2007,9,8,7,11,13)) |
| 231 | self.assertEqual(t1, tref) |
| 232 | |
| 233 | t2 = xmlrpclib._datetime(d) |
| 234 | self.assertEqual(t1, tref) |
| 235 | |
| 236 | class BinaryTestCase(unittest.TestCase): |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 237 | |
| 238 | # XXX What should str(Binary(b"\xff")) return? I'm chosing "\xff" |
| 239 | # for now (i.e. interpreting the binary data as Latin-1-encoded |
| 240 | # text). But this feels very unsatisfactory. Perhaps we should |
| 241 | # only define repr(), and return r"Binary(b'\xff')" instead? |
| 242 | |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 243 | def test_default(self): |
| 244 | t = xmlrpclib.Binary() |
| 245 | self.assertEqual(str(t), '') |
| 246 | |
| 247 | def test_string(self): |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 248 | d = b'\x01\x02\x03abc123\xff\xfe' |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 249 | t = xmlrpclib.Binary(d) |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 250 | self.assertEqual(str(t), str(d, "latin-1")) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 251 | |
| 252 | def test_decode(self): |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 253 | d = b'\x01\x02\x03abc123\xff\xfe' |
Georg Brandl | b54d801 | 2009-06-04 09:11:51 +0000 | [diff] [blame] | 254 | de = base64.encodebytes(d) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 255 | t1 = xmlrpclib.Binary() |
| 256 | t1.decode(de) |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 257 | self.assertEqual(str(t1), str(d, "latin-1")) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 258 | |
| 259 | t2 = xmlrpclib._binary(de) |
Guido van Rossum | 54a40cb | 2007-08-27 22:27:41 +0000 | [diff] [blame] | 260 | self.assertEqual(str(t2), str(d, "latin-1")) |
Guido van Rossum | b5a755e | 2007-07-18 18:15:48 +0000 | [diff] [blame] | 261 | |
| 262 | |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 263 | ADDR = PORT = URL = None |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 264 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 265 | # The evt is set twice. First when the server is ready to serve. |
| 266 | # Second when the server has been shutdown. The user must clear |
| 267 | # the event after it has been set the first time to catch the second set. |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 268 | def http_server(evt, numrequests, requestHandler=None): |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 269 | class TestInstanceClass: |
| 270 | def div(self, x, y): |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 271 | return x // y |
| 272 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 273 | def _methodHelp(self, name): |
| 274 | if name == 'div': |
| 275 | return 'This is the div function' |
| 276 | |
| 277 | def my_function(): |
| 278 | '''This is my function''' |
| 279 | return True |
| 280 | |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 281 | class MyXMLRPCServer(xmlrpc.server.SimpleXMLRPCServer): |
Christian Heimes | bbe741d | 2008-03-28 10:53:29 +0000 | [diff] [blame] | 282 | def get_request(self): |
| 283 | # Ensure the socket is always non-blocking. On Linux, socket |
| 284 | # attributes are not inherited like they are on *BSD and Windows. |
| 285 | s, port = self.socket.accept() |
| 286 | s.setblocking(True) |
| 287 | return s, port |
| 288 | |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 289 | if not requestHandler: |
| 290 | requestHandler = xmlrpc.server.SimpleXMLRPCRequestHandler |
| 291 | serv = MyXMLRPCServer(("localhost", 0), requestHandler, |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 292 | logRequests=False, bind_and_activate=False) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 293 | try: |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 294 | serv.server_bind() |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 295 | global ADDR, PORT, URL |
| 296 | ADDR, PORT = serv.socket.getsockname() |
| 297 | #connect to IP address directly. This avoids socket.create_connection() |
| 298 | #trying to connect to to "localhost" using all address families, which |
| 299 | #causes slowdown e.g. on vista which supports AF_INET6. The server listens |
| 300 | #on AF_INET only. |
| 301 | URL = "http://%s:%d"%(ADDR, PORT) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 302 | serv.server_activate() |
| 303 | serv.register_introspection_functions() |
| 304 | serv.register_multicall_functions() |
| 305 | serv.register_function(pow) |
| 306 | serv.register_function(lambda x,y: x+y, 'add') |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 307 | serv.register_function(my_function) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 308 | serv.register_instance(TestInstanceClass()) |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 309 | evt.set() |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 310 | |
| 311 | # handle up to 'numrequests' requests |
| 312 | while numrequests > 0: |
| 313 | serv.handle_request() |
| 314 | numrequests -= 1 |
| 315 | |
| 316 | except socket.timeout: |
| 317 | pass |
| 318 | finally: |
| 319 | serv.socket.close() |
| 320 | PORT = None |
| 321 | evt.set() |
| 322 | |
Kristján Valur Jónsson | 1f2a1ae | 2009-12-16 10:50:44 +0000 | [diff] [blame] | 323 | def http_multi_server(evt, numrequests, requestHandler=None): |
| 324 | class TestInstanceClass: |
| 325 | def div(self, x, y): |
| 326 | return x // y |
| 327 | |
| 328 | def _methodHelp(self, name): |
| 329 | if name == 'div': |
| 330 | return 'This is the div function' |
| 331 | |
| 332 | def my_function(): |
| 333 | '''This is my function''' |
| 334 | return True |
| 335 | |
| 336 | class MyXMLRPCServer(xmlrpc.server.MultiPathXMLRPCServer): |
| 337 | def get_request(self): |
| 338 | # Ensure the socket is always non-blocking. On Linux, socket |
| 339 | # attributes are not inherited like they are on *BSD and Windows. |
| 340 | s, port = self.socket.accept() |
| 341 | s.setblocking(True) |
| 342 | return s, port |
| 343 | |
| 344 | if not requestHandler: |
| 345 | requestHandler = xmlrpc.server.SimpleXMLRPCRequestHandler |
| 346 | class MyRequestHandler(requestHandler): |
| 347 | rpc_paths = [] |
| 348 | |
| 349 | serv = MyXMLRPCServer(("localhost", 0), MyRequestHandler, |
| 350 | logRequests=False, bind_and_activate=False) |
| 351 | serv.socket.settimeout(3) |
| 352 | serv.server_bind() |
| 353 | try: |
| 354 | global ADDR, PORT, URL |
| 355 | ADDR, PORT = serv.socket.getsockname() |
| 356 | #connect to IP address directly. This avoids socket.create_connection() |
| 357 | #trying to connect to to "localhost" using all address families, which |
| 358 | #causes slowdown e.g. on vista which supports AF_INET6. The server listens |
| 359 | #on AF_INET only. |
| 360 | URL = "http://%s:%d"%(ADDR, PORT) |
| 361 | serv.server_activate() |
| 362 | paths = ["/foo", "/foo/bar"] |
| 363 | for path in paths: |
| 364 | d = serv.add_dispatcher(path, xmlrpc.server.SimpleXMLRPCDispatcher()) |
| 365 | d.register_introspection_functions() |
| 366 | d.register_multicall_functions() |
| 367 | serv.get_dispatcher(paths[0]).register_function(pow) |
| 368 | serv.get_dispatcher(paths[1]).register_function(lambda x,y: x+y, 'add') |
| 369 | evt.set() |
| 370 | |
| 371 | # handle up to 'numrequests' requests |
| 372 | while numrequests > 0: |
| 373 | serv.handle_request() |
| 374 | numrequests -= 1 |
| 375 | |
| 376 | except socket.timeout: |
| 377 | pass |
| 378 | finally: |
| 379 | serv.socket.close() |
| 380 | PORT = None |
| 381 | evt.set() |
| 382 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 383 | # This function prevents errors like: |
| 384 | # <ProtocolError for localhost:57527/RPC2: 500 Internal Server Error> |
| 385 | def is_unavailable_exception(e): |
| 386 | '''Returns True if the given ProtocolError is the product of a server-side |
| 387 | exception caused by the 'temporarily unavailable' response sometimes |
| 388 | given by operations on non-blocking sockets.''' |
| 389 | |
| 390 | # sometimes we get a -1 error code and/or empty headers |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 391 | try: |
| 392 | if e.errcode == -1 or e.headers is None: |
| 393 | return True |
| 394 | exc_mess = e.headers.get('X-exception') |
| 395 | except AttributeError: |
| 396 | # Ignore socket.errors here. |
| 397 | exc_mess = str(e) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 398 | |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 399 | if exc_mess and 'temporarily unavailable' in exc_mess.lower(): |
| 400 | return True |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 401 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 402 | def make_request_and_skipIf(condition, reason): |
| 403 | # If we skip the test, we have to make a request because the |
| 404 | # the server created in setUp blocks expecting one to come in. |
| 405 | if not condition: |
| 406 | return lambda func: func |
| 407 | def decorator(func): |
| 408 | def make_request_and_skip(self): |
| 409 | try: |
| 410 | xmlrpclib.ServerProxy(URL).my_function() |
| 411 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 412 | if not is_unavailable_exception(e): |
| 413 | raise |
| 414 | raise unittest.SkipTest(reason) |
| 415 | return make_request_and_skip |
| 416 | return decorator |
| 417 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 418 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 419 | class BaseServerTestCase(unittest.TestCase): |
| 420 | requestHandler = None |
Kristján Valur Jónsson | f6087ca | 2009-07-12 22:45:18 +0000 | [diff] [blame] | 421 | request_count = 1 |
Kristján Valur Jónsson | 1f2a1ae | 2009-12-16 10:50:44 +0000 | [diff] [blame] | 422 | threadFunc = staticmethod(http_server) |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 423 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 424 | def setUp(self): |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 425 | # enable traceback reporting |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 426 | xmlrpc.server.SimpleXMLRPCServer._send_traceback_header = True |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 427 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 428 | self.evt = threading.Event() |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 429 | # start server thread to handle requests |
Kristján Valur Jónsson | f6087ca | 2009-07-12 22:45:18 +0000 | [diff] [blame] | 430 | serv_args = (self.evt, self.request_count, self.requestHandler) |
Kristján Valur Jónsson | 1f2a1ae | 2009-12-16 10:50:44 +0000 | [diff] [blame] | 431 | threading.Thread(target=self.threadFunc, args=serv_args).start() |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 432 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 433 | # wait for the server to be ready |
| 434 | self.evt.wait() |
| 435 | self.evt.clear() |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 436 | |
| 437 | def tearDown(self): |
| 438 | # wait on the server thread to terminate |
Christian Heimes | 576bf65 | 2007-12-08 17:09:18 +0000 | [diff] [blame] | 439 | self.evt.wait(4.0) |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 440 | # XXX this code does not work, and in fact stop_serving doesn't exist. |
Benjamin Peterson | 1e637b7 | 2008-06-11 21:55:48 +0000 | [diff] [blame] | 441 | if not self.evt.is_set(): |
Christian Heimes | 576bf65 | 2007-12-08 17:09:18 +0000 | [diff] [blame] | 442 | self.evt.set() |
| 443 | stop_serving() |
| 444 | raise RuntimeError("timeout reached, test has failed") |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 445 | |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 446 | # disable traceback reporting |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 447 | xmlrpc.server.SimpleXMLRPCServer._send_traceback_header = False |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 448 | |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 449 | class SimpleServerTestCase(BaseServerTestCase): |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 450 | def test_simple1(self): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 451 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 452 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 453 | self.assertEqual(p.pow(6,8), 6**8) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 454 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 455 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 456 | if not is_unavailable_exception(e): |
| 457 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 458 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 459 | |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 460 | def test_nonascii(self): |
| 461 | start_string = 'P\N{LATIN SMALL LETTER Y WITH CIRCUMFLEX}t' |
| 462 | end_string = 'h\N{LATIN SMALL LETTER O WITH HORN}n' |
| 463 | try: |
| 464 | p = xmlrpclib.ServerProxy(URL) |
| 465 | self.assertEqual(p.add(start_string, end_string), |
| 466 | start_string + end_string) |
| 467 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 468 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 469 | if not is_unavailable_exception(e): |
| 470 | # protocol error; provide additional information in test output |
| 471 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
| 472 | |
Georg Brandl | fceab5a | 2008-01-19 20:08:23 +0000 | [diff] [blame] | 473 | # [ch] The test 404 is causing lots of false alarms. |
| 474 | def XXXtest_404(self): |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 475 | # send POST with http.client, it should return 404 header and |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 476 | # 'Not Found' message. |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 477 | conn = httplib.client.HTTPConnection(ADDR, PORT) |
Christian Heimes | 99d73f2 | 2007-12-08 16:13:06 +0000 | [diff] [blame] | 478 | conn.request('POST', '/this-is-not-valid') |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 479 | response = conn.getresponse() |
| 480 | conn.close() |
| 481 | |
| 482 | self.assertEqual(response.status, 404) |
| 483 | self.assertEqual(response.reason, 'Not Found') |
| 484 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 485 | def test_introspection1(self): |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 486 | expected_methods = set(['pow', 'div', 'my_function', 'add', |
| 487 | 'system.listMethods', 'system.methodHelp', |
| 488 | 'system.methodSignature', 'system.multicall']) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 489 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 490 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 491 | meth = p.system.listMethods() |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 492 | self.assertEqual(set(meth), expected_methods) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 493 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 494 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 495 | if not is_unavailable_exception(e): |
| 496 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 497 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 498 | |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 499 | |
| 500 | def test_introspection2(self): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 501 | try: |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 502 | # test _methodHelp() |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 503 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 504 | divhelp = p.system.methodHelp('div') |
| 505 | self.assertEqual(divhelp, 'This is the div function') |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 506 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 507 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 508 | if not is_unavailable_exception(e): |
| 509 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 510 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 511 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 512 | @make_request_and_skipIf(sys.flags.optimize >= 2, |
| 513 | "Docstrings are omitted with -O2 and above") |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 514 | def test_introspection3(self): |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 515 | try: |
| 516 | # test native doc |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 517 | p = xmlrpclib.ServerProxy(URL) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 518 | myfunction = p.system.methodHelp('my_function') |
| 519 | self.assertEqual(myfunction, 'This is my function') |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 520 | except (xmlrpclib.ProtocolError, socket.error) as e: |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 521 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 522 | if not is_unavailable_exception(e): |
| 523 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 524 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 525 | |
| 526 | def test_introspection4(self): |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 527 | # the SimpleXMLRPCServer doesn't support signatures, but |
Thomas Wouters | ed03b41 | 2007-08-28 21:37:11 +0000 | [diff] [blame] | 528 | # at least check that we can try making the call |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 529 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 530 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 531 | divsig = p.system.methodSignature('div') |
| 532 | self.assertEqual(divsig, 'signatures not supported') |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 533 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 534 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 535 | if not is_unavailable_exception(e): |
| 536 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 537 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 538 | |
| 539 | def test_multicall(self): |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 540 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 541 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 542 | multicall = xmlrpclib.MultiCall(p) |
| 543 | multicall.add(2,3) |
| 544 | multicall.pow(6,8) |
| 545 | multicall.div(127,42) |
| 546 | add_result, pow_result, div_result = multicall() |
| 547 | self.assertEqual(add_result, 2+3) |
| 548 | self.assertEqual(pow_result, 6**8) |
| 549 | self.assertEqual(div_result, 127//42) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 550 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 551 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 552 | if not is_unavailable_exception(e): |
| 553 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 554 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 555 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 556 | def test_non_existing_multicall(self): |
| 557 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 558 | p = xmlrpclib.ServerProxy(URL) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 559 | multicall = xmlrpclib.MultiCall(p) |
| 560 | multicall.this_is_not_exists() |
| 561 | result = multicall() |
| 562 | |
| 563 | # result.results contains; |
Martin v. Löwis | 250ad61 | 2008-04-07 05:43:42 +0000 | [diff] [blame] | 564 | # [{'faultCode': 1, 'faultString': '<class \'exceptions.Exception\'>:' |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 565 | # 'method "this_is_not_exists" is not supported'>}] |
| 566 | |
| 567 | self.assertEqual(result.results[0]['faultCode'], 1) |
| 568 | self.assertEqual(result.results[0]['faultString'], |
Martin v. Löwis | 250ad61 | 2008-04-07 05:43:42 +0000 | [diff] [blame] | 569 | '<class \'Exception\'>:method "this_is_not_exists" ' |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 570 | 'is not supported') |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 571 | except (xmlrpclib.ProtocolError, socket.error) as e: |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 572 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 573 | if not is_unavailable_exception(e): |
| 574 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 575 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 576 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 577 | def test_dotted_attribute(self): |
| 578 | # Raises an AttributeError because private methods are not allowed. |
| 579 | self.assertRaises(AttributeError, |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 580 | xmlrpc.server.resolve_dotted_attribute, str, '__add') |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 581 | |
Benjamin Peterson | c9c0f20 | 2009-06-30 23:06:06 +0000 | [diff] [blame] | 582 | self.assertTrue(xmlrpc.server.resolve_dotted_attribute(str, 'title')) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 583 | # Get the test to run faster by sending a request with test_simple1. |
| 584 | # This avoids waiting for the socket timeout. |
| 585 | self.test_simple1() |
| 586 | |
Kristján Valur Jónsson | 1f2a1ae | 2009-12-16 10:50:44 +0000 | [diff] [blame] | 587 | class MultiPathServerTestCase(BaseServerTestCase): |
| 588 | threadFunc = staticmethod(http_multi_server) |
| 589 | request_count = 2 |
| 590 | def test_path1(self): |
| 591 | p = xmlrpclib.ServerProxy(URL+"/foo") |
| 592 | self.assertEqual(p.pow(6,8), 6**8) |
| 593 | self.assertRaises(xmlrpclib.Fault, p.add, 6, 8) |
| 594 | def test_path2(self): |
| 595 | p = xmlrpclib.ServerProxy(URL+"/foo/bar") |
| 596 | self.assertEqual(p.add(6,8), 6+8) |
| 597 | self.assertRaises(xmlrpclib.Fault, p.pow, 6, 8) |
| 598 | |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 599 | #A test case that verifies that a server using the HTTP/1.1 keep-alive mechanism |
| 600 | #does indeed serve subsequent requests on the same connection |
Kristján Valur Jónsson | f6087ca | 2009-07-12 22:45:18 +0000 | [diff] [blame] | 601 | class BaseKeepaliveServerTestCase(BaseServerTestCase): |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 602 | #a request handler that supports keep-alive and logs requests into a |
| 603 | #class variable |
| 604 | class RequestHandler(xmlrpc.server.SimpleXMLRPCRequestHandler): |
| 605 | parentClass = xmlrpc.server.SimpleXMLRPCRequestHandler |
| 606 | protocol_version = 'HTTP/1.1' |
| 607 | myRequests = [] |
| 608 | def handle(self): |
| 609 | self.myRequests.append([]) |
Kristján Valur Jónsson | f6087ca | 2009-07-12 22:45:18 +0000 | [diff] [blame] | 610 | self.reqidx = len(self.myRequests)-1 |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 611 | return self.parentClass.handle(self) |
| 612 | def handle_one_request(self): |
| 613 | result = self.parentClass.handle_one_request(self) |
Kristján Valur Jónsson | f6087ca | 2009-07-12 22:45:18 +0000 | [diff] [blame] | 614 | self.myRequests[self.reqidx].append(self.raw_requestline) |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 615 | return result |
| 616 | |
| 617 | requestHandler = RequestHandler |
| 618 | def setUp(self): |
| 619 | #clear request log |
| 620 | self.RequestHandler.myRequests = [] |
| 621 | return BaseServerTestCase.setUp(self) |
| 622 | |
Kristján Valur Jónsson | f6087ca | 2009-07-12 22:45:18 +0000 | [diff] [blame] | 623 | #A test case that verifies that a server using the HTTP/1.1 keep-alive mechanism |
| 624 | #does indeed serve subsequent requests on the same connection |
| 625 | class KeepaliveServerTestCase1(BaseKeepaliveServerTestCase): |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 626 | def test_two(self): |
| 627 | p = xmlrpclib.ServerProxy(URL) |
Kristján Valur Jónsson | 1bbb19a | 2009-07-11 21:57:16 +0000 | [diff] [blame] | 628 | #do three requests. |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 629 | self.assertEqual(p.pow(6,8), 6**8) |
| 630 | self.assertEqual(p.pow(6,8), 6**8) |
Kristján Valur Jónsson | 1bbb19a | 2009-07-11 21:57:16 +0000 | [diff] [blame] | 631 | self.assertEqual(p.pow(6,8), 6**8) |
| 632 | |
| 633 | #they should have all been handled by a single request handler |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 634 | self.assertEqual(len(self.RequestHandler.myRequests), 1) |
Kristján Valur Jónsson | 1bbb19a | 2009-07-11 21:57:16 +0000 | [diff] [blame] | 635 | |
| 636 | #check that we did at least two (the third may be pending append |
| 637 | #due to thread scheduling) |
| 638 | self.assertGreaterEqual(len(self.RequestHandler.myRequests[-1]), 2) |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 639 | |
Kristján Valur Jónsson | f6087ca | 2009-07-12 22:45:18 +0000 | [diff] [blame] | 640 | #test special attribute access on the serverproxy, through the __call__ |
| 641 | #function. |
| 642 | class KeepaliveServerTestCase2(BaseKeepaliveServerTestCase): |
| 643 | #ask for two keepalive requests to be handled. |
| 644 | request_count=2 |
| 645 | |
| 646 | def test_close(self): |
| 647 | p = xmlrpclib.ServerProxy(URL) |
| 648 | #do some requests with close. |
| 649 | self.assertEqual(p.pow(6,8), 6**8) |
| 650 | self.assertEqual(p.pow(6,8), 6**8) |
| 651 | self.assertEqual(p.pow(6,8), 6**8) |
| 652 | p("close")() #this should trigger a new keep-alive request |
| 653 | self.assertEqual(p.pow(6,8), 6**8) |
| 654 | self.assertEqual(p.pow(6,8), 6**8) |
| 655 | self.assertEqual(p.pow(6,8), 6**8) |
| 656 | |
| 657 | #they should have all been two request handlers, each having logged at least |
| 658 | #two complete requests |
| 659 | self.assertEqual(len(self.RequestHandler.myRequests), 2) |
| 660 | self.assertGreaterEqual(len(self.RequestHandler.myRequests[-1]), 2) |
| 661 | self.assertGreaterEqual(len(self.RequestHandler.myRequests[-2]), 2) |
| 662 | |
| 663 | def test_transport(self): |
| 664 | p = xmlrpclib.ServerProxy(URL) |
| 665 | #do some requests with close. |
| 666 | self.assertEqual(p.pow(6,8), 6**8) |
| 667 | p("transport").close() #same as above, really. |
| 668 | self.assertEqual(p.pow(6,8), 6**8) |
| 669 | self.assertEqual(len(self.RequestHandler.myRequests), 2) |
| 670 | |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 671 | #A test case that verifies that gzip encoding works in both directions |
| 672 | #(for a request and the response) |
| 673 | class GzipServerTestCase(BaseServerTestCase): |
| 674 | #a request handler that supports keep-alive and logs requests into a |
| 675 | #class variable |
| 676 | class RequestHandler(xmlrpc.server.SimpleXMLRPCRequestHandler): |
| 677 | parentClass = xmlrpc.server.SimpleXMLRPCRequestHandler |
| 678 | protocol_version = 'HTTP/1.1' |
| 679 | |
| 680 | def do_POST(self): |
| 681 | #store content of last request in class |
| 682 | self.__class__.content_length = int(self.headers["content-length"]) |
| 683 | return self.parentClass.do_POST(self) |
| 684 | requestHandler = RequestHandler |
| 685 | |
| 686 | class Transport(xmlrpclib.Transport): |
| 687 | #custom transport, stores the response length for our perusal |
| 688 | fake_gzip = False |
| 689 | def parse_response(self, response): |
| 690 | self.response_length=int(response.getheader("content-length", 0)) |
| 691 | return xmlrpclib.Transport.parse_response(self, response) |
| 692 | |
| 693 | def send_content(self, connection, body): |
| 694 | if self.fake_gzip: |
| 695 | #add a lone gzip header to induce decode error remotely |
| 696 | connection.putheader("Content-Encoding", "gzip") |
| 697 | return xmlrpclib.Transport.send_content(self, connection, body) |
| 698 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 699 | def setUp(self): |
| 700 | BaseServerTestCase.setUp(self) |
| 701 | |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 702 | def test_gzip_request(self): |
| 703 | t = self.Transport() |
| 704 | t.encode_threshold = None |
| 705 | p = xmlrpclib.ServerProxy(URL, transport=t) |
| 706 | self.assertEqual(p.pow(6,8), 6**8) |
| 707 | a = self.RequestHandler.content_length |
| 708 | t.encode_threshold = 0 #turn on request encoding |
| 709 | self.assertEqual(p.pow(6,8), 6**8) |
| 710 | b = self.RequestHandler.content_length |
| 711 | self.assertTrue(a>b) |
| 712 | |
| 713 | def test_bad_gzip_request(self): |
| 714 | t = self.Transport() |
| 715 | t.encode_threshold = None |
| 716 | t.fake_gzip = True |
| 717 | p = xmlrpclib.ServerProxy(URL, transport=t) |
| 718 | cm = self.assertRaisesRegexp(xmlrpclib.ProtocolError, |
| 719 | re.compile(r"\b400\b")) |
| 720 | with cm: |
| 721 | p.pow(6, 8) |
| 722 | |
| 723 | def test_gsip_response(self): |
| 724 | t = self.Transport() |
| 725 | p = xmlrpclib.ServerProxy(URL, transport=t) |
| 726 | old = self.requestHandler.encode_threshold |
| 727 | self.requestHandler.encode_threshold = None #no encoding |
| 728 | self.assertEqual(p.pow(6,8), 6**8) |
| 729 | a = t.response_length |
| 730 | self.requestHandler.encode_threshold = 0 #always encode |
| 731 | self.assertEqual(p.pow(6,8), 6**8) |
| 732 | b = t.response_length |
| 733 | self.requestHandler.encode_threshold = old |
| 734 | self.assertTrue(a>b) |
| 735 | |
| 736 | #Test special attributes of the ServerProxy object |
| 737 | class ServerProxyTestCase(unittest.TestCase): |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 738 | def setUp(self): |
| 739 | unittest.TestCase.setUp(self) |
| 740 | if threading: |
| 741 | self.url = URL |
| 742 | else: |
| 743 | # Without threading, http_server() and http_multi_server() will not |
| 744 | # be executed and URL is still equal to None. 'http://' is a just |
| 745 | # enough to choose the scheme (HTTP) |
| 746 | self.url = 'http://' |
| 747 | |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 748 | def test_close(self): |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 749 | p = xmlrpclib.ServerProxy(self.url) |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 750 | self.assertEqual(p('close')(), None) |
| 751 | |
| 752 | def test_transport(self): |
| 753 | t = xmlrpclib.Transport() |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 754 | p = xmlrpclib.ServerProxy(self.url, transport=t) |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 755 | self.assertEqual(p('transport'), t) |
| 756 | |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 757 | # This is a contrived way to make a failure occur on the server side |
| 758 | # in order to test the _send_traceback_header flag on the server |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 759 | class FailingMessageClass(http.client.HTTPMessage): |
| 760 | def get(self, key, failobj=None): |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 761 | key = key.lower() |
| 762 | if key == 'content-length': |
| 763 | return 'I am broken' |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 764 | return super().get(key, failobj) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 765 | |
| 766 | |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 767 | @unittest.skipUnless(threading, 'Threading required for this test.') |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 768 | class FailingServerTestCase(unittest.TestCase): |
| 769 | def setUp(self): |
| 770 | self.evt = threading.Event() |
| 771 | # start server thread to handle requests |
Guido van Rossum | 15c9746 | 2007-11-02 16:10:06 +0000 | [diff] [blame] | 772 | serv_args = (self.evt, 1) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 773 | threading.Thread(target=http_server, args=serv_args).start() |
| 774 | |
Christian Heimes | af98da1 | 2008-01-27 15:18:18 +0000 | [diff] [blame] | 775 | # wait for the server to be ready |
| 776 | self.evt.wait() |
| 777 | self.evt.clear() |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 778 | |
| 779 | def tearDown(self): |
| 780 | # wait on the server thread to terminate |
| 781 | self.evt.wait() |
| 782 | # reset flag |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 783 | xmlrpc.server.SimpleXMLRPCServer._send_traceback_header = False |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 784 | # reset message class |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 785 | default_class = http.client.HTTPMessage |
| 786 | xmlrpc.server.SimpleXMLRPCRequestHandler.MessageClass = default_class |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 787 | |
| 788 | def test_basic(self): |
| 789 | # check that flag is false by default |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 790 | flagval = xmlrpc.server.SimpleXMLRPCServer._send_traceback_header |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 791 | self.assertEqual(flagval, False) |
| 792 | |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 793 | # enable traceback reporting |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 794 | xmlrpc.server.SimpleXMLRPCServer._send_traceback_header = True |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 795 | |
| 796 | # test a call that shouldn't fail just as a smoke test |
| 797 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 798 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 04110fb | 2007-08-24 16:32:05 +0000 | [diff] [blame] | 799 | self.assertEqual(p.pow(6,8), 6**8) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 800 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 801 | # ignore failures due to non-blocking socket 'unavailable' errors |
| 802 | if not is_unavailable_exception(e): |
| 803 | # protocol error; provide additional information in test output |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 804 | self.fail("%s\n%s" % (e, getattr(e, "headers", ""))) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 805 | |
| 806 | def test_fail_no_info(self): |
| 807 | # use the broken message class |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 808 | xmlrpc.server.SimpleXMLRPCRequestHandler.MessageClass = FailingMessageClass |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 809 | |
| 810 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 811 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 812 | p.pow(6,8) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 813 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 814 | # ignore failures due to non-blocking socket 'unavailable' errors |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 815 | if not is_unavailable_exception(e) and hasattr(e, "headers"): |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 816 | # The two server-side error headers shouldn't be sent back in this case |
| 817 | self.assertTrue(e.headers.get("X-exception") is None) |
| 818 | self.assertTrue(e.headers.get("X-traceback") is None) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 819 | else: |
| 820 | self.fail('ProtocolError not raised') |
| 821 | |
| 822 | def test_fail_with_info(self): |
| 823 | # use the broken message class |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 824 | xmlrpc.server.SimpleXMLRPCRequestHandler.MessageClass = FailingMessageClass |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 825 | |
| 826 | # Check that errors in the server send back exception/traceback |
| 827 | # info when flag is set |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 828 | xmlrpc.server.SimpleXMLRPCServer._send_traceback_header = True |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 829 | |
| 830 | try: |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 831 | p = xmlrpclib.ServerProxy(URL) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 832 | p.pow(6,8) |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 833 | except (xmlrpclib.ProtocolError, socket.error) as e: |
| 834 | # ignore failures due to non-blocking socket 'unavailable' errors |
Christian Heimes | 2685563 | 2008-01-27 23:50:43 +0000 | [diff] [blame] | 835 | if not is_unavailable_exception(e) and hasattr(e, "headers"): |
Christian Heimes | 412dc9c | 2008-01-27 18:55:54 +0000 | [diff] [blame] | 836 | # We should get error info in the response |
| 837 | expected_err = "invalid literal for int() with base 10: 'I am broken'" |
Barry Warsaw | 820c120 | 2008-06-12 04:06:45 +0000 | [diff] [blame] | 838 | self.assertEqual(e.headers.get("X-exception"), expected_err) |
| 839 | self.assertTrue(e.headers.get("X-traceback") is not None) |
Guido van Rossum | 61e21b5 | 2007-08-20 19:06:03 +0000 | [diff] [blame] | 840 | else: |
| 841 | self.fail('ProtocolError not raised') |
| 842 | |
R. David Murray | 0548ce0 | 2009-10-26 08:24:14 +0000 | [diff] [blame] | 843 | |
| 844 | @contextlib.contextmanager |
| 845 | def captured_stdout(encoding='utf-8'): |
| 846 | """A variation on support.captured_stdout() which gives a text stream |
| 847 | having a `buffer` attribute. |
| 848 | """ |
| 849 | import io |
| 850 | orig_stdout = sys.stdout |
| 851 | sys.stdout = io.TextIOWrapper(io.BytesIO(), encoding=encoding) |
| 852 | try: |
| 853 | yield sys.stdout |
| 854 | finally: |
| 855 | sys.stdout = orig_stdout |
| 856 | |
| 857 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 858 | class CGIHandlerTestCase(unittest.TestCase): |
| 859 | def setUp(self): |
Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 860 | self.cgi = xmlrpc.server.CGIXMLRPCRequestHandler() |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 861 | |
| 862 | def tearDown(self): |
| 863 | self.cgi = None |
| 864 | |
| 865 | def test_cgi_get(self): |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 866 | with support.EnvironmentVarGuard() as env: |
Walter Dörwald | 155374d | 2009-05-01 19:58:58 +0000 | [diff] [blame] | 867 | env['REQUEST_METHOD'] = 'GET' |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 868 | # if the method is GET and no request_text is given, it runs handle_get |
| 869 | # get sysout output |
R. David Murray | 0548ce0 | 2009-10-26 08:24:14 +0000 | [diff] [blame] | 870 | with captured_stdout(encoding=self.cgi.encoding) as data_out: |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 871 | self.cgi.handle_request() |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 872 | |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 873 | # parse Status header |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 874 | data_out.seek(0) |
| 875 | handle = data_out.read() |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 876 | status = handle.split()[1] |
| 877 | message = ' '.join(handle.split()[2:4]) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 878 | |
Walter Dörwald | b525e18 | 2009-04-26 21:39:21 +0000 | [diff] [blame] | 879 | self.assertEqual(status, '400') |
| 880 | self.assertEqual(message, 'Bad Request') |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 881 | |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 882 | |
| 883 | def test_cgi_xmlrpc_response(self): |
| 884 | data = """<?xml version='1.0'?> |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 885 | <methodCall> |
| 886 | <methodName>test_method</methodName> |
| 887 | <params> |
| 888 | <param> |
| 889 | <value><string>foo</string></value> |
| 890 | </param> |
| 891 | <param> |
| 892 | <value><string>bar</string></value> |
| 893 | </param> |
| 894 | </params> |
| 895 | </methodCall> |
| 896 | """ |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 897 | |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 898 | with support.EnvironmentVarGuard() as env, \ |
R. David Murray | 0548ce0 | 2009-10-26 08:24:14 +0000 | [diff] [blame] | 899 | captured_stdout(encoding=self.cgi.encoding) as data_out, \ |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 900 | support.captured_stdin() as data_in: |
| 901 | data_in.write(data) |
| 902 | data_in.seek(0) |
Walter Dörwald | 155374d | 2009-05-01 19:58:58 +0000 | [diff] [blame] | 903 | env['CONTENT_LENGTH'] = str(len(data)) |
Georg Brandl | c748506 | 2009-04-01 15:53:15 +0000 | [diff] [blame] | 904 | self.cgi.handle_request() |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 905 | data_out.seek(0) |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 906 | |
| 907 | # will respond exception, if so, our goal is achieved ;) |
Nick Coghlan | 6ead552 | 2009-10-18 13:19:33 +0000 | [diff] [blame] | 908 | handle = data_out.read() |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 909 | |
Jeremy Hylton | 1afc169 | 2008-06-18 20:49:58 +0000 | [diff] [blame] | 910 | # start with 44th char so as not to get http header, we just |
| 911 | # need only xml |
Christian Heimes | 255f53b | 2007-12-08 15:33:56 +0000 | [diff] [blame] | 912 | self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, handle[44:]) |
| 913 | |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 914 | # Also test the content-length returned by handle_request |
| 915 | # Using the same test method inorder to avoid all the datapassing |
| 916 | # boilerplate code. |
| 917 | # Test for bug: http://bugs.python.org/issue5040 |
| 918 | |
| 919 | content = handle[handle.find("<?xml"):] |
| 920 | |
| 921 | self.assertEquals( |
| 922 | int(re.search('Content-Length: (\d+)', handle).group(1)), |
| 923 | len(content)) |
| 924 | |
Senthil Kumaran | b3af08f | 2009-04-01 20:20:43 +0000 | [diff] [blame] | 925 | |
Antoine Pitrou | e03866f | 2009-10-30 17:58:27 +0000 | [diff] [blame] | 926 | @support.reap_threads |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 927 | def test_main(): |
| 928 | xmlrpc_tests = [XMLRPCTestCase, HelperTestCase, DateTimeTestCase, |
| 929 | BinaryTestCase, FaultTestCase] |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 930 | xmlrpc_tests.append(SimpleServerTestCase) |
Kristján Valur Jónsson | f6087ca | 2009-07-12 22:45:18 +0000 | [diff] [blame] | 931 | xmlrpc_tests.append(KeepaliveServerTestCase1) |
| 932 | xmlrpc_tests.append(KeepaliveServerTestCase2) |
Kristján Valur Jónsson | aefde24 | 2009-07-19 22:29:24 +0000 | [diff] [blame] | 933 | try: |
| 934 | import gzip |
| 935 | xmlrpc_tests.append(GzipServerTestCase) |
| 936 | except ImportError: |
| 937 | pass #gzip not supported in this build |
Kristján Valur Jónsson | 1f2a1ae | 2009-12-16 10:50:44 +0000 | [diff] [blame] | 938 | xmlrpc_tests.append(MultiPathServerTestCase) |
Kristján Valur Jónsson | 985fc6a | 2009-07-01 10:01:31 +0000 | [diff] [blame] | 939 | xmlrpc_tests.append(ServerProxyTestCase) |
Benjamin Peterson | e1cdfd7 | 2009-01-18 21:02:37 +0000 | [diff] [blame] | 940 | xmlrpc_tests.append(FailingServerTestCase) |
| 941 | xmlrpc_tests.append(CGIHandlerTestCase) |
Guido van Rossum | af554a0 | 2007-08-16 23:48:43 +0000 | [diff] [blame] | 942 | |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 943 | support.run_unittest(*xmlrpc_tests) |
Skip Montanaro | 419abda | 2001-10-01 17:47:44 +0000 | [diff] [blame] | 944 | |
| 945 | if __name__ == "__main__": |
| 946 | test_main() |