mirror of
https://github.com/python/cpython.git
synced 2024-12-24 17:23:54 +08:00
45df820591
svn+ssh://pythondev@svn.python.org/python/trunk ........ r80552 | victor.stinner | 2010-04-27 23:46:03 +0200 (mar., 27 avril 2010) | 3 lines Issue #7449, part 1: fix test_support.py for Python compiled without thread ........ r80553 | victor.stinner | 2010-04-27 23:47:01 +0200 (mar., 27 avril 2010) | 1 line Issue #7449, part 2: regrtest.py -j option requires thread support ........ r80554 | victor.stinner | 2010-04-27 23:51:26 +0200 (mar., 27 avril 2010) | 9 lines Issue #7449 part 3, test_doctest: import trace module in test_coverage() Import trace module fail if the threading module is missing. test_coverage() is only used if test_doctest.py is used with the -c option. This commit allows to execute the test suite without thread support. Move "import trace" in test_coverage() and use test_support.import_module('trace'). ........ r80555 | victor.stinner | 2010-04-27 23:56:26 +0200 (mar., 27 avril 2010) | 6 lines Issue #7449, part 4: skip test_multiprocessing if thread support is disabled import threading after _multiprocessing to raise a more revelant error message: "No module named _multiprocessing". _multiprocessing is not compiled without thread support. ........ r80556 | victor.stinner | 2010-04-28 00:01:24 +0200 (mer., 28 avril 2010) | 8 lines Issue #7449, part 5: split Test.test_open() of ctypes/test/test_errno.py * Split Test.test_open() in 2 functions: test_open() and test_thread_open() * Skip test_open() and test_thread_open() if we are unable to find the C library * Skip test_thread_open() if thread support is disabled * Use unittest.skipUnless(os.name == "nt", ...) on test_GetLastError() ........ r80564 | victor.stinner | 2010-04-28 00:59:35 +0200 (mer., 28 avril 2010) | 4 lines Issue #7449, part 6: fix test_hashlib for missing threading module Move @test_support.reap_thread decorator from test_main() to test_threaded_hashing(). ........ r80565 | victor.stinner | 2010-04-28 01:01:29 +0200 (mer., 28 avril 2010) | 6 lines Issue #7449, part 7: simplify threading detection in test_capi * Skip TestPendingCalls if threading module is missing * Test if threading module is present or not, instead of test the presence of _testcapi._test_thread_state ........ r80566 | victor.stinner | 2010-04-28 01:03:16 +0200 (mer., 28 avril 2010) | 4 lines Issue #7449, part 8: don't skip the whole test_asynchat if threading is missing TestFifo can be executed without the threading module ........ r80568 | victor.stinner | 2010-04-28 01:14:58 +0200 (mer., 28 avril 2010) | 6 lines Issue #7449, part 9: fix test_xmlrpclib for missing threading module * Skip testcases using threads if threading module is missing * Use "http://" instead of URL in ServerProxyTestCase if threading is missing because URL is not set in this case ........ r80569 | victor.stinner | 2010-04-28 01:33:58 +0200 (mer., 28 avril 2010) | 6 lines Partial revert of r80556 (Issue #7449, part 5, fix ctypes test) Rewrite r80556: the thread test have to be executed just after the test on libc_open() and so the test cannot be splitted in two functions (without duplicating code, and I don't want to duplicate code). ........ r80570 | victor.stinner | 2010-04-28 01:51:16 +0200 (mer., 28 avril 2010) | 8 lines Issue #7449, part 10: test_cmd imports trace module using test_support.import_module() Use test_support.import_module() instead of import to raise a SkipTest exception if the import fail. Import trace fails if the threading module is missing. See also part 3: test_doctest: import trace module in test_coverage(). ........ r80571 | victor.stinner | 2010-04-28 01:55:59 +0200 (mer., 28 avril 2010) | 6 lines Issue #7449, last part (11): fix many tests if thread support is disabled * Use try/except ImportError or test_support.import_module() to import thread and threading modules * Add @unittest.skipUnless(threading, ...) to testcases using threads ........
199 lines
7.6 KiB
Python
199 lines
7.6 KiB
Python
from xmlrpc.server import DocXMLRPCServer
|
|
import http.client
|
|
import sys
|
|
from test import support
|
|
threading = support.import_module('threading')
|
|
import time
|
|
import socket
|
|
import unittest
|
|
|
|
PORT = None
|
|
|
|
def make_request_and_skipIf(condition, reason):
|
|
# If we skip the test, we have to make a request because the
|
|
# the server created in setUp blocks expecting one to come in.
|
|
if not condition:
|
|
return lambda func: func
|
|
def decorator(func):
|
|
def make_request_and_skip(self):
|
|
self.client.request("GET", "/")
|
|
self.client.getresponse()
|
|
raise unittest.SkipTest(reason)
|
|
return make_request_and_skip
|
|
return decorator
|
|
|
|
|
|
def server(evt, numrequests):
|
|
serv = DocXMLRPCServer(("localhost", 0), logRequests=False)
|
|
|
|
try:
|
|
global PORT
|
|
PORT = serv.socket.getsockname()[1]
|
|
|
|
# Add some documentation
|
|
serv.set_server_title("DocXMLRPCServer Test Documentation")
|
|
serv.set_server_name("DocXMLRPCServer Test Docs")
|
|
serv.set_server_documentation(
|
|
"This is an XML-RPC server's documentation, but the server "
|
|
"can be used by POSTing to /RPC2. Try self.add, too.")
|
|
|
|
# Create and register classes and functions
|
|
class TestClass(object):
|
|
def test_method(self, arg):
|
|
"""Test method's docs. This method truly does very little."""
|
|
self.arg = arg
|
|
|
|
serv.register_introspection_functions()
|
|
serv.register_instance(TestClass())
|
|
|
|
def add(x, y):
|
|
"""Add two instances together. This follows PEP008, but has nothing
|
|
to do with RFC1952. Case should matter: pEp008 and rFC1952. Things
|
|
that start with http and ftp should be auto-linked, too:
|
|
http://google.com.
|
|
"""
|
|
return x + y
|
|
|
|
serv.register_function(add)
|
|
serv.register_function(lambda x, y: x-y)
|
|
|
|
while numrequests > 0:
|
|
serv.handle_request()
|
|
numrequests -= 1
|
|
except socket.timeout:
|
|
pass
|
|
finally:
|
|
serv.server_close()
|
|
PORT = None
|
|
evt.set()
|
|
|
|
class DocXMLRPCHTTPGETServer(unittest.TestCase):
|
|
def setUp(self):
|
|
self._threads = support.threading_setup()
|
|
# Enable server feedback
|
|
DocXMLRPCServer._send_traceback_header = True
|
|
|
|
self.evt = threading.Event()
|
|
threading.Thread(target=server, args=(self.evt, 1)).start()
|
|
|
|
# wait for port to be assigned
|
|
n = 1000
|
|
while n > 0 and PORT is None:
|
|
time.sleep(0.001)
|
|
n -= 1
|
|
|
|
self.client = http.client.HTTPConnection("localhost:%d" % PORT)
|
|
|
|
def tearDown(self):
|
|
self.client.close()
|
|
|
|
self.evt.wait()
|
|
|
|
# Disable server feedback
|
|
DocXMLRPCServer._send_traceback_header = False
|
|
support.threading_cleanup(*self._threads)
|
|
|
|
def test_valid_get_response(self):
|
|
self.client.request("GET", "/")
|
|
response = self.client.getresponse()
|
|
|
|
self.assertEqual(response.status, 200)
|
|
self.assertEqual(response.getheader("Content-type"), "text/html")
|
|
|
|
# Server throws an exception if we don't start to read the data
|
|
response.read()
|
|
|
|
def test_invalid_get_response(self):
|
|
self.client.request("GET", "/spam")
|
|
response = self.client.getresponse()
|
|
|
|
self.assertEqual(response.status, 404)
|
|
self.assertEqual(response.getheader("Content-type"), "text/plain")
|
|
|
|
response.read()
|
|
|
|
def test_lambda(self):
|
|
"""Test that lambda functionality stays the same. The output produced
|
|
currently is, I suspect invalid because of the unencoded brackets in the
|
|
HTML, "<lambda>".
|
|
|
|
The subtraction lambda method is tested.
|
|
"""
|
|
self.client.request("GET", "/")
|
|
response = self.client.getresponse()
|
|
|
|
self.assertIn((b'<dl><dt><a name="-<lambda>"><strong>'
|
|
b'<lambda></strong></a>(x, y)</dt></dl>'),
|
|
response.read())
|
|
|
|
@make_request_and_skipIf(sys.flags.optimize >= 2,
|
|
"Docstrings are omitted with -O2 and above")
|
|
def test_autolinking(self):
|
|
"""Test that the server correctly automatically wraps references to
|
|
PEPS and RFCs with links, and that it linkifies text starting with
|
|
http or ftp protocol prefixes.
|
|
|
|
The documentation for the "add" method contains the test material.
|
|
"""
|
|
self.client.request("GET", "/")
|
|
response = self.client.getresponse().read()
|
|
|
|
self.assertIn(
|
|
(b'<dl><dt><a name="-add"><strong>add</strong></a>(x, y)</dt><dd>'
|
|
b'<tt>Add two instances together. This '
|
|
b'follows <a href="http://www.python.org/dev/peps/pep-0008/">'
|
|
b'PEP008</a>, but has nothing<br>\nto do '
|
|
b'with <a href="http://www.rfc-editor.org/rfc/rfc1952.txt">'
|
|
b'RFC1952</a>. Case should matter: pEp008 '
|
|
b'and rFC1952. Things<br>\nthat start '
|
|
b'with http and ftp should be '
|
|
b'auto-linked, too:<br>\n<a href="http://google.com">'
|
|
b'http://google.com</a>.</tt></dd></dl>'), response)
|
|
|
|
@make_request_and_skipIf(sys.flags.optimize >= 2,
|
|
"Docstrings are omitted with -O2 and above")
|
|
def test_system_methods(self):
|
|
"""Test the precense of three consecutive system.* methods.
|
|
|
|
This also tests their use of parameter type recognition and the
|
|
systems related to that process.
|
|
"""
|
|
self.client.request("GET", "/")
|
|
response = self.client.getresponse().read()
|
|
|
|
self.assertIn(
|
|
(b'<dl><dt><a name="-system.methodHelp"><strong>system.methodHelp'
|
|
b'</strong></a>(method_name)</dt><dd><tt><a href="#-system.method'
|
|
b'Help">system.methodHelp</a>(\'add\') => "Adds '
|
|
b'two integers together"<br>\n <br>\nReturns a'
|
|
b' string containing documentation for '
|
|
b'the specified method.</tt></dd></dl>\n<dl><dt><a name'
|
|
b'="-system.methodSignature"><strong>system.methodSignature</strong>'
|
|
b'</a>(method_name)</dt><dd><tt><a href="#-system.methodSignature">'
|
|
b'system.methodSignature</a>(\'add\') => [double, '
|
|
b'int, int]<br>\n <br>\nReturns a list '
|
|
b'describing the signature of the method.'
|
|
b' In the<br>\nabove example, the add '
|
|
b'method takes two integers as arguments'
|
|
b'<br>\nand returns a double result.<br>\n '
|
|
b'<br>\nThis server does NOT support system'
|
|
b'.methodSignature.</tt></dd></dl>\n<dl><dt><a name="-test_method">'
|
|
b'<strong>test_method</strong></a>(arg)</dt><dd><tt>Test '
|
|
b'method\'s docs. This method truly does'
|
|
b' very little.</tt></dd></dl>'), response)
|
|
|
|
def test_autolink_dotted_methods(self):
|
|
"""Test that selfdot values are made strong automatically in the
|
|
documentation."""
|
|
self.client.request("GET", "/")
|
|
response = self.client.getresponse()
|
|
|
|
self.assertIn(b"""Try self.<strong>add</strong>, too.""",
|
|
response.read())
|
|
|
|
def test_main():
|
|
support.run_unittest(DocXMLRPCHTTPGETServer)
|
|
|
|
if __name__ == '__main__':
|
|
test_main()
|