Georg Brandl | 38eceaa | 2008-05-26 11:14:17 +0000 | [diff] [blame] | 1 | from xmlrpc.server import DocXMLRPCServer |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 2 | import http.client |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 3 | import sys |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 4 | from test import support |
Victor Stinner | 45df820 | 2010-04-28 22:31:17 +0000 | [diff] [blame] | 5 | threading = support.import_module('threading') |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 6 | import time |
Georg Brandl | 89fad14 | 2010-03-14 10:23:39 +0000 | [diff] [blame] | 7 | import socket |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 8 | import unittest |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 9 | |
| 10 | PORT = None |
| 11 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 12 | def make_request_and_skipIf(condition, reason): |
| 13 | # If we skip the test, we have to make a request because the |
| 14 | # the server created in setUp blocks expecting one to come in. |
| 15 | if not condition: |
| 16 | return lambda func: func |
| 17 | def decorator(func): |
| 18 | def make_request_and_skip(self): |
| 19 | self.client.request("GET", "/") |
| 20 | self.client.getresponse() |
| 21 | raise unittest.SkipTest(reason) |
| 22 | return make_request_and_skip |
| 23 | return decorator |
| 24 | |
| 25 | |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 26 | def server(evt, numrequests): |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 27 | serv = DocXMLRPCServer(("localhost", 0), logRequests=False) |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 28 | |
Benjamin Peterson | f10a79a | 2008-10-11 00:49:57 +0000 | [diff] [blame] | 29 | try: |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 30 | global PORT |
| 31 | PORT = serv.socket.getsockname()[1] |
| 32 | |
| 33 | # Add some documentation |
| 34 | serv.set_server_title("DocXMLRPCServer Test Documentation") |
| 35 | serv.set_server_name("DocXMLRPCServer Test Docs") |
| 36 | serv.set_server_documentation( |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 37 | "This is an XML-RPC server's documentation, but the server " |
| 38 | "can be used by POSTing to /RPC2. Try self.add, too.") |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 39 | |
| 40 | # Create and register classes and functions |
| 41 | class TestClass(object): |
| 42 | def test_method(self, arg): |
| 43 | """Test method's docs. This method truly does very little.""" |
| 44 | self.arg = arg |
| 45 | |
| 46 | serv.register_introspection_functions() |
| 47 | serv.register_instance(TestClass()) |
| 48 | |
| 49 | def add(x, y): |
| 50 | """Add two instances together. This follows PEP008, but has nothing |
| 51 | to do with RFC1952. Case should matter: pEp008 and rFC1952. Things |
| 52 | that start with http and ftp should be auto-linked, too: |
| 53 | http://google.com. |
| 54 | """ |
| 55 | return x + y |
| 56 | |
R David Murray | f22b62e | 2013-08-10 12:01:47 -0400 | [diff] [blame] | 57 | def annotation(x: int): |
| 58 | """ Use function annotations. """ |
| 59 | return x |
| 60 | |
| 61 | class ClassWithAnnotation: |
| 62 | def method_annotation(self, x: bytes): |
| 63 | return x.decode() |
| 64 | |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 65 | serv.register_function(add) |
| 66 | serv.register_function(lambda x, y: x-y) |
R David Murray | f22b62e | 2013-08-10 12:01:47 -0400 | [diff] [blame] | 67 | serv.register_function(annotation) |
| 68 | serv.register_instance(ClassWithAnnotation()) |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 69 | |
| 70 | while numrequests > 0: |
| 71 | serv.handle_request() |
| 72 | numrequests -= 1 |
| 73 | except socket.timeout: |
| 74 | pass |
| 75 | finally: |
| 76 | serv.server_close() |
| 77 | PORT = None |
| 78 | evt.set() |
| 79 | |
| 80 | class DocXMLRPCHTTPGETServer(unittest.TestCase): |
| 81 | def setUp(self): |
Antoine Pitrou | d52656b | 2009-10-30 17:34:49 +0000 | [diff] [blame] | 82 | self._threads = support.threading_setup() |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 83 | # Enable server feedback |
| 84 | DocXMLRPCServer._send_traceback_header = True |
| 85 | |
| 86 | self.evt = threading.Event() |
| 87 | threading.Thread(target=server, args=(self.evt, 1)).start() |
| 88 | |
| 89 | # wait for port to be assigned |
| 90 | n = 1000 |
| 91 | while n > 0 and PORT is None: |
| 92 | time.sleep(0.001) |
| 93 | n -= 1 |
| 94 | |
Georg Brandl | 2442015 | 2008-05-26 16:32:26 +0000 | [diff] [blame] | 95 | self.client = http.client.HTTPConnection("localhost:%d" % PORT) |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 96 | |
| 97 | def tearDown(self): |
| 98 | self.client.close() |
| 99 | |
| 100 | self.evt.wait() |
| 101 | |
| 102 | # Disable server feedback |
| 103 | DocXMLRPCServer._send_traceback_header = False |
Antoine Pitrou | d52656b | 2009-10-30 17:34:49 +0000 | [diff] [blame] | 104 | support.threading_cleanup(*self._threads) |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 105 | |
| 106 | def test_valid_get_response(self): |
| 107 | self.client.request("GET", "/") |
| 108 | response = self.client.getresponse() |
| 109 | |
| 110 | self.assertEqual(response.status, 200) |
| 111 | self.assertEqual(response.getheader("Content-type"), "text/html") |
| 112 | |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 113 | # Server raises an exception if we don't start to read the data |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 114 | response.read() |
| 115 | |
| 116 | def test_invalid_get_response(self): |
| 117 | self.client.request("GET", "/spam") |
| 118 | response = self.client.getresponse() |
| 119 | |
| 120 | self.assertEqual(response.status, 404) |
| 121 | self.assertEqual(response.getheader("Content-type"), "text/plain") |
| 122 | |
| 123 | response.read() |
| 124 | |
| 125 | def test_lambda(self): |
| 126 | """Test that lambda functionality stays the same. The output produced |
| 127 | currently is, I suspect invalid because of the unencoded brackets in the |
| 128 | HTML, "<lambda>". |
| 129 | |
| 130 | The subtraction lambda method is tested. |
| 131 | """ |
| 132 | self.client.request("GET", "/") |
| 133 | response = self.client.getresponse() |
| 134 | |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 135 | self.assertIn((b'<dl><dt><a name="-<lambda>"><strong>' |
| 136 | b'<lambda></strong></a>(x, y)</dt></dl>'), |
| 137 | response.read()) |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 138 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 139 | @make_request_and_skipIf(sys.flags.optimize >= 2, |
| 140 | "Docstrings are omitted with -O2 and above") |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 141 | def test_autolinking(self): |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 142 | """Test that the server correctly automatically wraps references to |
| 143 | PEPS and RFCs with links, and that it linkifies text starting with |
| 144 | http or ftp protocol prefixes. |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 145 | |
| 146 | The documentation for the "add" method contains the test material. |
| 147 | """ |
| 148 | self.client.request("GET", "/") |
Christian Heimes | 2202f87 | 2008-02-06 14:31:34 +0000 | [diff] [blame] | 149 | response = self.client.getresponse().read() |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 150 | |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 151 | self.assertIn( |
| 152 | (b'<dl><dt><a name="-add"><strong>add</strong></a>(x, y)</dt><dd>' |
| 153 | b'<tt>Add two instances together. This ' |
| 154 | b'follows <a href="http://www.python.org/dev/peps/pep-0008/">' |
| 155 | b'PEP008</a>, but has nothing<br>\nto do ' |
| 156 | b'with <a href="http://www.rfc-editor.org/rfc/rfc1952.txt">' |
| 157 | b'RFC1952</a>. Case should matter: pEp008 ' |
| 158 | b'and rFC1952. Things<br>\nthat start ' |
| 159 | b'with http and ftp should be ' |
| 160 | b'auto-linked, too:<br>\n<a href="http://google.com">' |
| 161 | b'http://google.com</a>.</tt></dd></dl>'), response) |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 162 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 163 | @make_request_and_skipIf(sys.flags.optimize >= 2, |
| 164 | "Docstrings are omitted with -O2 and above") |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 165 | def test_system_methods(self): |
| 166 | """Test the precense of three consecutive system.* methods. |
| 167 | |
R. David Murray | 378c0cf | 2010-02-24 01:46:21 +0000 | [diff] [blame] | 168 | This also tests their use of parameter type recognition and the |
| 169 | systems related to that process. |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 170 | """ |
| 171 | self.client.request("GET", "/") |
Christian Heimes | a5535f2 | 2007-12-10 20:18:07 +0000 | [diff] [blame] | 172 | response = self.client.getresponse().read() |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 173 | |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 174 | self.assertIn( |
| 175 | (b'<dl><dt><a name="-system.methodHelp"><strong>system.methodHelp' |
| 176 | b'</strong></a>(method_name)</dt><dd><tt><a href="#-system.method' |
| 177 | b'Help">system.methodHelp</a>(\'add\') => "Adds ' |
| 178 | b'two integers together"<br>\n <br>\nReturns a' |
| 179 | b' string containing documentation for ' |
| 180 | b'the specified method.</tt></dd></dl>\n<dl><dt><a name' |
| 181 | b'="-system.methodSignature"><strong>system.methodSignature</strong>' |
| 182 | b'</a>(method_name)</dt><dd><tt><a href="#-system.methodSignature">' |
| 183 | b'system.methodSignature</a>(\'add\') => [double, ' |
| 184 | b'int, int]<br>\n <br>\nReturns a list ' |
| 185 | b'describing the signature of the method.' |
| 186 | b' In the<br>\nabove example, the add ' |
| 187 | b'method takes two integers as arguments' |
| 188 | b'<br>\nand returns a double result.<br>\n ' |
| 189 | b'<br>\nThis server does NOT support system' |
R David Murray | f22b62e | 2013-08-10 12:01:47 -0400 | [diff] [blame] | 190 | b'.methodSignature.</tt></dd></dl>'), response) |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 191 | |
| 192 | def test_autolink_dotted_methods(self): |
| 193 | """Test that selfdot values are made strong automatically in the |
| 194 | documentation.""" |
| 195 | self.client.request("GET", "/") |
| 196 | response = self.client.getresponse() |
| 197 | |
Ezio Melotti | b58e0bd | 2010-01-23 15:40:09 +0000 | [diff] [blame] | 198 | self.assertIn(b"""Try self.<strong>add</strong>, too.""", |
| 199 | response.read()) |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 200 | |
R David Murray | f22b62e | 2013-08-10 12:01:47 -0400 | [diff] [blame] | 201 | def test_annotations(self): |
| 202 | """ Test that annotations works as expected """ |
| 203 | self.client.request("GET", "/") |
| 204 | response = self.client.getresponse() |
Serhiy Storchaka | 3e60a9d | 2013-12-08 18:14:49 +0200 | [diff] [blame] | 205 | docstring = (b'' if sys.flags.optimize >= 2 else |
| 206 | b'<dd><tt>Use function annotations.</tt></dd>') |
R David Murray | f22b62e | 2013-08-10 12:01:47 -0400 | [diff] [blame] | 207 | self.assertIn( |
| 208 | (b'<dl><dt><a name="-annotation"><strong>annotation</strong></a>' |
Serhiy Storchaka | 3e60a9d | 2013-12-08 18:14:49 +0200 | [diff] [blame] | 209 | b'(x: int)</dt>' + docstring + b'</dl>\n' |
| 210 | b'<dl><dt><a name="-method_annotation"><strong>' |
R David Murray | f22b62e | 2013-08-10 12:01:47 -0400 | [diff] [blame] | 211 | b'method_annotation</strong></a>(x: bytes)</dt></dl>'), |
| 212 | response.read()) |
| 213 | |
| 214 | |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 215 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 216 | support.run_unittest(DocXMLRPCHTTPGETServer) |
Christian Heimes | 2f1019e | 2007-12-10 16:18:49 +0000 | [diff] [blame] | 217 | |
| 218 | if __name__ == '__main__': |
| 219 | test_main() |