test_docxmlrpc.py 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226
  1. from xmlrpc.server import DocXMLRPCServer
  2. import http.client
  3. import re
  4. import sys
  5. import threading
  6. import unittest
  7. from test import support
  8. support.requires_working_socket(module=True)
  9. def make_request_and_skipIf(condition, reason):
  10. # If we skip the test, we have to make a request because
  11. # the server created in setUp blocks expecting one to come in.
  12. if not condition:
  13. return lambda func: func
  14. def decorator(func):
  15. def make_request_and_skip(self):
  16. self.client.request("GET", "/")
  17. self.client.getresponse()
  18. raise unittest.SkipTest(reason)
  19. return make_request_and_skip
  20. return decorator
  21. def make_server():
  22. serv = DocXMLRPCServer(("localhost", 0), logRequests=False)
  23. try:
  24. # Add some documentation
  25. serv.set_server_title("DocXMLRPCServer Test Documentation")
  26. serv.set_server_name("DocXMLRPCServer Test Docs")
  27. serv.set_server_documentation(
  28. "This is an XML-RPC server's documentation, but the server "
  29. "can be used by POSTing to /RPC2. Try self.add, too.")
  30. # Create and register classes and functions
  31. class TestClass(object):
  32. def test_method(self, arg):
  33. """Test method's docs. This method truly does very little."""
  34. self.arg = arg
  35. serv.register_introspection_functions()
  36. serv.register_instance(TestClass())
  37. def add(x, y):
  38. """Add two instances together. This follows PEP008, but has nothing
  39. to do with RFC1952. Case should matter: pEp008 and rFC1952. Things
  40. that start with http and ftp should be auto-linked, too:
  41. http://google.com.
  42. """
  43. return x + y
  44. def annotation(x: int):
  45. """ Use function annotations. """
  46. return x
  47. class ClassWithAnnotation:
  48. def method_annotation(self, x: bytes):
  49. return x.decode()
  50. serv.register_function(add)
  51. serv.register_function(lambda x, y: x-y)
  52. serv.register_function(annotation)
  53. serv.register_instance(ClassWithAnnotation())
  54. return serv
  55. except:
  56. serv.server_close()
  57. raise
  58. class DocXMLRPCHTTPGETServer(unittest.TestCase):
  59. def setUp(self):
  60. # Enable server feedback
  61. DocXMLRPCServer._send_traceback_header = True
  62. self.serv = make_server()
  63. self.thread = threading.Thread(target=self.serv.serve_forever)
  64. self.thread.start()
  65. PORT = self.serv.server_address[1]
  66. self.client = http.client.HTTPConnection("localhost:%d" % PORT)
  67. def tearDown(self):
  68. self.client.close()
  69. # Disable server feedback
  70. DocXMLRPCServer._send_traceback_header = False
  71. self.serv.shutdown()
  72. self.thread.join()
  73. self.serv.server_close()
  74. def test_valid_get_response(self):
  75. self.client.request("GET", "/")
  76. response = self.client.getresponse()
  77. self.assertEqual(response.status, 200)
  78. self.assertEqual(response.getheader("Content-type"), "text/html; charset=UTF-8")
  79. # Server raises an exception if we don't start to read the data
  80. response.read()
  81. def test_get_css(self):
  82. self.client.request("GET", "/pydoc.css")
  83. response = self.client.getresponse()
  84. self.assertEqual(response.status, 200)
  85. self.assertEqual(response.getheader("Content-type"), "text/css; charset=UTF-8")
  86. # Server raises an exception if we don't start to read the data
  87. response.read()
  88. def test_invalid_get_response(self):
  89. self.client.request("GET", "/spam")
  90. response = self.client.getresponse()
  91. self.assertEqual(response.status, 404)
  92. self.assertEqual(response.getheader("Content-type"), "text/plain")
  93. response.read()
  94. def test_lambda(self):
  95. """Test that lambda functionality stays the same. The output produced
  96. currently is, I suspect invalid because of the unencoded brackets in the
  97. HTML, "<lambda>".
  98. The subtraction lambda method is tested.
  99. """
  100. self.client.request("GET", "/")
  101. response = self.client.getresponse()
  102. self.assertIn((b'<dl><dt><a name="-&lt;lambda&gt;"><strong>'
  103. b'&lt;lambda&gt;</strong></a>(x, y)</dt></dl>'),
  104. response.read())
  105. @make_request_and_skipIf(sys.flags.optimize >= 2,
  106. "Docstrings are omitted with -O2 and above")
  107. def test_autolinking(self):
  108. """Test that the server correctly automatically wraps references to
  109. PEPS and RFCs with links, and that it linkifies text starting with
  110. http or ftp protocol prefixes.
  111. The documentation for the "add" method contains the test material.
  112. """
  113. self.client.request("GET", "/")
  114. response = self.client.getresponse().read()
  115. self.assertIn(
  116. (b'<dl><dt><a name="-add"><strong>add</strong></a>(x, y)</dt><dd>'
  117. b'<tt>Add&nbsp;two&nbsp;instances&nbsp;together.&nbsp;This&nbsp;'
  118. b'follows&nbsp;<a href="https://peps.python.org/pep-0008/">'
  119. b'PEP008</a>,&nbsp;but&nbsp;has&nbsp;nothing<br>\nto&nbsp;do&nbsp;'
  120. b'with&nbsp;<a href="https://www.rfc-editor.org/rfc/rfc1952.txt">'
  121. b'RFC1952</a>.&nbsp;Case&nbsp;should&nbsp;matter:&nbsp;pEp008&nbsp;'
  122. b'and&nbsp;rFC1952.&nbsp;&nbsp;Things<br>\nthat&nbsp;start&nbsp;'
  123. b'with&nbsp;http&nbsp;and&nbsp;ftp&nbsp;should&nbsp;be&nbsp;'
  124. b'auto-linked,&nbsp;too:<br>\n<a href="http://google.com">'
  125. b'http://google.com</a>.</tt></dd></dl>'), response)
  126. @make_request_and_skipIf(sys.flags.optimize >= 2,
  127. "Docstrings are omitted with -O2 and above")
  128. def test_system_methods(self):
  129. """Test the presence of three consecutive system.* methods.
  130. This also tests their use of parameter type recognition and the
  131. systems related to that process.
  132. """
  133. self.client.request("GET", "/")
  134. response = self.client.getresponse().read()
  135. self.assertIn(
  136. (b'<dl><dt><a name="-system.methodHelp"><strong>system.methodHelp'
  137. b'</strong></a>(method_name)</dt><dd><tt><a href="#-system.method'
  138. b'Help">system.methodHelp</a>(\'add\')&nbsp;=&gt;&nbsp;"Adds&nbsp;'
  139. b'two&nbsp;integers&nbsp;together"<br>\n&nbsp;<br>\nReturns&nbsp;a'
  140. b'&nbsp;string&nbsp;containing&nbsp;documentation&nbsp;for&nbsp;'
  141. b'the&nbsp;specified&nbsp;method.</tt></dd></dl>\n<dl><dt><a name'
  142. b'="-system.methodSignature"><strong>system.methodSignature</strong>'
  143. b'</a>(method_name)</dt><dd><tt><a href="#-system.methodSignature">'
  144. b'system.methodSignature</a>(\'add\')&nbsp;=&gt;&nbsp;[double,&nbsp;'
  145. b'int,&nbsp;int]<br>\n&nbsp;<br>\nReturns&nbsp;a&nbsp;list&nbsp;'
  146. b'describing&nbsp;the&nbsp;signature&nbsp;of&nbsp;the&nbsp;method.'
  147. b'&nbsp;In&nbsp;the<br>\nabove&nbsp;example,&nbsp;the&nbsp;add&nbsp;'
  148. b'method&nbsp;takes&nbsp;two&nbsp;integers&nbsp;as&nbsp;arguments'
  149. b'<br>\nand&nbsp;returns&nbsp;a&nbsp;double&nbsp;result.<br>\n&nbsp;'
  150. b'<br>\nThis&nbsp;server&nbsp;does&nbsp;NOT&nbsp;support&nbsp;system'
  151. b'.methodSignature.</tt></dd></dl>'), response)
  152. def test_autolink_dotted_methods(self):
  153. """Test that selfdot values are made strong automatically in the
  154. documentation."""
  155. self.client.request("GET", "/")
  156. response = self.client.getresponse()
  157. self.assertIn(b"""Try&nbsp;self.<strong>add</strong>,&nbsp;too.""",
  158. response.read())
  159. def test_annotations(self):
  160. """ Test that annotations works as expected """
  161. self.client.request("GET", "/")
  162. response = self.client.getresponse()
  163. docstring = (b'' if sys.flags.optimize >= 2 else
  164. b'<dd><tt>Use&nbsp;function&nbsp;annotations.</tt></dd>')
  165. self.assertIn(
  166. (b'<dl><dt><a name="-annotation"><strong>annotation</strong></a>'
  167. b'(x: int)</dt>' + docstring + b'</dl>\n'
  168. b'<dl><dt><a name="-method_annotation"><strong>'
  169. b'method_annotation</strong></a>(x: bytes)</dt></dl>'),
  170. response.read())
  171. def test_server_title_escape(self):
  172. # bpo-38243: Ensure that the server title and documentation
  173. # are escaped for HTML.
  174. self.serv.set_server_title('test_title<script>')
  175. self.serv.set_server_documentation('test_documentation<script>')
  176. self.assertEqual('test_title<script>', self.serv.server_title)
  177. self.assertEqual('test_documentation<script>',
  178. self.serv.server_documentation)
  179. generated = self.serv.generate_html_documentation()
  180. title = re.search(r'<title>(.+?)</title>', generated).group()
  181. documentation = re.search(r'<p><tt>(.+?)</tt></p>', generated).group()
  182. self.assertEqual('<title>Python: test_title&lt;script&gt;</title>', title)
  183. self.assertEqual('<p><tt>test_documentation&lt;script&gt;</tt></p>', documentation)
  184. if __name__ == '__main__':
  185. unittest.main()