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