summaryrefslogtreecommitdiffstats
path: root/Lib/test/test_xmlrpc.py
diff options
context:
space:
mode:
Diffstat (limited to 'Lib/test/test_xmlrpc.py')
-rw-r--r--Lib/test/test_xmlrpc.py135
1 files changed, 134 insertions, 1 deletions
diff --git a/Lib/test/test_xmlrpc.py b/Lib/test/test_xmlrpc.py
index 791cca7..0b8a963 100644
--- a/Lib/test/test_xmlrpc.py
+++ b/Lib/test/test_xmlrpc.py
@@ -1,5 +1,7 @@
+import base64
import datetime
import sys
+import time
import unittest
import xmlrpclib
from test import test_support
@@ -18,6 +20,10 @@ alist = [{'astring': 'foo@bar.baz.spam',
(2005, 2, 10, 11, 41, 23, 0, 1, -1)),
'datetime3': xmlrpclib.DateTime(
datetime.datetime(2005, 2, 10, 11, 41, 23)),
+ 'datetime4': xmlrpclib.DateTime(
+ datetime.date(2005, 2, 10)),
+ 'datetime5': xmlrpclib.DateTime(
+ datetime.time(11, 41, 23)),
}]
class XMLRPCTestCase(unittest.TestCase):
@@ -94,11 +100,37 @@ class XMLRPCTestCase(unittest.TestCase):
def test_dump_bad_dict(self):
self.assertRaises(TypeError, xmlrpclib.dumps, ({(1,2,3): 1},))
+ def test_dump_recursive_seq(self):
+ l = [1,2,3]
+ t = [3,4,5,l]
+ l.append(t)
+ self.assertRaises(TypeError, xmlrpclib.dumps, (l,))
+
+ def test_dump_recursive_dict(self):
+ d = {'1':1, '2':1}
+ t = {'3':3, 'd':d}
+ d['t'] = t
+ self.assertRaises(TypeError, xmlrpclib.dumps, (d,))
+
def test_dump_big_int(self):
if sys.maxint > 2**31-1:
self.assertRaises(OverflowError, xmlrpclib.dumps,
(int(2**34),))
+ xmlrpclib.dumps((xmlrpclib.MAXINT, xmlrpclib.MININT))
+ self.assertRaises(OverflowError, xmlrpclib.dumps, (xmlrpclib.MAXINT+1,))
+ self.assertRaises(OverflowError, xmlrpclib.dumps, (xmlrpclib.MININT-1,))
+
+ def dummy_write(s):
+ pass
+
+ m = xmlrpclib.Marshaller()
+ m.dump_int(xmlrpclib.MAXINT, dummy_write)
+ m.dump_int(xmlrpclib.MININT, dummy_write)
+ self.assertRaises(OverflowError, m.dump_int, xmlrpclib.MAXINT+1, dummy_write)
+ self.assertRaises(OverflowError, m.dump_int, xmlrpclib.MININT-1, dummy_write)
+
+
def test_dump_none(self):
value = alist + [None]
arg1 = (alist + [None],)
@@ -146,8 +178,109 @@ class XMLRPCTestCase(unittest.TestCase):
self.assert_(isinstance(items[0][0], str))
self.assert_(isinstance(items[0][1], str))
+
+class HelperTestCase(unittest.TestCase):
+ def test_escape(self):
+ self.assertEqual(xmlrpclib.escape("a&b"), "a&b")
+ self.assertEqual(xmlrpclib.escape("a<b"), "a&lt;b")
+ self.assertEqual(xmlrpclib.escape("a>b"), "a&gt;b")
+
+class FaultTestCase(unittest.TestCase):
+ def test_repr(self):
+ f = xmlrpclib.Fault(42, 'Test Fault')
+ self.assertEqual(repr(f), "<Fault 42: 'Test Fault'>")
+ self.assertEqual(repr(f), str(f))
+
+ def test_dump_fault(self):
+ f = xmlrpclib.Fault(42, 'Test Fault')
+ s = xmlrpclib.dumps((f,))
+ (newf,), m = xmlrpclib.loads(s)
+ self.assertEquals(newf, {'faultCode': 42, 'faultString': 'Test Fault'})
+ self.assertEquals(m, None)
+
+ s = xmlrpclib.Marshaller().dumps(f)
+ self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, s)
+
+
+class DateTimeTestCase(unittest.TestCase):
+ def test_default(self):
+ t = xmlrpclib.DateTime()
+
+ def test_time(self):
+ d = 1181399930.036952
+ t = xmlrpclib.DateTime(d)
+ self.assertEqual(str(t), time.strftime("%Y%m%dT%H:%M:%S", time.localtime(d)))
+
+ def test_time_tuple(self):
+ d = (2007,6,9,10,38,50,5,160,0)
+ t = xmlrpclib.DateTime(d)
+ self.assertEqual(str(t), '20070609T10:38:50')
+
+ def test_time_struct(self):
+ d = time.localtime(1181399930.036952)
+ t = xmlrpclib.DateTime(d)
+ self.assertEqual(str(t), time.strftime("%Y%m%dT%H:%M:%S", d))
+
+ def test_datetime_datetime(self):
+ d = datetime.datetime(2007,1,2,3,4,5)
+ t = xmlrpclib.DateTime(d)
+ self.assertEqual(str(t), '20070102T03:04:05')
+
+ def test_datetime_date(self):
+ d = datetime.date(2007,9,8)
+ t = xmlrpclib.DateTime(d)
+ self.assertEqual(str(t), '20070908T00:00:00')
+
+ def test_datetime_time(self):
+ d = datetime.time(13,17,19)
+ # allow for date rollover by checking today's or tomorrow's dates
+ dd1 = datetime.datetime.now().date()
+ dd2 = dd1 + datetime.timedelta(days=1)
+ vals = (dd1.strftime('%Y%m%dT13:17:19'),
+ dd2.strftime('%Y%m%dT13:17:19'))
+ t = xmlrpclib.DateTime(d)
+ self.assertEqual(str(t) in vals, True)
+
+ def test_repr(self):
+ d = datetime.datetime(2007,1,2,3,4,5)
+ t = xmlrpclib.DateTime(d)
+ val ="<DateTime '20070102T03:04:05' at %x>" % id(t)
+ self.assertEqual(repr(t), val)
+
+ def test_decode(self):
+ d = ' 20070908T07:11:13 '
+ t1 = xmlrpclib.DateTime()
+ t1.decode(d)
+ tref = xmlrpclib.DateTime(datetime.datetime(2007,9,8,7,11,13))
+ self.assertEqual(t1, tref)
+
+ t2 = xmlrpclib._datetime(d)
+ self.assertEqual(t1, tref)
+
+class BinaryTestCase(unittest.TestCase):
+ def test_default(self):
+ t = xmlrpclib.Binary()
+ self.assertEqual(str(t), '')
+
+ def test_string(self):
+ d = '\x01\x02\x03abc123\xff\xfe'
+ t = xmlrpclib.Binary(d)
+ self.assertEqual(str(t), d)
+
+ def test_decode(self):
+ d = '\x01\x02\x03abc123\xff\xfe'
+ de = base64.encodestring(d)
+ t1 = xmlrpclib.Binary()
+ t1.decode(de)
+ self.assertEqual(str(t1), d)
+
+ t2 = xmlrpclib._binary(de)
+ self.assertEqual(str(t2), d)
+
+
def test_main():
- test_support.run_unittest(XMLRPCTestCase)
+ test_support.run_unittest(XMLRPCTestCase, HelperTestCase,
+ DateTimeTestCase, BinaryTestCase, FaultTestCase)
if __name__ == "__main__":