2008-05-26 08:14:17 -03:00
|
|
|
from xmlrpc.server import DocXMLRPCServer
|
2008-05-26 13:32:26 -03:00
|
|
|
import http.client
|
2019-09-27 16:59:37 -03:00
|
|
|
import re
|
2010-02-23 21:46:21 -04:00
|
|
|
import sys
|
2017-09-07 13:56:24 -03:00
|
|
|
import threading
|
2007-12-10 12:18:49 -04:00
|
|
|
import unittest
|
2022-03-22 07:04:36 -03:00
|
|
|
from test import support
|
|
|
|
|
|
|
|
support.requires_working_socket(module=True)
|
2007-12-10 12:18:49 -04:00
|
|
|
|
2010-02-23 21:46:21 -04:00
|
|
|
def make_request_and_skipIf(condition, reason):
|
2014-12-01 12:28:43 -04:00
|
|
|
# If we skip the test, we have to make a request because
|
2010-02-23 21:46:21 -04:00
|
|
|
# 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
|
|
|
|
|
|
|
|
|
2016-08-20 03:50:58 -03:00
|
|
|
def make_server():
|
2008-10-10 21:49:57 -03:00
|
|
|
serv = DocXMLRPCServer(("localhost", 0), logRequests=False)
|
2007-12-10 12:18:49 -04:00
|
|
|
|
2008-10-10 21:49:57 -03:00
|
|
|
try:
|
2007-12-10 12:18:49 -04:00
|
|
|
# Add some documentation
|
|
|
|
serv.set_server_title("DocXMLRPCServer Test Documentation")
|
|
|
|
serv.set_server_name("DocXMLRPCServer Test Docs")
|
|
|
|
serv.set_server_documentation(
|
2010-01-23 11:40:09 -04:00
|
|
|
"This is an XML-RPC server's documentation, but the server "
|
|
|
|
"can be used by POSTing to /RPC2. Try self.add, too.")
|
2007-12-10 12:18:49 -04:00
|
|
|
|
|
|
|
# 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
|
|
|
|
|
2013-08-10 13:01:47 -03:00
|
|
|
def annotation(x: int):
|
|
|
|
""" Use function annotations. """
|
|
|
|
return x
|
|
|
|
|
|
|
|
class ClassWithAnnotation:
|
|
|
|
def method_annotation(self, x: bytes):
|
|
|
|
return x.decode()
|
|
|
|
|
2007-12-10 12:18:49 -04:00
|
|
|
serv.register_function(add)
|
|
|
|
serv.register_function(lambda x, y: x-y)
|
2013-08-10 13:01:47 -03:00
|
|
|
serv.register_function(annotation)
|
|
|
|
serv.register_instance(ClassWithAnnotation())
|
2016-08-20 03:50:58 -03:00
|
|
|
return serv
|
|
|
|
except:
|
2007-12-10 12:18:49 -04:00
|
|
|
serv.server_close()
|
2016-08-20 03:50:58 -03:00
|
|
|
raise
|
2007-12-10 12:18:49 -04:00
|
|
|
|
|
|
|
class DocXMLRPCHTTPGETServer(unittest.TestCase):
|
|
|
|
def setUp(self):
|
|
|
|
# Enable server feedback
|
|
|
|
DocXMLRPCServer._send_traceback_header = True
|
|
|
|
|
2016-08-20 03:50:58 -03:00
|
|
|
self.serv = make_server()
|
|
|
|
self.thread = threading.Thread(target=self.serv.serve_forever)
|
|
|
|
self.thread.start()
|
2007-12-10 12:18:49 -04:00
|
|
|
|
2016-08-20 03:50:58 -03:00
|
|
|
PORT = self.serv.server_address[1]
|
2008-05-26 13:32:26 -03:00
|
|
|
self.client = http.client.HTTPConnection("localhost:%d" % PORT)
|
2007-12-10 12:18:49 -04:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.client.close()
|
|
|
|
|
|
|
|
# Disable server feedback
|
|
|
|
DocXMLRPCServer._send_traceback_header = False
|
2016-08-20 03:50:58 -03:00
|
|
|
self.serv.shutdown()
|
|
|
|
self.thread.join()
|
|
|
|
self.serv.server_close()
|
2007-12-10 12:18:49 -04:00
|
|
|
|
|
|
|
def test_valid_get_response(self):
|
|
|
|
self.client.request("GET", "/")
|
|
|
|
response = self.client.getresponse()
|
|
|
|
|
|
|
|
self.assertEqual(response.status, 200)
|
2021-10-09 04:36:50 -03:00
|
|
|
self.assertEqual(response.getheader("Content-type"), "text/html; charset=UTF-8")
|
|
|
|
|
|
|
|
# Server raises an exception if we don't start to read the data
|
|
|
|
response.read()
|
|
|
|
|
|
|
|
def test_get_css(self):
|
|
|
|
self.client.request("GET", "/pydoc.css")
|
|
|
|
response = self.client.getresponse()
|
|
|
|
|
|
|
|
self.assertEqual(response.status, 200)
|
|
|
|
self.assertEqual(response.getheader("Content-type"), "text/css; charset=UTF-8")
|
2007-12-10 12:18:49 -04:00
|
|
|
|
2012-12-18 15:14:22 -04:00
|
|
|
# Server raises an exception if we don't start to read the data
|
2007-12-10 12:18:49 -04:00
|
|
|
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()
|
|
|
|
|
2010-01-23 11:40:09 -04:00
|
|
|
self.assertIn((b'<dl><dt><a name="-<lambda>"><strong>'
|
|
|
|
b'<lambda></strong></a>(x, y)</dt></dl>'),
|
|
|
|
response.read())
|
2007-12-10 12:18:49 -04:00
|
|
|
|
2010-02-23 21:46:21 -04:00
|
|
|
@make_request_and_skipIf(sys.flags.optimize >= 2,
|
|
|
|
"Docstrings are omitted with -O2 and above")
|
2007-12-10 12:18:49 -04:00
|
|
|
def test_autolinking(self):
|
2010-02-23 21:46:21 -04:00
|
|
|
"""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.
|
2007-12-10 12:18:49 -04:00
|
|
|
|
|
|
|
The documentation for the "add" method contains the test material.
|
|
|
|
"""
|
|
|
|
self.client.request("GET", "/")
|
Merged revisions 60481,60485,60489-60492,60494-60496,60498-60499,60501-60503,60505-60506,60508-60509,60523-60524,60532,60543,60545,60547-60548,60552,60554,60556-60559,60561-60562,60568-60598,60600-60616 via svnmerge from
svn+ssh://pythondev@svn.python.org/python/trunk
........
r60568 | christian.heimes | 2008-02-04 19:48:38 +0100 (Mon, 04 Feb 2008) | 1 line
Increase debugging to investige failing tests on some build bots
........
r60570 | christian.heimes | 2008-02-04 20:30:05 +0100 (Mon, 04 Feb 2008) | 1 line
Small adjustments for test compact freelist test. It's no passing on Windows as well.
........
r60573 | amaury.forgeotdarc | 2008-02-04 21:53:14 +0100 (Mon, 04 Feb 2008) | 2 lines
Correct quotes in NEWS file
........
r60575 | amaury.forgeotdarc | 2008-02-04 22:45:05 +0100 (Mon, 04 Feb 2008) | 13 lines
#1750076: Debugger did not step on every iteration of a while statement.
The mapping between bytecode offsets and source lines (lnotab) did not contain
an entry for the beginning of the loop.
Now it does, and the lnotab can be a bit larger:
in particular, several statements on the same line generate several entries.
However, this does not bother the settrace function, which will trigger only
one 'line' event.
The lnotab seems to be exactly the same as with python2.4.
........
r60584 | amaury.forgeotdarc | 2008-02-05 01:26:21 +0100 (Tue, 05 Feb 2008) | 3 lines
Change r60575 broke test_compile:
there is no need to emit co_lnotab item when both offsets are zeros.
........
r60587 | skip.montanaro | 2008-02-05 03:32:16 +0100 (Tue, 05 Feb 2008) | 1 line
sync with most recent version from python-mode sf project
........
r60588 | lars.gustaebel | 2008-02-05 12:51:40 +0100 (Tue, 05 Feb 2008) | 5 lines
Issue #2004: Use mode 0700 for temporary directories and default
permissions for missing directories.
(will backport to 2.5)
........
r60590 | georg.brandl | 2008-02-05 13:01:24 +0100 (Tue, 05 Feb 2008) | 2 lines
Convert external links to internal links. Fixes #2010.
........
r60592 | marc-andre.lemburg | 2008-02-05 15:50:40 +0100 (Tue, 05 Feb 2008) | 3 lines
Keep distutils Python 2.1 compatible (or even Python 2.4 in this case).
........
r60593 | andrew.kuchling | 2008-02-05 17:06:57 +0100 (Tue, 05 Feb 2008) | 5 lines
Update PEP URL.
(This code is duplicated between pydoc and DocXMLRPCServer; maybe it
should be refactored as a GHOP project.)
2.5.2 backport candidate.
........
r60596 | guido.van.rossum | 2008-02-05 18:32:15 +0100 (Tue, 05 Feb 2008) | 2 lines
In the experimental 'Scanner' feature, the group count was set wrong.
........
r60602 | facundo.batista | 2008-02-05 20:03:32 +0100 (Tue, 05 Feb 2008) | 3 lines
Issue 1951. Converts wave test cases to unittest.
........
r60603 | georg.brandl | 2008-02-05 20:07:10 +0100 (Tue, 05 Feb 2008) | 2 lines
Actually run the test.
........
r60604 | skip.montanaro | 2008-02-05 20:24:30 +0100 (Tue, 05 Feb 2008) | 2 lines
correct object name
........
r60605 | georg.brandl | 2008-02-05 20:58:17 +0100 (Tue, 05 Feb 2008) | 7 lines
* Use the same code to profile for test_profile and test_cprofile.
* Convert both to unittest.
* Use the same unit testing code.
* Include the expected output in both test files.
* Make it possible to regenerate the expected output by running
the file as a script with an '-r' argument.
........
r60613 | raymond.hettinger | 2008-02-06 02:49:00 +0100 (Wed, 06 Feb 2008) | 1 line
Sync-up with Py3k work.
........
r60614 | christian.heimes | 2008-02-06 13:44:34 +0100 (Wed, 06 Feb 2008) | 1 line
Limit free list of method and builtin function objects to 256 entries each.
........
r60616 | christian.heimes | 2008-02-06 14:33:44 +0100 (Wed, 06 Feb 2008) | 7 lines
Unified naming convention for free lists and their limits. All free lists
in Object/ are named ``free_list``, the counter ``numfree`` and the upper
limit is a macro ``PyName_MAXFREELIST`` inside an #ifndef block.
The chances should make it easier to adjust Python for platforms with
less memory, e.g. mobile phones.
........
2008-02-06 10:31:34 -04:00
|
|
|
response = self.client.getresponse().read()
|
2007-12-10 12:18:49 -04:00
|
|
|
|
2010-01-23 11:40:09 -04:00
|
|
|
self.assertIn(
|
|
|
|
(b'<dl><dt><a name="-add"><strong>add</strong></a>(x, y)</dt><dd>'
|
|
|
|
b'<tt>Add two instances together. This '
|
2022-03-30 08:00:27 -03:00
|
|
|
b'follows <a href="https://peps.python.org/pep-0008/">'
|
2010-01-23 11:40:09 -04:00
|
|
|
b'PEP008</a>, but has nothing<br>\nto do '
|
2022-03-30 08:00:27 -03:00
|
|
|
b'with <a href="https://www.rfc-editor.org/rfc/rfc1952.txt">'
|
2010-01-23 11:40:09 -04:00
|
|
|
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)
|
2007-12-10 12:18:49 -04:00
|
|
|
|
2010-02-23 21:46:21 -04:00
|
|
|
@make_request_and_skipIf(sys.flags.optimize >= 2,
|
|
|
|
"Docstrings are omitted with -O2 and above")
|
2007-12-10 12:18:49 -04:00
|
|
|
def test_system_methods(self):
|
2016-08-04 10:07:31 -03:00
|
|
|
"""Test the presence of three consecutive system.* methods.
|
2007-12-10 12:18:49 -04:00
|
|
|
|
2010-02-23 21:46:21 -04:00
|
|
|
This also tests their use of parameter type recognition and the
|
|
|
|
systems related to that process.
|
2007-12-10 12:18:49 -04:00
|
|
|
"""
|
|
|
|
self.client.request("GET", "/")
|
2007-12-10 16:18:07 -04:00
|
|
|
response = self.client.getresponse().read()
|
2007-12-10 12:18:49 -04:00
|
|
|
|
2010-01-23 11:40:09 -04:00
|
|
|
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'
|
2013-08-10 13:01:47 -03:00
|
|
|
b'.methodSignature.</tt></dd></dl>'), response)
|
2007-12-10 12:18:49 -04:00
|
|
|
|
|
|
|
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()
|
|
|
|
|
2010-01-23 11:40:09 -04:00
|
|
|
self.assertIn(b"""Try self.<strong>add</strong>, too.""",
|
|
|
|
response.read())
|
2007-12-10 12:18:49 -04:00
|
|
|
|
2013-08-10 13:01:47 -03:00
|
|
|
def test_annotations(self):
|
|
|
|
""" Test that annotations works as expected """
|
|
|
|
self.client.request("GET", "/")
|
|
|
|
response = self.client.getresponse()
|
2013-12-08 12:14:49 -04:00
|
|
|
docstring = (b'' if sys.flags.optimize >= 2 else
|
|
|
|
b'<dd><tt>Use function annotations.</tt></dd>')
|
2013-08-10 13:01:47 -03:00
|
|
|
self.assertIn(
|
|
|
|
(b'<dl><dt><a name="-annotation"><strong>annotation</strong></a>'
|
2013-12-08 12:14:49 -04:00
|
|
|
b'(x: int)</dt>' + docstring + b'</dl>\n'
|
|
|
|
b'<dl><dt><a name="-method_annotation"><strong>'
|
2013-08-10 13:01:47 -03:00
|
|
|
b'method_annotation</strong></a>(x: bytes)</dt></dl>'),
|
|
|
|
response.read())
|
|
|
|
|
2019-09-27 16:59:37 -03:00
|
|
|
def test_server_title_escape(self):
|
|
|
|
# bpo-38243: Ensure that the server title and documentation
|
|
|
|
# are escaped for HTML.
|
|
|
|
self.serv.set_server_title('test_title<script>')
|
|
|
|
self.serv.set_server_documentation('test_documentation<script>')
|
|
|
|
self.assertEqual('test_title<script>', self.serv.server_title)
|
|
|
|
self.assertEqual('test_documentation<script>',
|
|
|
|
self.serv.server_documentation)
|
|
|
|
|
|
|
|
generated = self.serv.generate_html_documentation()
|
|
|
|
title = re.search(r'<title>(.+?)</title>', generated).group()
|
|
|
|
documentation = re.search(r'<p><tt>(.+?)</tt></p>', generated).group()
|
|
|
|
self.assertEqual('<title>Python: test_title<script></title>', title)
|
|
|
|
self.assertEqual('<p><tt>test_documentation<script></tt></p>', documentation)
|
|
|
|
|
2013-08-10 13:01:47 -03:00
|
|
|
|
2007-12-10 12:18:49 -04:00
|
|
|
if __name__ == '__main__':
|
2015-04-13 17:00:43 -03:00
|
|
|
unittest.main()
|