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