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