blob: 0b8a9639db7510248e24a4a249e5971390339aa6 [file] [log] [blame]
Guido van Rossumb5a755e2007-07-18 18:15:48 +00001import base64
Fred Drakeba613c32005-02-10 18:33:30 +00002import datetime
Skip Montanaro3e7bba92001-10-19 16:06:52 +00003import sys
Guido van Rossumb5a755e2007-07-18 18:15:48 +00004import time
Skip Montanaro419abda2001-10-01 17:47:44 +00005import unittest
6import xmlrpclib
Barry Warsaw04f357c2002-07-23 19:04:11 +00007from test import test_support
Skip Montanaro419abda2001-10-01 17:47:44 +00008
9alist = [{'astring': 'foo@bar.baz.spam',
10 'afloat': 7283.43,
Skip Montanaro3e7bba92001-10-19 16:06:52 +000011 'anint': 2**20,
Guido van Rossume2a383d2007-01-15 16:59:06 +000012 'ashortlong': 2,
Skip Montanaro419abda2001-10-01 17:47:44 +000013 'anotherlist': ['.zyx.41'],
14 'abase64': xmlrpclib.Binary("my dog has fleas"),
Guido van Rossume7ba4952007-06-06 23:52:48 +000015 'boolean': False,
Guido van Rossumef87d6e2007-05-02 19:09:54 +000016 'unicode': '\u4000\u6000\u8000',
17 'ukey\u4000': 'regular value',
Fred Drakeba613c32005-02-10 18:33:30 +000018 'datetime1': xmlrpclib.DateTime('20050210T11:41:23'),
19 'datetime2': xmlrpclib.DateTime(
Guido van Rossumcd16bf62007-06-13 18:07:49 +000020 (2005, 2, 10, 11, 41, 23, 0, 1, -1)),
Fred Drakeba613c32005-02-10 18:33:30 +000021 'datetime3': xmlrpclib.DateTime(
Guido van Rossumcd16bf62007-06-13 18:07:49 +000022 datetime.datetime(2005, 2, 10, 11, 41, 23)),
Guido van Rossumb5a755e2007-07-18 18:15:48 +000023 'datetime4': xmlrpclib.DateTime(
24 datetime.date(2005, 2, 10)),
25 'datetime5': xmlrpclib.DateTime(
26 datetime.time(11, 41, 23)),
Skip Montanaro419abda2001-10-01 17:47:44 +000027 }]
28
29class XMLRPCTestCase(unittest.TestCase):
30
31 def test_dump_load(self):
32 self.assertEquals(alist,
33 xmlrpclib.loads(xmlrpclib.dumps((alist,)))[0][0])
34
Fred Drakeba613c32005-02-10 18:33:30 +000035 def test_dump_bare_datetime(self):
Skip Montanaro174dd222005-05-14 20:54:16 +000036 # This checks that an unwrapped datetime.date object can be handled
37 # by the marshalling code. This can't be done via test_dump_load()
38 # since with use_datetime set to 1 the unmarshaller would create
39 # datetime objects for the 'datetime[123]' keys as well
Guido van Rossumcd16bf62007-06-13 18:07:49 +000040 dt = datetime.datetime(2005, 2, 10, 11, 41, 23)
Fred Drakeba613c32005-02-10 18:33:30 +000041 s = xmlrpclib.dumps((dt,))
Skip Montanaro174dd222005-05-14 20:54:16 +000042 (newdt,), m = xmlrpclib.loads(s, use_datetime=1)
43 self.assertEquals(newdt, dt)
Fred Drakeba613c32005-02-10 18:33:30 +000044 self.assertEquals(m, None)
45
Skip Montanaro174dd222005-05-14 20:54:16 +000046 (newdt,), m = xmlrpclib.loads(s, use_datetime=0)
47 self.assertEquals(newdt, xmlrpclib.DateTime('20050210T11:41:23'))
48
49 def test_dump_bare_date(self):
50 # This checks that an unwrapped datetime.date object can be handled
51 # by the marshalling code. This can't be done via test_dump_load()
52 # since the unmarshaller produces a datetime object
Guido van Rossumcd16bf62007-06-13 18:07:49 +000053 d = datetime.datetime(2005, 2, 10, 11, 41, 23).date()
Skip Montanaro174dd222005-05-14 20:54:16 +000054 s = xmlrpclib.dumps((d,))
55 (newd,), m = xmlrpclib.loads(s, use_datetime=1)
56 self.assertEquals(newd.date(), d)
57 self.assertEquals(newd.time(), datetime.time(0, 0, 0))
58 self.assertEquals(m, None)
59
60 (newdt,), m = xmlrpclib.loads(s, use_datetime=0)
61 self.assertEquals(newdt, xmlrpclib.DateTime('20050210T00:00:00'))
62
63 def test_dump_bare_time(self):
64 # This checks that an unwrapped datetime.time object can be handled
65 # by the marshalling code. This can't be done via test_dump_load()
66 # since the unmarshaller produces a datetime object
Guido van Rossumcd16bf62007-06-13 18:07:49 +000067 t = datetime.datetime(2005, 2, 10, 11, 41, 23).time()
Skip Montanaro174dd222005-05-14 20:54:16 +000068 s = xmlrpclib.dumps((t,))
69 (newt,), m = xmlrpclib.loads(s, use_datetime=1)
70 today = datetime.datetime.now().date().strftime("%Y%m%d")
71 self.assertEquals(newt.time(), t)
72 self.assertEquals(newt.date(), datetime.datetime.now().date())
73 self.assertEquals(m, None)
74
75 (newdt,), m = xmlrpclib.loads(s, use_datetime=0)
76 self.assertEquals(newdt, xmlrpclib.DateTime('%sT11:41:23'%today))
77
Andrew M. Kuchlingbdb39012005-12-04 19:11:17 +000078 def test_bug_1164912 (self):
79 d = xmlrpclib.DateTime()
Tim Peters536cf992005-12-25 23:18:31 +000080 ((new_d,), dummy) = xmlrpclib.loads(xmlrpclib.dumps((d,),
Andrew M. Kuchlingbdb39012005-12-04 19:11:17 +000081 methodresponse=True))
82 self.assert_(isinstance(new_d.value, str))
83
84 # Check that the output of dumps() is still an 8-bit string
85 s = xmlrpclib.dumps((new_d,), methodresponse=True)
86 self.assert_(isinstance(s, str))
87
Thomas Wouters89f507f2006-12-13 04:49:30 +000088 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 Montanaro3e7bba92001-10-19 16:06:52 +000097 def test_dump_big_long(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +000098 self.assertRaises(OverflowError, xmlrpclib.dumps, (2**99,))
Skip Montanaro3e7bba92001-10-19 16:06:52 +000099
100 def test_dump_bad_dict(self):
101 self.assertRaises(TypeError, xmlrpclib.dumps, ({(1,2,3): 1},))
102
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000103 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 Montanaro3e7bba92001-10-19 16:06:52 +0000115 def test_dump_big_int(self):
Guido van Rossume2a383d2007-01-15 16:59:06 +0000116 if sys.maxint > 2**31-1:
Skip Montanaro3e7bba92001-10-19 16:06:52 +0000117 self.assertRaises(OverflowError, xmlrpclib.dumps,
Guido van Rossume2a383d2007-01-15 16:59:06 +0000118 (int(2**34),))
Skip Montanaro3e7bba92001-10-19 16:06:52 +0000119
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000120 xmlrpclib.dumps((xmlrpclib.MAXINT, xmlrpclib.MININT))
121 self.assertRaises(OverflowError, xmlrpclib.dumps, (xmlrpclib.MAXINT+1,))
122 self.assertRaises(OverflowError, xmlrpclib.dumps, (xmlrpclib.MININT-1,))
123
124 def dummy_write(s):
125 pass
126
127 m = xmlrpclib.Marshaller()
128 m.dump_int(xmlrpclib.MAXINT, dummy_write)
129 m.dump_int(xmlrpclib.MININT, dummy_write)
130 self.assertRaises(OverflowError, m.dump_int, xmlrpclib.MAXINT+1, dummy_write)
131 self.assertRaises(OverflowError, m.dump_int, xmlrpclib.MININT-1, dummy_write)
132
133
Andrew M. Kuchling0b852032003-04-25 00:27:24 +0000134 def test_dump_none(self):
135 value = alist + [None]
136 arg1 = (alist + [None],)
137 strg = xmlrpclib.dumps(arg1, allow_none=True)
138 self.assertEquals(value,
139 xmlrpclib.loads(strg)[0][0])
140 self.assertRaises(TypeError, xmlrpclib.dumps, (arg1,))
141
Fred Drake22c07062005-02-11 17:59:08 +0000142 def test_default_encoding_issues(self):
143 # SF bug #1115989: wrong decoding in '_stringify'
144 utf8 = """<?xml version='1.0' encoding='iso-8859-1'?>
145 <params>
146 <param><value>
147 <string>abc \x95</string>
148 </value></param>
149 <param><value>
150 <struct>
151 <member>
152 <name>def \x96</name>
153 <value><string>ghi \x97</string></value>
154 </member>
155 </struct>
156 </value></param>
157 </params>
158 """
Tim Petersf754f5f2005-04-08 18:00:59 +0000159
160 # sys.setdefaultencoding() normally doesn't exist after site.py is
Guido van Rossume7ba4952007-06-06 23:52:48 +0000161 # loaded. Re-initializing sys again is the way to get it back. :-(
Fred Drake22c07062005-02-11 17:59:08 +0000162 old_encoding = sys.getdefaultencoding()
Tim Petersf754f5f2005-04-08 18:00:59 +0000163 setdefaultencoding_existed = hasattr(sys, "setdefaultencoding")
Guido van Rossume7ba4952007-06-06 23:52:48 +0000164 import imp
165 imp.init_builtin('sys')
Fred Drake22c07062005-02-11 17:59:08 +0000166 sys.setdefaultencoding("iso-8859-1")
167 try:
168 (s, d), m = xmlrpclib.loads(utf8)
169 finally:
170 sys.setdefaultencoding(old_encoding)
Tim Petersf754f5f2005-04-08 18:00:59 +0000171 if not setdefaultencoding_existed:
172 del sys.setdefaultencoding
173
Guido van Rossumcc2b0162007-02-11 06:12:03 +0000174 items = list(d.items())
Walter Dörwald9b775532007-06-08 14:30:53 +0000175 self.assertEquals(s, "abc \x95")
176 self.assert_(isinstance(s, str))
177 self.assertEquals(items, [("def \x96", "ghi \x97")])
178 self.assert_(isinstance(items[0][0], str))
179 self.assert_(isinstance(items[0][1], str))
Fred Drake22c07062005-02-11 17:59:08 +0000180
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000181
182class HelperTestCase(unittest.TestCase):
183 def test_escape(self):
184 self.assertEqual(xmlrpclib.escape("a&b"), "a&amp;b")
185 self.assertEqual(xmlrpclib.escape("a<b"), "a&lt;b")
186 self.assertEqual(xmlrpclib.escape("a>b"), "a&gt;b")
187
188class FaultTestCase(unittest.TestCase):
189 def test_repr(self):
190 f = xmlrpclib.Fault(42, 'Test Fault')
191 self.assertEqual(repr(f), "<Fault 42: 'Test Fault'>")
192 self.assertEqual(repr(f), str(f))
193
194 def test_dump_fault(self):
195 f = xmlrpclib.Fault(42, 'Test Fault')
196 s = xmlrpclib.dumps((f,))
197 (newf,), m = xmlrpclib.loads(s)
198 self.assertEquals(newf, {'faultCode': 42, 'faultString': 'Test Fault'})
199 self.assertEquals(m, None)
200
201 s = xmlrpclib.Marshaller().dumps(f)
202 self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, s)
203
204
205class DateTimeTestCase(unittest.TestCase):
206 def test_default(self):
207 t = xmlrpclib.DateTime()
208
209 def test_time(self):
210 d = 1181399930.036952
211 t = xmlrpclib.DateTime(d)
212 self.assertEqual(str(t), time.strftime("%Y%m%dT%H:%M:%S", time.localtime(d)))
213
214 def test_time_tuple(self):
215 d = (2007,6,9,10,38,50,5,160,0)
216 t = xmlrpclib.DateTime(d)
217 self.assertEqual(str(t), '20070609T10:38:50')
218
219 def test_time_struct(self):
220 d = time.localtime(1181399930.036952)
221 t = xmlrpclib.DateTime(d)
222 self.assertEqual(str(t), time.strftime("%Y%m%dT%H:%M:%S", d))
223
224 def test_datetime_datetime(self):
225 d = datetime.datetime(2007,1,2,3,4,5)
226 t = xmlrpclib.DateTime(d)
227 self.assertEqual(str(t), '20070102T03:04:05')
228
229 def test_datetime_date(self):
230 d = datetime.date(2007,9,8)
231 t = xmlrpclib.DateTime(d)
232 self.assertEqual(str(t), '20070908T00:00:00')
233
234 def test_datetime_time(self):
235 d = datetime.time(13,17,19)
236 # allow for date rollover by checking today's or tomorrow's dates
237 dd1 = datetime.datetime.now().date()
238 dd2 = dd1 + datetime.timedelta(days=1)
239 vals = (dd1.strftime('%Y%m%dT13:17:19'),
240 dd2.strftime('%Y%m%dT13:17:19'))
241 t = xmlrpclib.DateTime(d)
242 self.assertEqual(str(t) in vals, True)
243
244 def test_repr(self):
245 d = datetime.datetime(2007,1,2,3,4,5)
246 t = xmlrpclib.DateTime(d)
247 val ="<DateTime '20070102T03:04:05' at %x>" % id(t)
248 self.assertEqual(repr(t), val)
249
250 def test_decode(self):
251 d = ' 20070908T07:11:13 '
252 t1 = xmlrpclib.DateTime()
253 t1.decode(d)
254 tref = xmlrpclib.DateTime(datetime.datetime(2007,9,8,7,11,13))
255 self.assertEqual(t1, tref)
256
257 t2 = xmlrpclib._datetime(d)
258 self.assertEqual(t1, tref)
259
260class BinaryTestCase(unittest.TestCase):
261 def test_default(self):
262 t = xmlrpclib.Binary()
263 self.assertEqual(str(t), '')
264
265 def test_string(self):
266 d = '\x01\x02\x03abc123\xff\xfe'
267 t = xmlrpclib.Binary(d)
268 self.assertEqual(str(t), d)
269
270 def test_decode(self):
271 d = '\x01\x02\x03abc123\xff\xfe'
272 de = base64.encodestring(d)
273 t1 = xmlrpclib.Binary()
274 t1.decode(de)
275 self.assertEqual(str(t1), d)
276
277 t2 = xmlrpclib._binary(de)
278 self.assertEqual(str(t2), d)
279
280
Skip Montanaro419abda2001-10-01 17:47:44 +0000281def test_main():
Guido van Rossumb5a755e2007-07-18 18:15:48 +0000282 test_support.run_unittest(XMLRPCTestCase, HelperTestCase,
283 DateTimeTestCase, BinaryTestCase, FaultTestCase)
Skip Montanaro419abda2001-10-01 17:47:44 +0000284
285
286if __name__ == "__main__":
287 test_main()