2007-07-13 07:43:44 -03:00
|
|
|
import base64
|
2005-02-10 14:33:30 -04:00
|
|
|
import datetime
|
2001-10-19 13:06:52 -03:00
|
|
|
import sys
|
2007-07-13 07:43:44 -03:00
|
|
|
import time
|
2001-10-01 14:47:44 -03:00
|
|
|
import unittest
|
|
|
|
import xmlrpclib
|
2007-08-14 10:35:00 -03:00
|
|
|
import SimpleXMLRPCServer
|
2007-08-17 16:16:44 -03:00
|
|
|
import mimetools
|
2007-12-07 05:07:10 -04:00
|
|
|
import httplib
|
|
|
|
import socket
|
2008-11-24 18:00:29 -04:00
|
|
|
import StringIO
|
2007-12-07 05:07:10 -04:00
|
|
|
import os
|
2009-04-01 17:26:33 -03:00
|
|
|
import re
|
2002-07-23 16:04:11 -03:00
|
|
|
from test import test_support
|
2001-10-01 14:47:44 -03:00
|
|
|
|
2010-04-27 20:14:58 -03:00
|
|
|
try:
|
|
|
|
import threading
|
|
|
|
except ImportError:
|
|
|
|
threading = None
|
|
|
|
|
2005-02-11 13:59:08 -04:00
|
|
|
try:
|
|
|
|
unicode
|
|
|
|
except NameError:
|
|
|
|
have_unicode = False
|
|
|
|
else:
|
|
|
|
have_unicode = True
|
|
|
|
|
2001-10-01 14:47:44 -03:00
|
|
|
alist = [{'astring': 'foo@bar.baz.spam',
|
|
|
|
'afloat': 7283.43,
|
2001-10-19 13:06:52 -03:00
|
|
|
'anint': 2**20,
|
|
|
|
'ashortlong': 2L,
|
2001-10-01 14:47:44 -03:00
|
|
|
'anotherlist': ['.zyx.41'],
|
|
|
|
'abase64': xmlrpclib.Binary("my dog has fleas"),
|
|
|
|
'boolean': xmlrpclib.False,
|
2004-06-05 09:33:27 -03:00
|
|
|
'unicode': u'\u4000\u6000\u8000',
|
|
|
|
u'ukey\u4000': 'regular value',
|
2005-02-10 14:33:30 -04:00
|
|
|
'datetime1': xmlrpclib.DateTime('20050210T11:41:23'),
|
|
|
|
'datetime2': xmlrpclib.DateTime(
|
|
|
|
(2005, 02, 10, 11, 41, 23, 0, 1, -1)),
|
|
|
|
'datetime3': xmlrpclib.DateTime(
|
|
|
|
datetime.datetime(2005, 02, 10, 11, 41, 23)),
|
2001-10-01 14:47:44 -03:00
|
|
|
}]
|
|
|
|
|
|
|
|
class XMLRPCTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def test_dump_load(self):
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(alist,
|
|
|
|
xmlrpclib.loads(xmlrpclib.dumps((alist,)))[0][0])
|
2001-10-01 14:47:44 -03:00
|
|
|
|
2005-02-10 14:33:30 -04:00
|
|
|
def test_dump_bare_datetime(self):
|
2005-05-14 17:54:16 -03:00
|
|
|
# This checks that an unwrapped datetime.date object can be handled
|
|
|
|
# by the marshalling code. This can't be done via test_dump_load()
|
|
|
|
# since with use_datetime set to 1 the unmarshaller would create
|
|
|
|
# datetime objects for the 'datetime[123]' keys as well
|
2005-02-10 14:33:30 -04:00
|
|
|
dt = datetime.datetime(2005, 02, 10, 11, 41, 23)
|
|
|
|
s = xmlrpclib.dumps((dt,))
|
2005-05-14 17:54:16 -03:00
|
|
|
(newdt,), m = xmlrpclib.loads(s, use_datetime=1)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(newdt, dt)
|
|
|
|
self.assertEqual(m, None)
|
2005-02-10 14:33:30 -04:00
|
|
|
|
2005-05-14 17:54:16 -03:00
|
|
|
(newdt,), m = xmlrpclib.loads(s, use_datetime=0)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(newdt, xmlrpclib.DateTime('20050210T11:41:23'))
|
2005-05-14 17:54:16 -03:00
|
|
|
|
2008-04-18 17:35:46 -03:00
|
|
|
def test_datetime_before_1900(self):
|
2008-04-20 22:45:57 -03:00
|
|
|
# same as before but with a date before 1900
|
2008-04-18 17:35:46 -03:00
|
|
|
dt = datetime.datetime(1, 02, 10, 11, 41, 23)
|
|
|
|
s = xmlrpclib.dumps((dt,))
|
|
|
|
(newdt,), m = xmlrpclib.loads(s, use_datetime=1)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(newdt, dt)
|
|
|
|
self.assertEqual(m, None)
|
2008-04-18 17:35:46 -03:00
|
|
|
|
|
|
|
(newdt,), m = xmlrpclib.loads(s, use_datetime=0)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(newdt, xmlrpclib.DateTime('00010210T11:41:23'))
|
2008-04-18 17:35:46 -03:00
|
|
|
|
2008-02-23 12:23:05 -04:00
|
|
|
def test_cmp_datetime_DateTime(self):
|
|
|
|
now = datetime.datetime.now()
|
|
|
|
dt = xmlrpclib.DateTime(now.timetuple())
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(dt == now)
|
|
|
|
self.assertTrue(now == dt)
|
2008-02-23 12:23:05 -04:00
|
|
|
then = now + datetime.timedelta(seconds=4)
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(then >= dt)
|
|
|
|
self.assertTrue(dt < then)
|
2005-05-14 17:54:16 -03:00
|
|
|
|
2005-12-04 15:11:17 -04:00
|
|
|
def test_bug_1164912 (self):
|
|
|
|
d = xmlrpclib.DateTime()
|
2005-12-25 19:18:31 -04:00
|
|
|
((new_d,), dummy) = xmlrpclib.loads(xmlrpclib.dumps((d,),
|
2005-12-04 15:11:17 -04:00
|
|
|
methodresponse=True))
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(new_d.value, str)
|
2005-12-04 15:11:17 -04:00
|
|
|
|
|
|
|
# Check that the output of dumps() is still an 8-bit string
|
|
|
|
s = xmlrpclib.dumps((new_d,), methodresponse=True)
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(s, str)
|
2005-12-04 15:11:17 -04:00
|
|
|
|
2006-11-19 14:51:54 -04:00
|
|
|
def test_newstyle_class(self):
|
|
|
|
class T(object):
|
|
|
|
pass
|
|
|
|
t = T()
|
|
|
|
t.x = 100
|
|
|
|
t.y = "Hello"
|
|
|
|
((t2,), dummy) = xmlrpclib.loads(xmlrpclib.dumps((t,)))
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(t2, t.__dict__)
|
2006-11-19 14:51:54 -04:00
|
|
|
|
2001-10-19 13:06:52 -03:00
|
|
|
def test_dump_big_long(self):
|
|
|
|
self.assertRaises(OverflowError, xmlrpclib.dumps, (2L**99,))
|
|
|
|
|
|
|
|
def test_dump_bad_dict(self):
|
|
|
|
self.assertRaises(TypeError, xmlrpclib.dumps, ({(1,2,3): 1},))
|
|
|
|
|
2007-07-13 07:43:44 -03:00
|
|
|
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,))
|
|
|
|
|
2001-10-19 13:06:52 -03:00
|
|
|
def test_dump_big_int(self):
|
|
|
|
if sys.maxint > 2L**31-1:
|
|
|
|
self.assertRaises(OverflowError, xmlrpclib.dumps,
|
|
|
|
(int(2L**34),))
|
|
|
|
|
2007-07-13 07:43:44 -03:00
|
|
|
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)
|
|
|
|
|
|
|
|
|
2003-04-24 21:27:24 -03:00
|
|
|
def test_dump_none(self):
|
|
|
|
value = alist + [None]
|
|
|
|
arg1 = (alist + [None],)
|
|
|
|
strg = xmlrpclib.dumps(arg1, allow_none=True)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(value,
|
|
|
|
xmlrpclib.loads(strg)[0][0])
|
2003-04-24 21:27:24 -03:00
|
|
|
self.assertRaises(TypeError, xmlrpclib.dumps, (arg1,))
|
|
|
|
|
2005-02-11 13:59:08 -04:00
|
|
|
def test_default_encoding_issues(self):
|
|
|
|
# SF bug #1115989: wrong decoding in '_stringify'
|
|
|
|
utf8 = """<?xml version='1.0' encoding='iso-8859-1'?>
|
|
|
|
<params>
|
|
|
|
<param><value>
|
|
|
|
<string>abc \x95</string>
|
|
|
|
</value></param>
|
|
|
|
<param><value>
|
|
|
|
<struct>
|
|
|
|
<member>
|
|
|
|
<name>def \x96</name>
|
|
|
|
<value><string>ghi \x97</string></value>
|
|
|
|
</member>
|
|
|
|
</struct>
|
|
|
|
</value></param>
|
|
|
|
</params>
|
|
|
|
"""
|
2005-04-08 15:00:59 -03:00
|
|
|
|
|
|
|
# sys.setdefaultencoding() normally doesn't exist after site.py is
|
2009-10-17 12:09:41 -03:00
|
|
|
# loaded. Import a temporary fresh copy to get access to it
|
|
|
|
# but then restore the original copy to avoid messing with
|
|
|
|
# other potentially modified sys module attributes
|
2005-02-11 13:59:08 -04:00
|
|
|
old_encoding = sys.getdefaultencoding()
|
2009-10-17 12:09:41 -03:00
|
|
|
with test_support.CleanImport('sys'):
|
|
|
|
import sys as temp_sys
|
|
|
|
temp_sys.setdefaultencoding("iso-8859-1")
|
|
|
|
try:
|
|
|
|
(s, d), m = xmlrpclib.loads(utf8)
|
|
|
|
finally:
|
|
|
|
temp_sys.setdefaultencoding(old_encoding)
|
2005-04-08 15:00:59 -03:00
|
|
|
|
2005-02-11 13:59:08 -04:00
|
|
|
items = d.items()
|
|
|
|
if have_unicode:
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(s, u"abc \x95")
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(s, unicode)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(items, [(u"def \x96", u"ghi \x97")])
|
2010-01-24 12:58:36 -04:00
|
|
|
self.assertIsInstance(items[0][0], unicode)
|
|
|
|
self.assertIsInstance(items[0][1], unicode)
|
2005-02-11 13:59:08 -04:00
|
|
|
else:
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(s, "abc \xc2\x95")
|
|
|
|
self.assertEqual(items, [("def \xc2\x96", "ghi \xc2\x97")])
|
2005-02-11 13:59:08 -04:00
|
|
|
|
2007-07-13 07:43:44 -03:00
|
|
|
|
|
|
|
class HelperTestCase(unittest.TestCase):
|
|
|
|
def test_escape(self):
|
|
|
|
self.assertEqual(xmlrpclib.escape("a&b"), "a&b")
|
|
|
|
self.assertEqual(xmlrpclib.escape("a<b"), "a<b")
|
|
|
|
self.assertEqual(xmlrpclib.escape("a>b"), "a>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)
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(newf, {'faultCode': 42, 'faultString': 'Test Fault'})
|
|
|
|
self.assertEqual(m, None)
|
2007-07-13 07:43:44 -03:00
|
|
|
|
|
|
|
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_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)
|
|
|
|
|
|
|
|
|
2009-01-14 06:50:57 -04:00
|
|
|
ADDR = PORT = URL = None
|
2007-08-14 10:35:00 -03:00
|
|
|
|
2008-01-26 03:26:12 -04:00
|
|
|
# The evt is set twice. First when the server is ready to serve.
|
|
|
|
# Second when the server has been shutdown. The user must clear
|
|
|
|
# the event after it has been set the first time to catch the second set.
|
2009-06-28 18:04:17 -03:00
|
|
|
def http_server(evt, numrequests, requestHandler=None):
|
2007-08-14 10:35:00 -03:00
|
|
|
class TestInstanceClass:
|
|
|
|
def div(self, x, y):
|
|
|
|
return x // y
|
|
|
|
|
2007-12-07 05:07:10 -04:00
|
|
|
def _methodHelp(self, name):
|
|
|
|
if name == 'div':
|
|
|
|
return 'This is the div function'
|
|
|
|
|
|
|
|
def my_function():
|
|
|
|
'''This is my function'''
|
|
|
|
return True
|
|
|
|
|
2008-03-28 03:34:03 -03:00
|
|
|
class MyXMLRPCServer(SimpleXMLRPCServer.SimpleXMLRPCServer):
|
|
|
|
def get_request(self):
|
|
|
|
# Ensure the socket is always non-blocking. On Linux, socket
|
|
|
|
# attributes are not inherited like they are on *BSD and Windows.
|
|
|
|
s, port = self.socket.accept()
|
|
|
|
s.setblocking(True)
|
|
|
|
return s, port
|
|
|
|
|
2009-06-28 18:04:17 -03:00
|
|
|
if not requestHandler:
|
|
|
|
requestHandler = SimpleXMLRPCServer.SimpleXMLRPCRequestHandler
|
|
|
|
serv = MyXMLRPCServer(("localhost", 0), requestHandler,
|
2009-01-14 06:50:57 -04:00
|
|
|
logRequests=False, bind_and_activate=False)
|
2007-08-14 10:35:00 -03:00
|
|
|
try:
|
|
|
|
serv.socket.settimeout(3)
|
|
|
|
serv.server_bind()
|
2009-01-14 06:50:57 -04:00
|
|
|
global ADDR, PORT, URL
|
|
|
|
ADDR, PORT = serv.socket.getsockname()
|
|
|
|
#connect to IP address directly. This avoids socket.create_connection()
|
2011-10-19 04:39:35 -03:00
|
|
|
#trying to connect to "localhost" using all address families, which
|
2009-01-14 06:50:57 -04:00
|
|
|
#causes slowdown e.g. on vista which supports AF_INET6. The server listens
|
|
|
|
#on AF_INET only.
|
|
|
|
URL = "http://%s:%d"%(ADDR, PORT)
|
2007-08-14 10:35:00 -03:00
|
|
|
serv.server_activate()
|
|
|
|
serv.register_introspection_functions()
|
|
|
|
serv.register_multicall_functions()
|
|
|
|
serv.register_function(pow)
|
|
|
|
serv.register_function(lambda x,y: x+y, 'add')
|
2007-12-07 05:07:10 -04:00
|
|
|
serv.register_function(my_function)
|
2007-08-14 10:35:00 -03:00
|
|
|
serv.register_instance(TestInstanceClass())
|
2008-01-26 03:26:12 -04:00
|
|
|
evt.set()
|
2007-08-14 10:35:00 -03:00
|
|
|
|
|
|
|
# handle up to 'numrequests' requests
|
|
|
|
while numrequests > 0:
|
|
|
|
serv.handle_request()
|
|
|
|
numrequests -= 1
|
|
|
|
|
|
|
|
except socket.timeout:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
serv.socket.close()
|
|
|
|
PORT = None
|
|
|
|
evt.set()
|
|
|
|
|
2009-08-27 20:13:18 -03:00
|
|
|
def http_multi_server(evt, numrequests, requestHandler=None):
|
|
|
|
class TestInstanceClass:
|
|
|
|
def div(self, x, y):
|
|
|
|
return x // y
|
|
|
|
|
|
|
|
def _methodHelp(self, name):
|
|
|
|
if name == 'div':
|
|
|
|
return 'This is the div function'
|
|
|
|
|
|
|
|
def my_function():
|
|
|
|
'''This is my function'''
|
|
|
|
return True
|
|
|
|
|
|
|
|
class MyXMLRPCServer(SimpleXMLRPCServer.MultiPathXMLRPCServer):
|
|
|
|
def get_request(self):
|
|
|
|
# Ensure the socket is always non-blocking. On Linux, socket
|
|
|
|
# attributes are not inherited like they are on *BSD and Windows.
|
|
|
|
s, port = self.socket.accept()
|
|
|
|
s.setblocking(True)
|
|
|
|
return s, port
|
|
|
|
|
|
|
|
if not requestHandler:
|
|
|
|
requestHandler = SimpleXMLRPCServer.SimpleXMLRPCRequestHandler
|
|
|
|
class MyRequestHandler(requestHandler):
|
|
|
|
rpc_paths = []
|
|
|
|
|
|
|
|
serv = MyXMLRPCServer(("localhost", 0), MyRequestHandler,
|
|
|
|
logRequests=False, bind_and_activate=False)
|
|
|
|
serv.socket.settimeout(3)
|
|
|
|
serv.server_bind()
|
|
|
|
try:
|
|
|
|
global ADDR, PORT, URL
|
|
|
|
ADDR, PORT = serv.socket.getsockname()
|
|
|
|
#connect to IP address directly. This avoids socket.create_connection()
|
2011-10-19 04:39:35 -03:00
|
|
|
#trying to connect to "localhost" using all address families, which
|
2009-08-27 20:13:18 -03:00
|
|
|
#causes slowdown e.g. on vista which supports AF_INET6. The server listens
|
|
|
|
#on AF_INET only.
|
|
|
|
URL = "http://%s:%d"%(ADDR, PORT)
|
|
|
|
serv.server_activate()
|
|
|
|
paths = ["/foo", "/foo/bar"]
|
|
|
|
for path in paths:
|
|
|
|
d = serv.add_dispatcher(path, SimpleXMLRPCServer.SimpleXMLRPCDispatcher())
|
|
|
|
d.register_introspection_functions()
|
|
|
|
d.register_multicall_functions()
|
|
|
|
serv.get_dispatcher(paths[0]).register_function(pow)
|
|
|
|
serv.get_dispatcher(paths[1]).register_function(lambda x,y: x+y, 'add')
|
|
|
|
evt.set()
|
|
|
|
|
|
|
|
# handle up to 'numrequests' requests
|
|
|
|
while numrequests > 0:
|
|
|
|
serv.handle_request()
|
|
|
|
numrequests -= 1
|
|
|
|
|
|
|
|
except socket.timeout:
|
|
|
|
pass
|
|
|
|
finally:
|
|
|
|
serv.socket.close()
|
|
|
|
PORT = None
|
|
|
|
evt.set()
|
|
|
|
|
2008-01-26 04:26:00 -04:00
|
|
|
# This function prevents errors like:
|
|
|
|
# <ProtocolError for localhost:57527/RPC2: 500 Internal Server Error>
|
2007-08-26 22:15:34 -03:00
|
|
|
def is_unavailable_exception(e):
|
|
|
|
'''Returns True if the given ProtocolError is the product of a server-side
|
|
|
|
exception caused by the 'temporarily unavailable' response sometimes
|
|
|
|
given by operations on non-blocking sockets.'''
|
2008-01-26 03:26:12 -04:00
|
|
|
|
2007-08-26 22:15:34 -03:00
|
|
|
# sometimes we get a -1 error code and/or empty headers
|
2008-01-27 14:19:04 -04:00
|
|
|
try:
|
|
|
|
if e.errcode == -1 or e.headers is None:
|
|
|
|
return True
|
|
|
|
exc_mess = e.headers.get('X-exception')
|
|
|
|
except AttributeError:
|
|
|
|
# Ignore socket.errors here.
|
|
|
|
exc_mess = str(e)
|
2007-08-26 22:15:34 -03:00
|
|
|
|
|
|
|
if exc_mess and 'temporarily unavailable' in exc_mess.lower():
|
|
|
|
return True
|
|
|
|
|
|
|
|
return False
|
|
|
|
|
2010-04-27 20:14:58 -03:00
|
|
|
@unittest.skipUnless(threading, 'Threading required for this test.')
|
2009-06-28 18:04:17 -03:00
|
|
|
class BaseServerTestCase(unittest.TestCase):
|
|
|
|
requestHandler = None
|
2009-07-12 19:42:08 -03:00
|
|
|
request_count = 1
|
2009-08-27 20:13:18 -03:00
|
|
|
threadFunc = staticmethod(http_server)
|
2010-04-27 20:14:58 -03:00
|
|
|
|
2007-08-14 10:35:00 -03:00
|
|
|
def setUp(self):
|
2007-08-17 16:16:44 -03:00
|
|
|
# enable traceback reporting
|
|
|
|
SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = True
|
|
|
|
|
2007-08-14 10:35:00 -03:00
|
|
|
self.evt = threading.Event()
|
2007-08-17 16:16:44 -03:00
|
|
|
# start server thread to handle requests
|
2009-07-12 19:42:08 -03:00
|
|
|
serv_args = (self.evt, self.request_count, self.requestHandler)
|
2009-08-27 20:13:18 -03:00
|
|
|
threading.Thread(target=self.threadFunc, args=serv_args).start()
|
2007-08-14 10:35:00 -03:00
|
|
|
|
2008-01-26 03:26:12 -04:00
|
|
|
# wait for the server to be ready
|
2009-06-28 18:04:17 -03:00
|
|
|
self.evt.wait(10)
|
2008-01-26 03:26:12 -04:00
|
|
|
self.evt.clear()
|
2007-08-14 10:35:00 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# wait on the server thread to terminate
|
2009-06-28 18:04:17 -03:00
|
|
|
self.evt.wait(10)
|
2007-08-14 10:35:00 -03:00
|
|
|
|
2007-08-17 16:16:44 -03:00
|
|
|
# disable traceback reporting
|
|
|
|
SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = False
|
|
|
|
|
2009-06-28 18:04:17 -03:00
|
|
|
# NOTE: The tests in SimpleServerTestCase will ignore failures caused by
|
|
|
|
# "temporarily unavailable" exceptions raised in SimpleXMLRPCServer. This
|
|
|
|
# condition occurs infrequently on some platforms, frequently on others, and
|
|
|
|
# is apparently caused by using SimpleXMLRPCServer with a non-blocking socket
|
|
|
|
# If the server class is updated at some point in the future to handle this
|
|
|
|
# situation more gracefully, these tests should be modified appropriately.
|
|
|
|
|
|
|
|
class SimpleServerTestCase(BaseServerTestCase):
|
2007-08-14 10:35:00 -03:00
|
|
|
def test_simple1(self):
|
2007-08-20 21:16:21 -03:00
|
|
|
try:
|
2009-01-14 06:50:57 -04:00
|
|
|
p = xmlrpclib.ServerProxy(URL)
|
2007-08-20 21:16:21 -03:00
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
2008-01-27 13:11:11 -04:00
|
|
|
except (xmlrpclib.ProtocolError, socket.error), e:
|
2007-08-26 22:15:34 -03:00
|
|
|
# ignore failures due to non-blocking socket 'unavailable' errors
|
|
|
|
if not is_unavailable_exception(e):
|
|
|
|
# protocol error; provide additional information in test output
|
2008-01-27 16:03:13 -04:00
|
|
|
self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
|
2007-08-14 10:35:00 -03:00
|
|
|
|
2009-04-01 17:26:33 -03:00
|
|
|
def test_nonascii(self):
|
|
|
|
start_string = 'P\N{LATIN SMALL LETTER Y WITH CIRCUMFLEX}t'
|
|
|
|
end_string = 'h\N{LATIN SMALL LETTER O WITH HORN}n'
|
|
|
|
|
|
|
|
try:
|
|
|
|
p = xmlrpclib.ServerProxy(URL)
|
|
|
|
self.assertEqual(p.add(start_string, end_string),
|
|
|
|
start_string + end_string)
|
|
|
|
except (xmlrpclib.ProtocolError, socket.error) as e:
|
|
|
|
# ignore failures due to non-blocking socket unavailable errors.
|
|
|
|
if not is_unavailable_exception(e):
|
|
|
|
# protocol error; provide additional information in test output
|
|
|
|
self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
|
|
|
|
|
2011-09-22 20:15:32 -03:00
|
|
|
def test_unicode_host(self):
|
|
|
|
server = xmlrpclib.ServerProxy(u"http://%s:%d/RPC2"%(ADDR, PORT))
|
|
|
|
self.assertEqual(server.add("a", u"\xe9"), u"a\xe9")
|
2009-04-01 17:26:33 -03:00
|
|
|
|
2008-01-19 12:39:27 -04:00
|
|
|
# [ch] The test 404 is causing lots of false alarms.
|
|
|
|
def XXXtest_404(self):
|
2007-12-07 05:07:10 -04:00
|
|
|
# send POST with httplib, it should return 404 header and
|
|
|
|
# 'Not Found' message.
|
2009-01-14 06:50:57 -04:00
|
|
|
conn = httplib.HTTPConnection(ADDR, PORT)
|
2007-12-07 05:07:10 -04:00
|
|
|
conn.request('POST', '/this-is-not-valid')
|
|
|
|
response = conn.getresponse()
|
|
|
|
conn.close()
|
|
|
|
|
|
|
|
self.assertEqual(response.status, 404)
|
|
|
|
self.assertEqual(response.reason, 'Not Found')
|
|
|
|
|
2007-08-14 10:35:00 -03:00
|
|
|
def test_introspection1(self):
|
2007-08-20 21:16:21 -03:00
|
|
|
try:
|
2009-01-14 06:50:57 -04:00
|
|
|
p = xmlrpclib.ServerProxy(URL)
|
2007-08-20 21:16:21 -03:00
|
|
|
meth = p.system.listMethods()
|
2007-12-07 05:07:10 -04:00
|
|
|
expected_methods = set(['pow', 'div', 'my_function', 'add',
|
|
|
|
'system.listMethods', 'system.methodHelp',
|
|
|
|
'system.methodSignature', 'system.multicall'])
|
2007-08-20 21:16:21 -03:00
|
|
|
self.assertEqual(set(meth), expected_methods)
|
2008-01-27 13:11:11 -04:00
|
|
|
except (xmlrpclib.ProtocolError, socket.error), e:
|
2007-08-26 22:15:34 -03:00
|
|
|
# ignore failures due to non-blocking socket 'unavailable' errors
|
|
|
|
if not is_unavailable_exception(e):
|
|
|
|
# protocol error; provide additional information in test output
|
2008-01-27 16:03:13 -04:00
|
|
|
self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
|
2007-08-14 10:35:00 -03:00
|
|
|
|
|
|
|
def test_introspection2(self):
|
2007-08-20 21:16:21 -03:00
|
|
|
try:
|
2007-12-07 05:07:10 -04:00
|
|
|
# test _methodHelp()
|
2009-01-14 06:50:57 -04:00
|
|
|
p = xmlrpclib.ServerProxy(URL)
|
2007-08-20 21:16:21 -03:00
|
|
|
divhelp = p.system.methodHelp('div')
|
|
|
|
self.assertEqual(divhelp, 'This is the div function')
|
2008-01-27 13:11:11 -04:00
|
|
|
except (xmlrpclib.ProtocolError, socket.error), e:
|
2007-08-26 22:15:34 -03:00
|
|
|
# ignore failures due to non-blocking socket 'unavailable' errors
|
|
|
|
if not is_unavailable_exception(e):
|
|
|
|
# protocol error; provide additional information in test output
|
2008-01-27 16:03:13 -04:00
|
|
|
self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
|
2007-08-14 10:35:00 -03:00
|
|
|
|
2010-02-22 20:24:49 -04:00
|
|
|
@unittest.skipIf(sys.flags.optimize >= 2,
|
|
|
|
"Docstrings are omitted with -O2 and above")
|
2007-08-14 10:35:00 -03:00
|
|
|
def test_introspection3(self):
|
2007-12-07 05:07:10 -04:00
|
|
|
try:
|
|
|
|
# test native doc
|
2009-01-14 06:50:57 -04:00
|
|
|
p = xmlrpclib.ServerProxy(URL)
|
2007-12-07 05:07:10 -04:00
|
|
|
myfunction = p.system.methodHelp('my_function')
|
|
|
|
self.assertEqual(myfunction, 'This is my function')
|
2008-01-27 13:11:11 -04:00
|
|
|
except (xmlrpclib.ProtocolError, socket.error), e:
|
2007-12-07 05:07:10 -04:00
|
|
|
# ignore failures due to non-blocking socket 'unavailable' errors
|
|
|
|
if not is_unavailable_exception(e):
|
|
|
|
# protocol error; provide additional information in test output
|
2008-01-27 16:03:13 -04:00
|
|
|
self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
|
2007-12-07 05:07:10 -04:00
|
|
|
|
|
|
|
def test_introspection4(self):
|
2007-08-14 10:35:00 -03:00
|
|
|
# the SimpleXMLRPCServer doesn't support signatures, but
|
2007-08-26 22:15:34 -03:00
|
|
|
# at least check that we can try making the call
|
2007-08-20 21:16:21 -03:00
|
|
|
try:
|
2009-01-14 06:50:57 -04:00
|
|
|
p = xmlrpclib.ServerProxy(URL)
|
2007-08-20 21:16:21 -03:00
|
|
|
divsig = p.system.methodSignature('div')
|
|
|
|
self.assertEqual(divsig, 'signatures not supported')
|
2008-01-27 13:11:11 -04:00
|
|
|
except (xmlrpclib.ProtocolError, socket.error), e:
|
2007-08-26 22:15:34 -03:00
|
|
|
# ignore failures due to non-blocking socket 'unavailable' errors
|
|
|
|
if not is_unavailable_exception(e):
|
|
|
|
# protocol error; provide additional information in test output
|
2008-01-27 16:03:13 -04:00
|
|
|
self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
|
2007-08-14 10:35:00 -03:00
|
|
|
|
|
|
|
def test_multicall(self):
|
2007-08-20 21:16:21 -03:00
|
|
|
try:
|
2009-01-14 06:50:57 -04:00
|
|
|
p = xmlrpclib.ServerProxy(URL)
|
2007-08-20 21:16:21 -03:00
|
|
|
multicall = xmlrpclib.MultiCall(p)
|
|
|
|
multicall.add(2,3)
|
|
|
|
multicall.pow(6,8)
|
|
|
|
multicall.div(127,42)
|
|
|
|
add_result, pow_result, div_result = multicall()
|
|
|
|
self.assertEqual(add_result, 2+3)
|
|
|
|
self.assertEqual(pow_result, 6**8)
|
|
|
|
self.assertEqual(div_result, 127//42)
|
2008-01-27 13:11:11 -04:00
|
|
|
except (xmlrpclib.ProtocolError, socket.error), e:
|
2007-08-26 22:15:34 -03:00
|
|
|
# ignore failures due to non-blocking socket 'unavailable' errors
|
|
|
|
if not is_unavailable_exception(e):
|
|
|
|
# protocol error; provide additional information in test output
|
2008-01-27 16:03:13 -04:00
|
|
|
self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
|
2007-08-14 10:35:00 -03:00
|
|
|
|
2007-12-07 05:07:10 -04:00
|
|
|
def test_non_existing_multicall(self):
|
|
|
|
try:
|
2009-01-14 06:50:57 -04:00
|
|
|
p = xmlrpclib.ServerProxy(URL)
|
2007-12-07 05:07:10 -04:00
|
|
|
multicall = xmlrpclib.MultiCall(p)
|
|
|
|
multicall.this_is_not_exists()
|
|
|
|
result = multicall()
|
|
|
|
|
|
|
|
# result.results contains;
|
|
|
|
# [{'faultCode': 1, 'faultString': '<type \'exceptions.Exception\'>:'
|
|
|
|
# 'method "this_is_not_exists" is not supported'>}]
|
|
|
|
|
|
|
|
self.assertEqual(result.results[0]['faultCode'], 1)
|
|
|
|
self.assertEqual(result.results[0]['faultString'],
|
|
|
|
'<type \'exceptions.Exception\'>:method "this_is_not_exists" '
|
|
|
|
'is not supported')
|
2008-01-27 13:11:11 -04:00
|
|
|
except (xmlrpclib.ProtocolError, socket.error), e:
|
2007-12-07 05:07:10 -04:00
|
|
|
# ignore failures due to non-blocking socket 'unavailable' errors
|
|
|
|
if not is_unavailable_exception(e):
|
|
|
|
# protocol error; provide additional information in test output
|
2008-01-27 16:03:13 -04:00
|
|
|
self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
|
2007-12-07 05:07:10 -04:00
|
|
|
|
|
|
|
def test_dotted_attribute(self):
|
2008-03-23 01:08:30 -03:00
|
|
|
# Raises an AttributeError because private methods are not allowed.
|
2007-12-07 05:07:10 -04:00
|
|
|
self.assertRaises(AttributeError,
|
|
|
|
SimpleXMLRPCServer.resolve_dotted_attribute, str, '__add')
|
|
|
|
|
2009-06-30 19:57:08 -03:00
|
|
|
self.assertTrue(SimpleXMLRPCServer.resolve_dotted_attribute(str, 'title'))
|
2008-03-23 01:08:30 -03:00
|
|
|
# Get the test to run faster by sending a request with test_simple1.
|
|
|
|
# This avoids waiting for the socket timeout.
|
|
|
|
self.test_simple1()
|
2007-08-14 10:35:00 -03:00
|
|
|
|
2009-08-27 20:13:18 -03:00
|
|
|
class MultiPathServerTestCase(BaseServerTestCase):
|
|
|
|
threadFunc = staticmethod(http_multi_server)
|
|
|
|
request_count = 2
|
|
|
|
def test_path1(self):
|
|
|
|
p = xmlrpclib.ServerProxy(URL+"/foo")
|
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
|
|
|
self.assertRaises(xmlrpclib.Fault, p.add, 6, 8)
|
|
|
|
def test_path2(self):
|
|
|
|
p = xmlrpclib.ServerProxy(URL+"/foo/bar")
|
|
|
|
self.assertEqual(p.add(6,8), 6+8)
|
|
|
|
self.assertRaises(xmlrpclib.Fault, p.pow, 6, 8)
|
|
|
|
|
2009-06-28 18:04:17 -03:00
|
|
|
#A test case that verifies that a server using the HTTP/1.1 keep-alive mechanism
|
|
|
|
#does indeed serve subsequent requests on the same connection
|
2009-07-12 19:42:08 -03:00
|
|
|
class BaseKeepaliveServerTestCase(BaseServerTestCase):
|
2009-06-28 18:04:17 -03:00
|
|
|
#a request handler that supports keep-alive and logs requests into a
|
|
|
|
#class variable
|
|
|
|
class RequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
|
|
|
|
parentClass = SimpleXMLRPCServer.SimpleXMLRPCRequestHandler
|
|
|
|
protocol_version = 'HTTP/1.1'
|
|
|
|
myRequests = []
|
|
|
|
def handle(self):
|
|
|
|
self.myRequests.append([])
|
2009-07-12 19:42:08 -03:00
|
|
|
self.reqidx = len(self.myRequests)-1
|
2009-06-28 18:04:17 -03:00
|
|
|
return self.parentClass.handle(self)
|
|
|
|
def handle_one_request(self):
|
|
|
|
result = self.parentClass.handle_one_request(self)
|
2009-07-12 19:42:08 -03:00
|
|
|
self.myRequests[self.reqidx].append(self.raw_requestline)
|
2009-06-28 18:04:17 -03:00
|
|
|
return result
|
|
|
|
|
|
|
|
requestHandler = RequestHandler
|
|
|
|
def setUp(self):
|
|
|
|
#clear request log
|
|
|
|
self.RequestHandler.myRequests = []
|
|
|
|
return BaseServerTestCase.setUp(self)
|
|
|
|
|
2009-07-12 19:42:08 -03:00
|
|
|
#A test case that verifies that a server using the HTTP/1.1 keep-alive mechanism
|
|
|
|
#does indeed serve subsequent requests on the same connection
|
|
|
|
class KeepaliveServerTestCase1(BaseKeepaliveServerTestCase):
|
2009-06-28 18:04:17 -03:00
|
|
|
def test_two(self):
|
|
|
|
p = xmlrpclib.ServerProxy(URL)
|
2009-07-11 05:44:43 -03:00
|
|
|
#do three requests.
|
2009-06-28 18:04:17 -03:00
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
2009-07-11 05:44:43 -03:00
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
|
|
|
|
|
|
|
#they should have all been handled by a single request handler
|
2009-06-28 18:04:17 -03:00
|
|
|
self.assertEqual(len(self.RequestHandler.myRequests), 1)
|
2009-07-11 05:44:43 -03:00
|
|
|
|
|
|
|
#check that we did at least two (the third may be pending append
|
|
|
|
#due to thread scheduling)
|
|
|
|
self.assertGreaterEqual(len(self.RequestHandler.myRequests[-1]), 2)
|
2009-06-28 18:04:17 -03:00
|
|
|
|
2009-07-12 19:42:08 -03:00
|
|
|
#test special attribute access on the serverproxy, through the __call__
|
|
|
|
#function.
|
|
|
|
class KeepaliveServerTestCase2(BaseKeepaliveServerTestCase):
|
|
|
|
#ask for two keepalive requests to be handled.
|
|
|
|
request_count=2
|
|
|
|
|
|
|
|
def test_close(self):
|
|
|
|
p = xmlrpclib.ServerProxy(URL)
|
|
|
|
#do some requests with close.
|
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
|
|
|
p("close")() #this should trigger a new keep-alive request
|
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
|
|
|
|
|
|
|
#they should have all been two request handlers, each having logged at least
|
|
|
|
#two complete requests
|
|
|
|
self.assertEqual(len(self.RequestHandler.myRequests), 2)
|
|
|
|
self.assertGreaterEqual(len(self.RequestHandler.myRequests[-1]), 2)
|
|
|
|
self.assertGreaterEqual(len(self.RequestHandler.myRequests[-2]), 2)
|
|
|
|
|
|
|
|
def test_transport(self):
|
|
|
|
p = xmlrpclib.ServerProxy(URL)
|
|
|
|
#do some requests with close.
|
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
|
|
|
p("transport").close() #same as above, really.
|
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
|
|
|
self.assertEqual(len(self.RequestHandler.myRequests), 2)
|
|
|
|
|
2009-06-28 18:04:17 -03:00
|
|
|
#A test case that verifies that gzip encoding works in both directions
|
|
|
|
#(for a request and the response)
|
|
|
|
class GzipServerTestCase(BaseServerTestCase):
|
|
|
|
#a request handler that supports keep-alive and logs requests into a
|
|
|
|
#class variable
|
|
|
|
class RequestHandler(SimpleXMLRPCServer.SimpleXMLRPCRequestHandler):
|
|
|
|
parentClass = SimpleXMLRPCServer.SimpleXMLRPCRequestHandler
|
|
|
|
protocol_version = 'HTTP/1.1'
|
|
|
|
|
|
|
|
def do_POST(self):
|
|
|
|
#store content of last request in class
|
|
|
|
self.__class__.content_length = int(self.headers["content-length"])
|
|
|
|
return self.parentClass.do_POST(self)
|
|
|
|
requestHandler = RequestHandler
|
|
|
|
|
|
|
|
class Transport(xmlrpclib.Transport):
|
|
|
|
#custom transport, stores the response length for our perusal
|
|
|
|
fake_gzip = False
|
|
|
|
def parse_response(self, response):
|
|
|
|
self.response_length=int(response.getheader("content-length", 0))
|
|
|
|
return xmlrpclib.Transport.parse_response(self, response)
|
|
|
|
|
|
|
|
def send_content(self, connection, body):
|
|
|
|
if self.fake_gzip:
|
|
|
|
#add a lone gzip header to induce decode error remotely
|
|
|
|
connection.putheader("Content-Encoding", "gzip")
|
|
|
|
return xmlrpclib.Transport.send_content(self, connection, body)
|
|
|
|
|
2010-04-27 20:14:58 -03:00
|
|
|
def setUp(self):
|
|
|
|
BaseServerTestCase.setUp(self)
|
|
|
|
|
2009-06-28 18:04:17 -03:00
|
|
|
def test_gzip_request(self):
|
|
|
|
t = self.Transport()
|
|
|
|
t.encode_threshold = None
|
|
|
|
p = xmlrpclib.ServerProxy(URL, transport=t)
|
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
|
|
|
a = self.RequestHandler.content_length
|
|
|
|
t.encode_threshold = 0 #turn on request encoding
|
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
|
|
|
b = self.RequestHandler.content_length
|
|
|
|
self.assertTrue(a>b)
|
|
|
|
|
|
|
|
def test_bad_gzip_request(self):
|
|
|
|
t = self.Transport()
|
|
|
|
t.encode_threshold = None
|
|
|
|
t.fake_gzip = True
|
|
|
|
p = xmlrpclib.ServerProxy(URL, transport=t)
|
|
|
|
cm = self.assertRaisesRegexp(xmlrpclib.ProtocolError,
|
|
|
|
re.compile(r"\b400\b"))
|
|
|
|
with cm:
|
|
|
|
p.pow(6, 8)
|
|
|
|
|
|
|
|
def test_gsip_response(self):
|
|
|
|
t = self.Transport()
|
|
|
|
p = xmlrpclib.ServerProxy(URL, transport=t)
|
|
|
|
old = self.requestHandler.encode_threshold
|
|
|
|
self.requestHandler.encode_threshold = None #no encoding
|
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
|
|
|
a = t.response_length
|
|
|
|
self.requestHandler.encode_threshold = 0 #always encode
|
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
|
|
|
b = t.response_length
|
|
|
|
self.requestHandler.encode_threshold = old
|
|
|
|
self.assertTrue(a>b)
|
|
|
|
|
|
|
|
#Test special attributes of the ServerProxy object
|
|
|
|
class ServerProxyTestCase(unittest.TestCase):
|
2010-04-27 20:14:58 -03:00
|
|
|
def setUp(self):
|
|
|
|
unittest.TestCase.setUp(self)
|
|
|
|
if threading:
|
|
|
|
self.url = URL
|
|
|
|
else:
|
|
|
|
# Without threading, http_server() and http_multi_server() will not
|
|
|
|
# be executed and URL is still equal to None. 'http://' is a just
|
|
|
|
# enough to choose the scheme (HTTP)
|
|
|
|
self.url = 'http://'
|
|
|
|
|
2009-06-28 18:04:17 -03:00
|
|
|
def test_close(self):
|
2010-04-27 20:14:58 -03:00
|
|
|
p = xmlrpclib.ServerProxy(self.url)
|
2009-06-28 18:04:17 -03:00
|
|
|
self.assertEqual(p('close')(), None)
|
|
|
|
|
|
|
|
def test_transport(self):
|
|
|
|
t = xmlrpclib.Transport()
|
2010-04-27 20:14:58 -03:00
|
|
|
p = xmlrpclib.ServerProxy(self.url, transport=t)
|
2009-06-28 18:04:17 -03:00
|
|
|
self.assertEqual(p('transport'), t)
|
|
|
|
|
2007-08-17 16:16:44 -03:00
|
|
|
# This is a contrived way to make a failure occur on the server side
|
|
|
|
# in order to test the _send_traceback_header flag on the server
|
|
|
|
class FailingMessageClass(mimetools.Message):
|
|
|
|
def __getitem__(self, key):
|
|
|
|
key = key.lower()
|
|
|
|
if key == 'content-length':
|
|
|
|
return 'I am broken'
|
|
|
|
return mimetools.Message.__getitem__(self, key)
|
|
|
|
|
|
|
|
|
2010-04-27 20:14:58 -03:00
|
|
|
@unittest.skipUnless(threading, 'Threading required for this test.')
|
2007-08-17 16:16:44 -03:00
|
|
|
class FailingServerTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.evt = threading.Event()
|
|
|
|
# start server thread to handle requests
|
2008-03-23 01:08:30 -03:00
|
|
|
serv_args = (self.evt, 1)
|
2007-08-17 16:16:44 -03:00
|
|
|
threading.Thread(target=http_server, args=serv_args).start()
|
|
|
|
|
2008-01-26 03:26:12 -04:00
|
|
|
# wait for the server to be ready
|
|
|
|
self.evt.wait()
|
|
|
|
self.evt.clear()
|
2007-08-17 16:16:44 -03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
# wait on the server thread to terminate
|
|
|
|
self.evt.wait()
|
|
|
|
# reset flag
|
|
|
|
SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = False
|
|
|
|
# reset message class
|
|
|
|
SimpleXMLRPCServer.SimpleXMLRPCRequestHandler.MessageClass = mimetools.Message
|
|
|
|
|
|
|
|
def test_basic(self):
|
|
|
|
# check that flag is false by default
|
|
|
|
flagval = SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header
|
|
|
|
self.assertEqual(flagval, False)
|
|
|
|
|
2007-08-20 21:16:21 -03:00
|
|
|
# enable traceback reporting
|
|
|
|
SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = True
|
|
|
|
|
|
|
|
# test a call that shouldn't fail just as a smoke test
|
|
|
|
try:
|
2009-01-14 06:50:57 -04:00
|
|
|
p = xmlrpclib.ServerProxy(URL)
|
2007-08-20 21:16:21 -03:00
|
|
|
self.assertEqual(p.pow(6,8), 6**8)
|
2008-01-27 13:11:11 -04:00
|
|
|
except (xmlrpclib.ProtocolError, socket.error), e:
|
2007-08-29 07:28:28 -03:00
|
|
|
# ignore failures due to non-blocking socket 'unavailable' errors
|
|
|
|
if not is_unavailable_exception(e):
|
|
|
|
# protocol error; provide additional information in test output
|
2008-01-27 16:03:13 -04:00
|
|
|
self.fail("%s\n%s" % (e, getattr(e, "headers", "")))
|
2007-08-17 16:16:44 -03:00
|
|
|
|
|
|
|
def test_fail_no_info(self):
|
|
|
|
# use the broken message class
|
|
|
|
SimpleXMLRPCServer.SimpleXMLRPCRequestHandler.MessageClass = FailingMessageClass
|
|
|
|
|
|
|
|
try:
|
2009-01-14 06:50:57 -04:00
|
|
|
p = xmlrpclib.ServerProxy(URL)
|
2007-08-17 16:16:44 -03:00
|
|
|
p.pow(6,8)
|
2008-01-27 13:11:11 -04:00
|
|
|
except (xmlrpclib.ProtocolError, socket.error), e:
|
2007-08-29 07:28:28 -03:00
|
|
|
# ignore failures due to non-blocking socket 'unavailable' errors
|
2008-01-27 16:03:13 -04:00
|
|
|
if not is_unavailable_exception(e) and hasattr(e, "headers"):
|
2007-08-29 07:28:28 -03:00
|
|
|
# The two server-side error headers shouldn't be sent back in this case
|
|
|
|
self.assertTrue(e.headers.get("X-exception") is None)
|
|
|
|
self.assertTrue(e.headers.get("X-traceback") is None)
|
2007-08-17 16:16:44 -03:00
|
|
|
else:
|
|
|
|
self.fail('ProtocolError not raised')
|
|
|
|
|
|
|
|
def test_fail_with_info(self):
|
|
|
|
# use the broken message class
|
|
|
|
SimpleXMLRPCServer.SimpleXMLRPCRequestHandler.MessageClass = FailingMessageClass
|
|
|
|
|
|
|
|
# Check that errors in the server send back exception/traceback
|
|
|
|
# info when flag is set
|
|
|
|
SimpleXMLRPCServer.SimpleXMLRPCServer._send_traceback_header = True
|
|
|
|
|
|
|
|
try:
|
2009-01-14 06:50:57 -04:00
|
|
|
p = xmlrpclib.ServerProxy(URL)
|
2007-08-17 16:16:44 -03:00
|
|
|
p.pow(6,8)
|
2008-01-27 13:11:11 -04:00
|
|
|
except (xmlrpclib.ProtocolError, socket.error), e:
|
2007-08-29 07:28:28 -03:00
|
|
|
# ignore failures due to non-blocking socket 'unavailable' errors
|
2008-01-27 16:03:13 -04:00
|
|
|
if not is_unavailable_exception(e) and hasattr(e, "headers"):
|
2007-08-29 07:28:28 -03:00
|
|
|
# We should get error info in the response
|
|
|
|
expected_err = "invalid literal for int() with base 10: 'I am broken'"
|
|
|
|
self.assertEqual(e.headers.get("x-exception"), expected_err)
|
|
|
|
self.assertTrue(e.headers.get("x-traceback") is not None)
|
2007-08-17 16:16:44 -03:00
|
|
|
else:
|
|
|
|
self.fail('ProtocolError not raised')
|
|
|
|
|
2007-12-07 05:07:10 -04:00
|
|
|
class CGIHandlerTestCase(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
self.cgi = SimpleXMLRPCServer.CGIXMLRPCRequestHandler()
|
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.cgi = None
|
|
|
|
|
|
|
|
def test_cgi_get(self):
|
2009-04-26 17:51:44 -03:00
|
|
|
with test_support.EnvironmentVarGuard() as env:
|
2009-05-01 14:35:37 -03:00
|
|
|
env['REQUEST_METHOD'] = 'GET'
|
2009-04-26 17:51:44 -03:00
|
|
|
# if the method is GET and no request_text is given, it runs handle_get
|
|
|
|
# get sysout output
|
2009-10-17 12:09:41 -03:00
|
|
|
with test_support.captured_stdout() as data_out:
|
|
|
|
self.cgi.handle_request()
|
2007-12-07 05:07:10 -04:00
|
|
|
|
2009-04-26 17:51:44 -03:00
|
|
|
# parse Status header
|
2009-10-17 12:09:41 -03:00
|
|
|
data_out.seek(0)
|
|
|
|
handle = data_out.read()
|
2009-04-26 17:51:44 -03:00
|
|
|
status = handle.split()[1]
|
|
|
|
message = ' '.join(handle.split()[2:4])
|
2007-12-07 05:07:10 -04:00
|
|
|
|
2009-04-26 17:51:44 -03:00
|
|
|
self.assertEqual(status, '400')
|
|
|
|
self.assertEqual(message, 'Bad Request')
|
2007-12-07 05:07:10 -04:00
|
|
|
|
|
|
|
|
|
|
|
def test_cgi_xmlrpc_response(self):
|
|
|
|
data = """<?xml version='1.0'?>
|
2009-10-17 12:09:41 -03:00
|
|
|
<methodCall>
|
|
|
|
<methodName>test_method</methodName>
|
|
|
|
<params>
|
|
|
|
<param>
|
|
|
|
<value><string>foo</string></value>
|
|
|
|
</param>
|
|
|
|
<param>
|
|
|
|
<value><string>bar</string></value>
|
|
|
|
</param>
|
|
|
|
</params>
|
|
|
|
</methodCall>
|
|
|
|
"""
|
|
|
|
|
|
|
|
with test_support.EnvironmentVarGuard() as env, \
|
|
|
|
test_support.captured_stdout() as data_out, \
|
|
|
|
test_support.captured_stdin() as data_in:
|
|
|
|
data_in.write(data)
|
|
|
|
data_in.seek(0)
|
2009-05-01 14:35:37 -03:00
|
|
|
env['CONTENT_LENGTH'] = str(len(data))
|
2009-04-01 12:23:43 -03:00
|
|
|
self.cgi.handle_request()
|
2009-10-17 12:09:41 -03:00
|
|
|
data_out.seek(0)
|
2007-12-07 05:07:10 -04:00
|
|
|
|
|
|
|
# will respond exception, if so, our goal is achieved ;)
|
2009-10-17 12:09:41 -03:00
|
|
|
handle = data_out.read()
|
2007-12-07 05:07:10 -04:00
|
|
|
|
|
|
|
# start with 44th char so as not to get http header, we just need only xml
|
|
|
|
self.assertRaises(xmlrpclib.Fault, xmlrpclib.loads, handle[44:])
|
|
|
|
|
2009-04-01 17:26:33 -03:00
|
|
|
# Also test the content-length returned by handle_request
|
|
|
|
# Using the same test method inorder to avoid all the datapassing
|
|
|
|
# boilerplate code.
|
|
|
|
# Test for bug: http://bugs.python.org/issue5040
|
|
|
|
|
|
|
|
content = handle[handle.find("<?xml"):]
|
|
|
|
|
2010-11-21 09:34:58 -04:00
|
|
|
self.assertEqual(
|
2009-04-01 17:26:33 -03:00
|
|
|
int(re.search('Content-Length: (\d+)', handle).group(1)),
|
|
|
|
len(content))
|
|
|
|
|
|
|
|
|
2008-11-24 18:00:29 -04:00
|
|
|
class FakeSocket:
|
|
|
|
|
|
|
|
def __init__(self):
|
|
|
|
self.data = StringIO.StringIO()
|
|
|
|
|
|
|
|
def send(self, buf):
|
|
|
|
self.data.write(buf)
|
|
|
|
return len(buf)
|
|
|
|
|
|
|
|
def sendall(self, buf):
|
|
|
|
self.data.write(buf)
|
|
|
|
|
|
|
|
def getvalue(self):
|
|
|
|
return self.data.getvalue()
|
|
|
|
|
2009-01-11 12:23:37 -04:00
|
|
|
def makefile(self, x='r', y=-1):
|
2008-11-24 18:00:29 -04:00
|
|
|
raise RuntimeError
|
|
|
|
|
2009-06-28 18:04:17 -03:00
|
|
|
def close(self):
|
|
|
|
pass
|
|
|
|
|
2008-11-24 18:00:29 -04:00
|
|
|
class FakeTransport(xmlrpclib.Transport):
|
|
|
|
"""A Transport instance that records instead of sending a request.
|
|
|
|
|
|
|
|
This class replaces the actual socket used by httplib with a
|
|
|
|
FakeSocket object that records the request. It doesn't provide a
|
|
|
|
response.
|
|
|
|
"""
|
|
|
|
|
|
|
|
def make_connection(self, host):
|
|
|
|
conn = xmlrpclib.Transport.make_connection(self, host)
|
2009-06-28 18:04:17 -03:00
|
|
|
conn.sock = self.fake_socket = FakeSocket()
|
2008-11-24 18:00:29 -04:00
|
|
|
return conn
|
|
|
|
|
|
|
|
class TransportSubclassTestCase(unittest.TestCase):
|
|
|
|
|
|
|
|
def issue_request(self, transport_class):
|
|
|
|
"""Return an HTTP request made via transport_class."""
|
|
|
|
transport = transport_class()
|
|
|
|
proxy = xmlrpclib.ServerProxy("http://example.com/",
|
|
|
|
transport=transport)
|
|
|
|
try:
|
|
|
|
proxy.pow(6, 8)
|
|
|
|
except RuntimeError:
|
|
|
|
return transport.fake_socket.getvalue()
|
|
|
|
return None
|
|
|
|
|
|
|
|
def test_custom_user_agent(self):
|
|
|
|
class TestTransport(FakeTransport):
|
|
|
|
|
|
|
|
def send_user_agent(self, conn):
|
|
|
|
xmlrpclib.Transport.send_user_agent(self, conn)
|
|
|
|
conn.putheader("X-Test", "test_custom_user_agent")
|
|
|
|
|
|
|
|
req = self.issue_request(TestTransport)
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn("X-Test: test_custom_user_agent\r\n", req)
|
2008-11-24 18:00:29 -04:00
|
|
|
|
|
|
|
def test_send_host(self):
|
|
|
|
class TestTransport(FakeTransport):
|
|
|
|
|
|
|
|
def send_host(self, conn, host):
|
|
|
|
xmlrpclib.Transport.send_host(self, conn, host)
|
|
|
|
conn.putheader("X-Test", "test_send_host")
|
|
|
|
|
|
|
|
req = self.issue_request(TestTransport)
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn("X-Test: test_send_host\r\n", req)
|
2008-11-24 18:00:29 -04:00
|
|
|
|
|
|
|
def test_send_request(self):
|
|
|
|
class TestTransport(FakeTransport):
|
|
|
|
|
|
|
|
def send_request(self, conn, url, body):
|
|
|
|
xmlrpclib.Transport.send_request(self, conn, url, body)
|
|
|
|
conn.putheader("X-Test", "test_send_request")
|
|
|
|
|
|
|
|
req = self.issue_request(TestTransport)
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn("X-Test: test_send_request\r\n", req)
|
2008-11-24 18:00:29 -04:00
|
|
|
|
|
|
|
def test_send_content(self):
|
|
|
|
class TestTransport(FakeTransport):
|
|
|
|
|
|
|
|
def send_content(self, conn, body):
|
|
|
|
conn.putheader("X-Test", "test_send_content")
|
|
|
|
xmlrpclib.Transport.send_content(self, conn, body)
|
|
|
|
|
|
|
|
req = self.issue_request(TestTransport)
|
2010-01-23 19:04:36 -04:00
|
|
|
self.assertIn("X-Test: test_send_content\r\n", req)
|
2008-11-24 18:00:29 -04:00
|
|
|
|
2009-10-30 14:56:00 -03:00
|
|
|
@test_support.reap_threads
|
2001-10-01 14:47:44 -03:00
|
|
|
def test_main():
|
2007-08-14 10:35:00 -03:00
|
|
|
xmlrpc_tests = [XMLRPCTestCase, HelperTestCase, DateTimeTestCase,
|
2008-11-24 18:00:29 -04:00
|
|
|
BinaryTestCase, FaultTestCase, TransportSubclassTestCase]
|
2009-01-14 06:50:57 -04:00
|
|
|
xmlrpc_tests.append(SimpleServerTestCase)
|
2009-07-12 19:42:08 -03:00
|
|
|
xmlrpc_tests.append(KeepaliveServerTestCase1)
|
|
|
|
xmlrpc_tests.append(KeepaliveServerTestCase2)
|
2009-07-19 19:14:00 -03:00
|
|
|
try:
|
|
|
|
import gzip
|
|
|
|
xmlrpc_tests.append(GzipServerTestCase)
|
|
|
|
except ImportError:
|
|
|
|
pass #gzip not supported in this build
|
2009-08-27 20:13:18 -03:00
|
|
|
xmlrpc_tests.append(MultiPathServerTestCase)
|
2009-06-28 18:04:17 -03:00
|
|
|
xmlrpc_tests.append(ServerProxyTestCase)
|
2009-01-14 06:50:57 -04:00
|
|
|
xmlrpc_tests.append(FailingServerTestCase)
|
|
|
|
xmlrpc_tests.append(CGIHandlerTestCase)
|
2001-10-01 14:47:44 -03:00
|
|
|
|
2007-08-14 10:35:00 -03:00
|
|
|
test_support.run_unittest(*xmlrpc_tests)
|
2001-10-01 14:47:44 -03:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
|
|
|
test_main()
|