7 import SimpleXMLRPCServer
14 from test
import test_support
28 alist
= [{'astring': 'foo@bar.baz.spam',
32 'anotherlist': ['.zyx.41'],
33 'abase64': xmlrpclib
.Binary("my dog has fleas"),
34 'boolean': xmlrpclib
.False,
35 'unicode': u
'\u4000\u6000\u8000',
36 u
'ukey\u4000': 'regular value',
37 'datetime1': xmlrpclib
.DateTime('20050210T11:41:23'),
38 'datetime2': xmlrpclib
.DateTime(
39 (2005, 02, 10, 11, 41, 23, 0, 1, -1)),
40 'datetime3': xmlrpclib
.DateTime(
41 datetime
.datetime(2005, 02, 10, 11, 41, 23)),
44 class XMLRPCTestCase(unittest
.TestCase
):
46 def test_dump_load(self
):
47 self
.assertEquals(alist
,
48 xmlrpclib
.loads(xmlrpclib
.dumps((alist
,)))[0][0])
50 def test_dump_bare_datetime(self
):
51 # This checks that an unwrapped datetime.date object can be handled
52 # by the marshalling code. This can't be done via test_dump_load()
53 # since with use_datetime set to 1 the unmarshaller would create
54 # datetime objects for the 'datetime[123]' keys as well
55 dt
= datetime
.datetime(2005, 02, 10, 11, 41, 23)
56 s
= xmlrpclib
.dumps((dt
,))
57 (newdt
,), m
= xmlrpclib
.loads(s
, use_datetime
=1)
58 self
.assertEquals(newdt
, dt
)
59 self
.assertEquals(m
, None)
61 (newdt
,), m
= xmlrpclib
.loads(s
, use_datetime
=0)
62 self
.assertEquals(newdt
, xmlrpclib
.DateTime('20050210T11:41:23'))
64 def test_datetime_before_1900(self
):
65 # same as before but with a date before 1900
66 dt
= datetime
.datetime(1, 02, 10, 11, 41, 23)
67 s
= xmlrpclib
.dumps((dt
,))
68 (newdt
,), m
= xmlrpclib
.loads(s
, use_datetime
=1)
69 self
.assertEquals(newdt
, dt
)
70 self
.assertEquals(m
, None)
72 (newdt
,), m
= xmlrpclib
.loads(s
, use_datetime
=0)
73 self
.assertEquals(newdt
, xmlrpclib
.DateTime('00010210T11:41:23'))
75 def test_cmp_datetime_DateTime(self
):
76 now
= datetime
.datetime
.now()
77 dt
= xmlrpclib
.DateTime(now
.timetuple())
78 self
.assertTrue(dt
== now
)
79 self
.assertTrue(now
== dt
)
80 then
= now
+ datetime
.timedelta(seconds
=4)
81 self
.assertTrue(then
>= dt
)
82 self
.assertTrue(dt
< then
)
84 def test_bug_1164912 (self
):
85 d
= xmlrpclib
.DateTime()
86 ((new_d
,), dummy
) = xmlrpclib
.loads(xmlrpclib
.dumps((d
,),
88 self
.assertIsInstance(new_d
.value
, str)
90 # Check that the output of dumps() is still an 8-bit string
91 s
= xmlrpclib
.dumps((new_d
,), methodresponse
=True)
92 self
.assertIsInstance(s
, str)
94 def test_newstyle_class(self
):
100 ((t2
,), dummy
) = xmlrpclib
.loads(xmlrpclib
.dumps((t
,)))
101 self
.assertEquals(t2
, t
.__dict
__)
103 def test_dump_big_long(self
):
104 self
.assertRaises(OverflowError, xmlrpclib
.dumps
, (2L**99,))
106 def test_dump_bad_dict(self
):
107 self
.assertRaises(TypeError, xmlrpclib
.dumps
, ({(1,2,3): 1},))
109 def test_dump_recursive_seq(self
):
113 self
.assertRaises(TypeError, xmlrpclib
.dumps
, (l
,))
115 def test_dump_recursive_dict(self
):
119 self
.assertRaises(TypeError, xmlrpclib
.dumps
, (d
,))
121 def test_dump_big_int(self
):
122 if sys
.maxint
> 2L**31-1:
123 self
.assertRaises(OverflowError, xmlrpclib
.dumps
,
126 xmlrpclib
.dumps((xmlrpclib
.MAXINT
, xmlrpclib
.MININT
))
127 self
.assertRaises(OverflowError, xmlrpclib
.dumps
, (xmlrpclib
.MAXINT
+1,))
128 self
.assertRaises(OverflowError, xmlrpclib
.dumps
, (xmlrpclib
.MININT
-1,))
133 m
= xmlrpclib
.Marshaller()
134 m
.dump_int(xmlrpclib
.MAXINT
, dummy_write
)
135 m
.dump_int(xmlrpclib
.MININT
, dummy_write
)
136 self
.assertRaises(OverflowError, m
.dump_int
, xmlrpclib
.MAXINT
+1, dummy_write
)
137 self
.assertRaises(OverflowError, m
.dump_int
, xmlrpclib
.MININT
-1, dummy_write
)
140 def test_dump_none(self
):
141 value
= alist
+ [None]
142 arg1
= (alist
+ [None],)
143 strg
= xmlrpclib
.dumps(arg1
, allow_none
=True)
144 self
.assertEquals(value
,
145 xmlrpclib
.loads(strg
)[0][0])
146 self
.assertRaises(TypeError, xmlrpclib
.dumps
, (arg1
,))
148 def test_default_encoding_issues(self
):
149 # SF bug #1115989: wrong decoding in '_stringify'
150 utf8
= """<?xml version='1.0' encoding='iso-8859-1'?>
153 <string>abc \x95</string>
158 <name>def \x96</name>
159 <value><string>ghi \x97</string></value>
166 # sys.setdefaultencoding() normally doesn't exist after site.py is
167 # loaded. Import a temporary fresh copy to get access to it
168 # but then restore the original copy to avoid messing with
169 # other potentially modified sys module attributes
170 old_encoding
= sys
.getdefaultencoding()
171 with test_support
.CleanImport('sys'):
172 import sys
as temp_sys
173 temp_sys
.setdefaultencoding("iso-8859-1")
175 (s
, d
), m
= xmlrpclib
.loads(utf8
)
177 temp_sys
.setdefaultencoding(old_encoding
)
181 self
.assertEquals(s
, u
"abc \x95")
182 self
.assertIsInstance(s
, unicode)
183 self
.assertEquals(items
, [(u
"def \x96", u
"ghi \x97")])
184 self
.assertIsInstance(items
[0][0], unicode)
185 self
.assertIsInstance(items
[0][1], unicode)
187 self
.assertEquals(s
, "abc \xc2\x95")
188 self
.assertEquals(items
, [("def \xc2\x96", "ghi \xc2\x97")])
191 class HelperTestCase(unittest
.TestCase
):
192 def test_escape(self
):
193 self
.assertEqual(xmlrpclib
.escape("a&b"), "a&b")
194 self
.assertEqual(xmlrpclib
.escape("a<b"), "a<b")
195 self
.assertEqual(xmlrpclib
.escape("a>b"), "a>b")
197 class FaultTestCase(unittest
.TestCase
):
199 f
= xmlrpclib
.Fault(42, 'Test Fault')
200 self
.assertEqual(repr(f
), "<Fault 42: 'Test Fault'>")
201 self
.assertEqual(repr(f
), str(f
))
203 def test_dump_fault(self
):
204 f
= xmlrpclib
.Fault(42, 'Test Fault')
205 s
= xmlrpclib
.dumps((f
,))
206 (newf
,), m
= xmlrpclib
.loads(s
)
207 self
.assertEquals(newf
, {'faultCode': 42, 'faultString': 'Test Fault'})
208 self
.assertEquals(m
, None)
210 s
= xmlrpclib
.Marshaller().dumps(f
)
211 self
.assertRaises(xmlrpclib
.Fault
, xmlrpclib
.loads
, s
)
214 class DateTimeTestCase(unittest
.TestCase
):
215 def test_default(self
):
216 t
= xmlrpclib
.DateTime()
219 d
= 1181399930.036952
220 t
= xmlrpclib
.DateTime(d
)
221 self
.assertEqual(str(t
), time
.strftime("%Y%m%dT%H:%M:%S", time
.localtime(d
)))
223 def test_time_tuple(self
):
224 d
= (2007,6,9,10,38,50,5,160,0)
225 t
= xmlrpclib
.DateTime(d
)
226 self
.assertEqual(str(t
), '20070609T10:38:50')
228 def test_time_struct(self
):
229 d
= time
.localtime(1181399930.036952)
230 t
= xmlrpclib
.DateTime(d
)
231 self
.assertEqual(str(t
), time
.strftime("%Y%m%dT%H:%M:%S", d
))
233 def test_datetime_datetime(self
):
234 d
= datetime
.datetime(2007,1,2,3,4,5)
235 t
= xmlrpclib
.DateTime(d
)
236 self
.assertEqual(str(t
), '20070102T03:04:05')
239 d
= datetime
.datetime(2007,1,2,3,4,5)
240 t
= xmlrpclib
.DateTime(d
)
241 val
="<DateTime '20070102T03:04:05' at %x>" % id(t
)
242 self
.assertEqual(repr(t
), val
)
244 def test_decode(self
):
245 d
= ' 20070908T07:11:13 '
246 t1
= xmlrpclib
.DateTime()
248 tref
= xmlrpclib
.DateTime(datetime
.datetime(2007,9,8,7,11,13))
249 self
.assertEqual(t1
, tref
)
251 t2
= xmlrpclib
._datetime
(d
)
252 self
.assertEqual(t1
, tref
)
254 class BinaryTestCase(unittest
.TestCase
):
255 def test_default(self
):
256 t
= xmlrpclib
.Binary()
257 self
.assertEqual(str(t
), '')
259 def test_string(self
):
260 d
= '\x01\x02\x03abc123\xff\xfe'
261 t
= xmlrpclib
.Binary(d
)
262 self
.assertEqual(str(t
), d
)
264 def test_decode(self
):
265 d
= '\x01\x02\x03abc123\xff\xfe'
266 de
= base64
.encodestring(d
)
267 t1
= xmlrpclib
.Binary()
269 self
.assertEqual(str(t1
), d
)
271 t2
= xmlrpclib
._binary
(de
)
272 self
.assertEqual(str(t2
), d
)
275 ADDR
= PORT
= URL
= None
277 # The evt is set twice. First when the server is ready to serve.
278 # Second when the server has been shutdown. The user must clear
279 # the event after it has been set the first time to catch the second set.
280 def http_server(evt
, numrequests
, requestHandler
=None):
281 class TestInstanceClass
:
285 def _methodHelp(self
, name
):
287 return 'This is the div function'
290 '''This is my function'''
293 class MyXMLRPCServer(SimpleXMLRPCServer
.SimpleXMLRPCServer
):
294 def get_request(self
):
295 # Ensure the socket is always non-blocking. On Linux, socket
296 # attributes are not inherited like they are on *BSD and Windows.
297 s
, port
= self
.socket
.accept()
301 if not requestHandler
:
302 requestHandler
= SimpleXMLRPCServer
.SimpleXMLRPCRequestHandler
303 serv
= MyXMLRPCServer(("localhost", 0), requestHandler
,
304 logRequests
=False, bind_and_activate
=False)
306 serv
.socket
.settimeout(3)
308 global ADDR
, PORT
, URL
309 ADDR
, PORT
= serv
.socket
.getsockname()
310 #connect to IP address directly. This avoids socket.create_connection()
311 #trying to connect to to "localhost" using all address families, which
312 #causes slowdown e.g. on vista which supports AF_INET6. The server listens
314 URL
= "http://%s:%d"%(ADDR
, PORT
)
315 serv
.server_activate()
316 serv
.register_introspection_functions()
317 serv
.register_multicall_functions()
318 serv
.register_function(pow)
319 serv
.register_function(lambda x
,y
: x
+y
, 'add')
320 serv
.register_function(my_function
)
321 serv
.register_instance(TestInstanceClass())
324 # handle up to 'numrequests' requests
325 while numrequests
> 0:
326 serv
.handle_request()
329 except socket
.timeout
:
336 def http_multi_server(evt
, numrequests
, requestHandler
=None):
337 class TestInstanceClass
:
341 def _methodHelp(self
, name
):
343 return 'This is the div function'
346 '''This is my function'''
349 class MyXMLRPCServer(SimpleXMLRPCServer
.MultiPathXMLRPCServer
):
350 def get_request(self
):
351 # Ensure the socket is always non-blocking. On Linux, socket
352 # attributes are not inherited like they are on *BSD and Windows.
353 s
, port
= self
.socket
.accept()
357 if not requestHandler
:
358 requestHandler
= SimpleXMLRPCServer
.SimpleXMLRPCRequestHandler
359 class MyRequestHandler(requestHandler
):
362 serv
= MyXMLRPCServer(("localhost", 0), MyRequestHandler
,
363 logRequests
=False, bind_and_activate
=False)
364 serv
.socket
.settimeout(3)
367 global ADDR
, PORT
, URL
368 ADDR
, PORT
= serv
.socket
.getsockname()
369 #connect to IP address directly. This avoids socket.create_connection()
370 #trying to connect to to "localhost" using all address families, which
371 #causes slowdown e.g. on vista which supports AF_INET6. The server listens
373 URL
= "http://%s:%d"%(ADDR
, PORT
)
374 serv
.server_activate()
375 paths
= ["/foo", "/foo/bar"]
377 d
= serv
.add_dispatcher(path
, SimpleXMLRPCServer
.SimpleXMLRPCDispatcher())
378 d
.register_introspection_functions()
379 d
.register_multicall_functions()
380 serv
.get_dispatcher(paths
[0]).register_function(pow)
381 serv
.get_dispatcher(paths
[1]).register_function(lambda x
,y
: x
+y
, 'add')
384 # handle up to 'numrequests' requests
385 while numrequests
> 0:
386 serv
.handle_request()
389 except socket
.timeout
:
396 # This function prevents errors like:
397 # <ProtocolError for localhost:57527/RPC2: 500 Internal Server Error>
398 def is_unavailable_exception(e
):
399 '''Returns True if the given ProtocolError is the product of a server-side
400 exception caused by the 'temporarily unavailable' response sometimes
401 given by operations on non-blocking sockets.'''
403 # sometimes we get a -1 error code and/or empty headers
405 if e
.errcode
== -1 or e
.headers
is None:
407 exc_mess
= e
.headers
.get('X-exception')
408 except AttributeError:
409 # Ignore socket.errors here.
412 if exc_mess
and 'temporarily unavailable' in exc_mess
.lower():
417 @unittest.skipUnless(threading
, 'Threading required for this test.')
418 class BaseServerTestCase(unittest
.TestCase
):
419 requestHandler
= None
421 threadFunc
= staticmethod(http_server
)
424 # enable traceback reporting
425 SimpleXMLRPCServer
.SimpleXMLRPCServer
._send
_traceback
_header
= True
427 self
.evt
= threading
.Event()
428 # start server thread to handle requests
429 serv_args
= (self
.evt
, self
.request_count
, self
.requestHandler
)
430 threading
.Thread(target
=self
.threadFunc
, args
=serv_args
).start()
432 # wait for the server to be ready
437 # wait on the server thread to terminate
440 # disable traceback reporting
441 SimpleXMLRPCServer
.SimpleXMLRPCServer
._send
_traceback
_header
= False
443 # NOTE: The tests in SimpleServerTestCase will ignore failures caused by
444 # "temporarily unavailable" exceptions raised in SimpleXMLRPCServer. This
445 # condition occurs infrequently on some platforms, frequently on others, and
446 # is apparently caused by using SimpleXMLRPCServer with a non-blocking socket
447 # If the server class is updated at some point in the future to handle this
448 # situation more gracefully, these tests should be modified appropriately.
450 class SimpleServerTestCase(BaseServerTestCase
):
451 def test_simple1(self
):
453 p
= xmlrpclib
.ServerProxy(URL
)
454 self
.assertEqual(p
.pow(6,8), 6**8)
455 except (xmlrpclib
.ProtocolError
, socket
.error
), e
:
456 # ignore failures due to non-blocking socket 'unavailable' errors
457 if not is_unavailable_exception(e
):
458 # protocol error; provide additional information in test output
459 self
.fail("%s\n%s" % (e
, getattr(e
, "headers", "")))
461 def test_nonascii(self
):
462 start_string
= 'P\N{LATIN SMALL LETTER Y WITH CIRCUMFLEX}t'
463 end_string
= 'h\N{LATIN SMALL LETTER O WITH HORN}n'
466 p
= xmlrpclib
.ServerProxy(URL
)
467 self
.assertEqual(p
.add(start_string
, end_string
),
468 start_string
+ end_string
)
469 except (xmlrpclib
.ProtocolError
, socket
.error
) as e
:
470 # ignore failures due to non-blocking socket unavailable errors.
471 if not is_unavailable_exception(e
):
472 # protocol error; provide additional information in test output
473 self
.fail("%s\n%s" % (e
, getattr(e
, "headers", "")))
476 # [ch] The test 404 is causing lots of false alarms.
477 def XXXtest_404(self
):
478 # send POST with httplib, it should return 404 header and
479 # 'Not Found' message.
480 conn
= httplib
.HTTPConnection(ADDR
, PORT
)
481 conn
.request('POST', '/this-is-not-valid')
482 response
= conn
.getresponse()
485 self
.assertEqual(response
.status
, 404)
486 self
.assertEqual(response
.reason
, 'Not Found')
488 def test_introspection1(self
):
490 p
= xmlrpclib
.ServerProxy(URL
)
491 meth
= p
.system
.listMethods()
492 expected_methods
= set(['pow', 'div', 'my_function', 'add',
493 'system.listMethods', 'system.methodHelp',
494 'system.methodSignature', 'system.multicall'])
495 self
.assertEqual(set(meth
), expected_methods
)
496 except (xmlrpclib
.ProtocolError
, socket
.error
), e
:
497 # ignore failures due to non-blocking socket 'unavailable' errors
498 if not is_unavailable_exception(e
):
499 # protocol error; provide additional information in test output
500 self
.fail("%s\n%s" % (e
, getattr(e
, "headers", "")))
502 def test_introspection2(self
):
505 p
= xmlrpclib
.ServerProxy(URL
)
506 divhelp
= p
.system
.methodHelp('div')
507 self
.assertEqual(divhelp
, 'This is the div function')
508 except (xmlrpclib
.ProtocolError
, socket
.error
), e
:
509 # ignore failures due to non-blocking socket 'unavailable' errors
510 if not is_unavailable_exception(e
):
511 # protocol error; provide additional information in test output
512 self
.fail("%s\n%s" % (e
, getattr(e
, "headers", "")))
514 @unittest.skipIf(sys
.flags
.optimize
>= 2,
515 "Docstrings are omitted with -O2 and above")
516 def test_introspection3(self
):
519 p
= xmlrpclib
.ServerProxy(URL
)
520 myfunction
= p
.system
.methodHelp('my_function')
521 self
.assertEqual(myfunction
, 'This is my function')
522 except (xmlrpclib
.ProtocolError
, socket
.error
), e
:
523 # ignore failures due to non-blocking socket 'unavailable' errors
524 if not is_unavailable_exception(e
):
525 # protocol error; provide additional information in test output
526 self
.fail("%s\n%s" % (e
, getattr(e
, "headers", "")))
528 def test_introspection4(self
):
529 # the SimpleXMLRPCServer doesn't support signatures, but
530 # at least check that we can try making the call
532 p
= xmlrpclib
.ServerProxy(URL
)
533 divsig
= p
.system
.methodSignature('div')
534 self
.assertEqual(divsig
, 'signatures not supported')
535 except (xmlrpclib
.ProtocolError
, socket
.error
), e
:
536 # ignore failures due to non-blocking socket 'unavailable' errors
537 if not is_unavailable_exception(e
):
538 # protocol error; provide additional information in test output
539 self
.fail("%s\n%s" % (e
, getattr(e
, "headers", "")))
541 def test_multicall(self
):
543 p
= xmlrpclib
.ServerProxy(URL
)
544 multicall
= xmlrpclib
.MultiCall(p
)
547 multicall
.div(127,42)
548 add_result
, pow_result
, div_result
= multicall()
549 self
.assertEqual(add_result
, 2+3)
550 self
.assertEqual(pow_result
, 6**8)
551 self
.assertEqual(div_result
, 127//42)
552 except (xmlrpclib
.ProtocolError
, socket
.error
), e
:
553 # ignore failures due to non-blocking socket 'unavailable' errors
554 if not is_unavailable_exception(e
):
555 # protocol error; provide additional information in test output
556 self
.fail("%s\n%s" % (e
, getattr(e
, "headers", "")))
558 def test_non_existing_multicall(self
):
560 p
= xmlrpclib
.ServerProxy(URL
)
561 multicall
= xmlrpclib
.MultiCall(p
)
562 multicall
.this_is_not_exists()
565 # result.results contains;
566 # [{'faultCode': 1, 'faultString': '<type \'exceptions.Exception\'>:'
567 # 'method "this_is_not_exists" is not supported'>}]
569 self
.assertEqual(result
.results
[0]['faultCode'], 1)
570 self
.assertEqual(result
.results
[0]['faultString'],
571 '<type \'exceptions.Exception\'>:method "this_is_not_exists" '
573 except (xmlrpclib
.ProtocolError
, socket
.error
), e
:
574 # ignore failures due to non-blocking socket 'unavailable' errors
575 if not is_unavailable_exception(e
):
576 # protocol error; provide additional information in test output
577 self
.fail("%s\n%s" % (e
, getattr(e
, "headers", "")))
579 def test_dotted_attribute(self
):
580 # Raises an AttributeError because private methods are not allowed.
581 self
.assertRaises(AttributeError,
582 SimpleXMLRPCServer
.resolve_dotted_attribute
, str, '__add')
584 self
.assertTrue(SimpleXMLRPCServer
.resolve_dotted_attribute(str, 'title'))
585 # Get the test to run faster by sending a request with test_simple1.
586 # This avoids waiting for the socket timeout.
589 class MultiPathServerTestCase(BaseServerTestCase
):
590 threadFunc
= staticmethod(http_multi_server
)
592 def test_path1(self
):
593 p
= xmlrpclib
.ServerProxy(URL
+"/foo")
594 self
.assertEqual(p
.pow(6,8), 6**8)
595 self
.assertRaises(xmlrpclib
.Fault
, p
.add
, 6, 8)
596 def test_path2(self
):
597 p
= xmlrpclib
.ServerProxy(URL
+"/foo/bar")
598 self
.assertEqual(p
.add(6,8), 6+8)
599 self
.assertRaises(xmlrpclib
.Fault
, p
.pow, 6, 8)
601 #A test case that verifies that a server using the HTTP/1.1 keep-alive mechanism
602 #does indeed serve subsequent requests on the same connection
603 class BaseKeepaliveServerTestCase(BaseServerTestCase
):
604 #a request handler that supports keep-alive and logs requests into a
606 class RequestHandler(SimpleXMLRPCServer
.SimpleXMLRPCRequestHandler
):
607 parentClass
= SimpleXMLRPCServer
.SimpleXMLRPCRequestHandler
608 protocol_version
= 'HTTP/1.1'
611 self
.myRequests
.append([])
612 self
.reqidx
= len(self
.myRequests
)-1
613 return self
.parentClass
.handle(self
)
614 def handle_one_request(self
):
615 result
= self
.parentClass
.handle_one_request(self
)
616 self
.myRequests
[self
.reqidx
].append(self
.raw_requestline
)
619 requestHandler
= RequestHandler
622 self
.RequestHandler
.myRequests
= []
623 return BaseServerTestCase
.setUp(self
)
625 #A test case that verifies that a server using the HTTP/1.1 keep-alive mechanism
626 #does indeed serve subsequent requests on the same connection
627 class KeepaliveServerTestCase1(BaseKeepaliveServerTestCase
):
629 p
= xmlrpclib
.ServerProxy(URL
)
631 self
.assertEqual(p
.pow(6,8), 6**8)
632 self
.assertEqual(p
.pow(6,8), 6**8)
633 self
.assertEqual(p
.pow(6,8), 6**8)
635 #they should have all been handled by a single request handler
636 self
.assertEqual(len(self
.RequestHandler
.myRequests
), 1)
638 #check that we did at least two (the third may be pending append
639 #due to thread scheduling)
640 self
.assertGreaterEqual(len(self
.RequestHandler
.myRequests
[-1]), 2)
642 #test special attribute access on the serverproxy, through the __call__
644 class KeepaliveServerTestCase2(BaseKeepaliveServerTestCase
):
645 #ask for two keepalive requests to be handled.
648 def test_close(self
):
649 p
= xmlrpclib
.ServerProxy(URL
)
650 #do some requests with close.
651 self
.assertEqual(p
.pow(6,8), 6**8)
652 self
.assertEqual(p
.pow(6,8), 6**8)
653 self
.assertEqual(p
.pow(6,8), 6**8)
654 p("close")() #this should trigger a new keep-alive request
655 self
.assertEqual(p
.pow(6,8), 6**8)
656 self
.assertEqual(p
.pow(6,8), 6**8)
657 self
.assertEqual(p
.pow(6,8), 6**8)
659 #they should have all been two request handlers, each having logged at least
660 #two complete requests
661 self
.assertEqual(len(self
.RequestHandler
.myRequests
), 2)
662 self
.assertGreaterEqual(len(self
.RequestHandler
.myRequests
[-1]), 2)
663 self
.assertGreaterEqual(len(self
.RequestHandler
.myRequests
[-2]), 2)
665 def test_transport(self
):
666 p
= xmlrpclib
.ServerProxy(URL
)
667 #do some requests with close.
668 self
.assertEqual(p
.pow(6,8), 6**8)
669 p("transport").close() #same as above, really.
670 self
.assertEqual(p
.pow(6,8), 6**8)
671 self
.assertEqual(len(self
.RequestHandler
.myRequests
), 2)
673 #A test case that verifies that gzip encoding works in both directions
674 #(for a request and the response)
675 class GzipServerTestCase(BaseServerTestCase
):
676 #a request handler that supports keep-alive and logs requests into a
678 class RequestHandler(SimpleXMLRPCServer
.SimpleXMLRPCRequestHandler
):
679 parentClass
= SimpleXMLRPCServer
.SimpleXMLRPCRequestHandler
680 protocol_version
= 'HTTP/1.1'
683 #store content of last request in class
684 self
.__class
__.content_length
= int(self
.headers
["content-length"])
685 return self
.parentClass
.do_POST(self
)
686 requestHandler
= RequestHandler
688 class Transport(xmlrpclib
.Transport
):
689 #custom transport, stores the response length for our perusal
691 def parse_response(self
, response
):
692 self
.response_length
=int(response
.getheader("content-length", 0))
693 return xmlrpclib
.Transport
.parse_response(self
, response
)
695 def send_content(self
, connection
, body
):
697 #add a lone gzip header to induce decode error remotely
698 connection
.putheader("Content-Encoding", "gzip")
699 return xmlrpclib
.Transport
.send_content(self
, connection
, body
)
702 BaseServerTestCase
.setUp(self
)
704 def test_gzip_request(self
):
706 t
.encode_threshold
= None
707 p
= xmlrpclib
.ServerProxy(URL
, transport
=t
)
708 self
.assertEqual(p
.pow(6,8), 6**8)
709 a
= self
.RequestHandler
.content_length
710 t
.encode_threshold
= 0 #turn on request encoding
711 self
.assertEqual(p
.pow(6,8), 6**8)
712 b
= self
.RequestHandler
.content_length
715 def test_bad_gzip_request(self
):
717 t
.encode_threshold
= None
719 p
= xmlrpclib
.ServerProxy(URL
, transport
=t
)
720 cm
= self
.assertRaisesRegexp(xmlrpclib
.ProtocolError
,
721 re
.compile(r
"\b400\b"))
725 def test_gsip_response(self
):
727 p
= xmlrpclib
.ServerProxy(URL
, transport
=t
)
728 old
= self
.requestHandler
.encode_threshold
729 self
.requestHandler
.encode_threshold
= None #no encoding
730 self
.assertEqual(p
.pow(6,8), 6**8)
731 a
= t
.response_length
732 self
.requestHandler
.encode_threshold
= 0 #always encode
733 self
.assertEqual(p
.pow(6,8), 6**8)
734 b
= t
.response_length
735 self
.requestHandler
.encode_threshold
= old
738 #Test special attributes of the ServerProxy object
739 class ServerProxyTestCase(unittest
.TestCase
):
741 unittest
.TestCase
.setUp(self
)
745 # Without threading, http_server() and http_multi_server() will not
746 # be executed and URL is still equal to None. 'http://' is a just
747 # enough to choose the scheme (HTTP)
750 def test_close(self
):
751 p
= xmlrpclib
.ServerProxy(self
.url
)
752 self
.assertEqual(p('close')(), None)
754 def test_transport(self
):
755 t
= xmlrpclib
.Transport()
756 p
= xmlrpclib
.ServerProxy(self
.url
, transport
=t
)
757 self
.assertEqual(p('transport'), t
)
759 # This is a contrived way to make a failure occur on the server side
760 # in order to test the _send_traceback_header flag on the server
761 class FailingMessageClass(mimetools
.Message
):
762 def __getitem__(self
, key
):
764 if key
== 'content-length':
766 return mimetools
.Message
.__getitem
__(self
, key
)
769 @unittest.skipUnless(threading
, 'Threading required for this test.')
770 class FailingServerTestCase(unittest
.TestCase
):
772 self
.evt
= threading
.Event()
773 # start server thread to handle requests
774 serv_args
= (self
.evt
, 1)
775 threading
.Thread(target
=http_server
, args
=serv_args
).start()
777 # wait for the server to be ready
782 # wait on the server thread to terminate
785 SimpleXMLRPCServer
.SimpleXMLRPCServer
._send
_traceback
_header
= False
786 # reset message class
787 SimpleXMLRPCServer
.SimpleXMLRPCRequestHandler
.MessageClass
= mimetools
.Message
789 def test_basic(self
):
790 # check that flag is false by default
791 flagval
= SimpleXMLRPCServer
.SimpleXMLRPCServer
._send
_traceback
_header
792 self
.assertEqual(flagval
, False)
794 # enable traceback reporting
795 SimpleXMLRPCServer
.SimpleXMLRPCServer
._send
_traceback
_header
= True
797 # test a call that shouldn't fail just as a smoke test
799 p
= xmlrpclib
.ServerProxy(URL
)
800 self
.assertEqual(p
.pow(6,8), 6**8)
801 except (xmlrpclib
.ProtocolError
, socket
.error
), e
:
802 # ignore failures due to non-blocking socket 'unavailable' errors
803 if not is_unavailable_exception(e
):
804 # protocol error; provide additional information in test output
805 self
.fail("%s\n%s" % (e
, getattr(e
, "headers", "")))
807 def test_fail_no_info(self
):
808 # use the broken message class
809 SimpleXMLRPCServer
.SimpleXMLRPCRequestHandler
.MessageClass
= FailingMessageClass
812 p
= xmlrpclib
.ServerProxy(URL
)
814 except (xmlrpclib
.ProtocolError
, socket
.error
), e
:
815 # ignore failures due to non-blocking socket 'unavailable' errors
816 if not is_unavailable_exception(e
) and hasattr(e
, "headers"):
817 # The two server-side error headers shouldn't be sent back in this case
818 self
.assertTrue(e
.headers
.get("X-exception") is None)
819 self
.assertTrue(e
.headers
.get("X-traceback") is None)
821 self
.fail('ProtocolError not raised')
823 def test_fail_with_info(self
):
824 # use the broken message class
825 SimpleXMLRPCServer
.SimpleXMLRPCRequestHandler
.MessageClass
= FailingMessageClass
827 # Check that errors in the server send back exception/traceback
828 # info when flag is set
829 SimpleXMLRPCServer
.SimpleXMLRPCServer
._send
_traceback
_header
= True
832 p
= xmlrpclib
.ServerProxy(URL
)
834 except (xmlrpclib
.ProtocolError
, socket
.error
), e
:
835 # ignore failures due to non-blocking socket 'unavailable' errors
836 if not is_unavailable_exception(e
) and hasattr(e
, "headers"):
837 # We should get error info in the response
838 expected_err
= "invalid literal for int() with base 10: 'I am broken'"
839 self
.assertEqual(e
.headers
.get("x-exception"), expected_err
)
840 self
.assertTrue(e
.headers
.get("x-traceback") is not None)
842 self
.fail('ProtocolError not raised')
844 class CGIHandlerTestCase(unittest
.TestCase
):
846 self
.cgi
= SimpleXMLRPCServer
.CGIXMLRPCRequestHandler()
851 def test_cgi_get(self
):
852 with test_support
.EnvironmentVarGuard() as env
:
853 env
['REQUEST_METHOD'] = 'GET'
854 # if the method is GET and no request_text is given, it runs handle_get
856 with test_support
.captured_stdout() as data_out
:
857 self
.cgi
.handle_request()
859 # parse Status header
861 handle
= data_out
.read()
862 status
= handle
.split()[1]
863 message
= ' '.join(handle
.split()[2:4])
865 self
.assertEqual(status
, '400')
866 self
.assertEqual(message
, 'Bad Request')
869 def test_cgi_xmlrpc_response(self
):
870 data
= """<?xml version='1.0'?>
872 <methodName>test_method</methodName>
875 <value><string>foo</string></value>
878 <value><string>bar</string></value>
884 with test_support
.EnvironmentVarGuard() as env
, \
885 test_support
.captured_stdout() as data_out
, \
886 test_support
.captured_stdin() as data_in
:
889 env
['CONTENT_LENGTH'] = str(len(data
))
890 self
.cgi
.handle_request()
893 # will respond exception, if so, our goal is achieved ;)
894 handle
= data_out
.read()
896 # start with 44th char so as not to get http header, we just need only xml
897 self
.assertRaises(xmlrpclib
.Fault
, xmlrpclib
.loads
, handle
[44:])
899 # Also test the content-length returned by handle_request
900 # Using the same test method inorder to avoid all the datapassing
902 # Test for bug: http://bugs.python.org/issue5040
904 content
= handle
[handle
.find("<?xml"):]
907 int(re
.search('Content-Length: (\d+)', handle
).group(1)),
914 self
.data
= StringIO
.StringIO()
920 def sendall(self
, buf
):
924 return self
.data
.getvalue()
926 def makefile(self
, x
='r', y
=-1):
932 class FakeTransport(xmlrpclib
.Transport
):
933 """A Transport instance that records instead of sending a request.
935 This class replaces the actual socket used by httplib with a
936 FakeSocket object that records the request. It doesn't provide a
940 def make_connection(self
, host
):
941 conn
= xmlrpclib
.Transport
.make_connection(self
, host
)
942 conn
.sock
= self
.fake_socket
= FakeSocket()
945 class TransportSubclassTestCase(unittest
.TestCase
):
947 def issue_request(self
, transport_class
):
948 """Return an HTTP request made via transport_class."""
949 transport
= transport_class()
950 proxy
= xmlrpclib
.ServerProxy("http://example.com/",
955 return transport
.fake_socket
.getvalue()
958 def test_custom_user_agent(self
):
959 class TestTransport(FakeTransport
):
961 def send_user_agent(self
, conn
):
962 xmlrpclib
.Transport
.send_user_agent(self
, conn
)
963 conn
.putheader("X-Test", "test_custom_user_agent")
965 req
= self
.issue_request(TestTransport
)
966 self
.assertIn("X-Test: test_custom_user_agent\r\n", req
)
968 def test_send_host(self
):
969 class TestTransport(FakeTransport
):
971 def send_host(self
, conn
, host
):
972 xmlrpclib
.Transport
.send_host(self
, conn
, host
)
973 conn
.putheader("X-Test", "test_send_host")
975 req
= self
.issue_request(TestTransport
)
976 self
.assertIn("X-Test: test_send_host\r\n", req
)
978 def test_send_request(self
):
979 class TestTransport(FakeTransport
):
981 def send_request(self
, conn
, url
, body
):
982 xmlrpclib
.Transport
.send_request(self
, conn
, url
, body
)
983 conn
.putheader("X-Test", "test_send_request")
985 req
= self
.issue_request(TestTransport
)
986 self
.assertIn("X-Test: test_send_request\r\n", req
)
988 def test_send_content(self
):
989 class TestTransport(FakeTransport
):
991 def send_content(self
, conn
, body
):
992 conn
.putheader("X-Test", "test_send_content")
993 xmlrpclib
.Transport
.send_content(self
, conn
, body
)
995 req
= self
.issue_request(TestTransport
)
996 self
.assertIn("X-Test: test_send_content\r\n", req
)
998 @test_support.reap_threads
1000 xmlrpc_tests
= [XMLRPCTestCase
, HelperTestCase
, DateTimeTestCase
,
1001 BinaryTestCase
, FaultTestCase
, TransportSubclassTestCase
]
1002 xmlrpc_tests
.append(SimpleServerTestCase
)
1003 xmlrpc_tests
.append(KeepaliveServerTestCase1
)
1004 xmlrpc_tests
.append(KeepaliveServerTestCase2
)
1007 xmlrpc_tests
.append(GzipServerTestCase
)
1009 pass #gzip not supported in this build
1010 xmlrpc_tests
.append(MultiPathServerTestCase
)
1011 xmlrpc_tests
.append(ServerProxyTestCase
)
1012 xmlrpc_tests
.append(FailingServerTestCase
)
1013 xmlrpc_tests
.append(CGIHandlerTestCase
)
1015 test_support
.run_unittest(*xmlrpc_tests
)
1017 if __name__
== "__main__":