Georg Brandl | e6daafb | 2007-12-09 22:39:12 +0000 | [diff] [blame] | 1 | from DocXMLRPCServer import DocXMLRPCServer |
| 2 | import httplib |
| 3 | from test import test_support |
| 4 | import threading |
| 5 | import time |
| 6 | import unittest |
| 7 | import xmlrpclib |
| 8 | |
| 9 | PORT = None |
| 10 | |
| 11 | def server(evt, numrequests): |
Hirokazu Yamamoto | b7df32e | 2008-10-03 16:18:42 +0000 | [diff] [blame] | 12 | serv = DocXMLRPCServer(("localhost", 0), logRequests=False) |
Georg Brandl | e6daafb | 2007-12-09 22:39:12 +0000 | [diff] [blame] | 13 | |
Hirokazu Yamamoto | b7df32e | 2008-10-03 16:18:42 +0000 | [diff] [blame] | 14 | try: |
Georg Brandl | e6daafb | 2007-12-09 22:39:12 +0000 | [diff] [blame] | 15 | global PORT |
| 16 | PORT = serv.socket.getsockname()[1] |
| 17 | |
| 18 | # Add some documentation |
| 19 | serv.set_server_title("DocXMLRPCServer Test Documentation") |
| 20 | serv.set_server_name("DocXMLRPCServer Test Docs") |
| 21 | serv.set_server_documentation( |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 22 | "This is an XML-RPC server's documentation, but the server " |
| 23 | "can be used by POSTing to /RPC2. Try self.add, too.") |
Georg Brandl | e6daafb | 2007-12-09 22:39:12 +0000 | [diff] [blame] | 24 | |
| 25 | # Create and register classes and functions |
| 26 | class TestClass(object): |
| 27 | def test_method(self, arg): |
| 28 | """Test method's docs. This method truly does very little.""" |
| 29 | self.arg = arg |
| 30 | |
| 31 | serv.register_introspection_functions() |
| 32 | serv.register_instance(TestClass()) |
| 33 | |
| 34 | def add(x, y): |
| 35 | """Add two instances together. This follows PEP008, but has nothing |
| 36 | to do with RFC1952. Case should matter: pEp008 and rFC1952. Things |
| 37 | that start with http and ftp should be auto-linked, too: |
| 38 | http://google.com. |
| 39 | """ |
| 40 | return x + y |
| 41 | |
| 42 | serv.register_function(add) |
| 43 | serv.register_function(lambda x, y: x-y) |
| 44 | |
| 45 | while numrequests > 0: |
| 46 | serv.handle_request() |
| 47 | numrequests -= 1 |
| 48 | except socket.timeout: |
| 49 | pass |
| 50 | finally: |
| 51 | serv.server_close() |
| 52 | PORT = None |
| 53 | evt.set() |
| 54 | |
| 55 | class DocXMLRPCHTTPGETServer(unittest.TestCase): |
| 56 | def setUp(self): |
Antoine Pitrou | 61d5f6f | 2009-10-30 17:33:28 +0000 | [diff] [blame] | 57 | self._threads = test_support.threading_setup() |
Georg Brandl | e6daafb | 2007-12-09 22:39:12 +0000 | [diff] [blame] | 58 | # Enable server feedback |
| 59 | DocXMLRPCServer._send_traceback_header = True |
| 60 | |
| 61 | self.evt = threading.Event() |
| 62 | threading.Thread(target=server, args=(self.evt, 1)).start() |
| 63 | |
| 64 | # wait for port to be assigned |
| 65 | n = 1000 |
| 66 | while n > 0 and PORT is None: |
| 67 | time.sleep(0.001) |
| 68 | n -= 1 |
| 69 | |
| 70 | self.client = httplib.HTTPConnection("localhost:%d" % PORT) |
| 71 | |
| 72 | def tearDown(self): |
| 73 | self.client.close() |
| 74 | |
| 75 | self.evt.wait() |
| 76 | |
| 77 | # Disable server feedback |
| 78 | DocXMLRPCServer._send_traceback_header = False |
Antoine Pitrou | 61d5f6f | 2009-10-30 17:33:28 +0000 | [diff] [blame] | 79 | test_support.threading_cleanup(*self._threads) |
Georg Brandl | e6daafb | 2007-12-09 22:39:12 +0000 | [diff] [blame] | 80 | |
| 81 | def test_valid_get_response(self): |
| 82 | self.client.request("GET", "/") |
| 83 | response = self.client.getresponse() |
| 84 | |
| 85 | self.assertEqual(response.status, 200) |
| 86 | self.assertEqual(response.getheader("Content-type"), "text/html") |
| 87 | |
| 88 | # Server throws an exception if we don't start to read the data |
| 89 | response.read() |
| 90 | |
| 91 | def test_invalid_get_response(self): |
| 92 | self.client.request("GET", "/spam") |
| 93 | response = self.client.getresponse() |
| 94 | |
| 95 | self.assertEqual(response.status, 404) |
| 96 | self.assertEqual(response.getheader("Content-type"), "text/plain") |
| 97 | |
| 98 | response.read() |
| 99 | |
| 100 | def test_lambda(self): |
| 101 | """Test that lambda functionality stays the same. The output produced |
| 102 | currently is, I suspect invalid because of the unencoded brackets in the |
| 103 | HTML, "<lambda>". |
| 104 | |
| 105 | The subtraction lambda method is tested. |
| 106 | """ |
| 107 | self.client.request("GET", "/") |
| 108 | response = self.client.getresponse() |
| 109 | |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 110 | self.assertIn('<dl><dt><a name="-<lambda>"><strong>' |
| 111 | '<lambda></strong></a>(x, y)</dt></dl>', |
| 112 | response.read()) |
Georg Brandl | e6daafb | 2007-12-09 22:39:12 +0000 | [diff] [blame] | 113 | |
| 114 | def test_autolinking(self): |
| 115 | """Test that the server correctly automatically wraps references to PEPS |
| 116 | and RFCs with links, and that it linkifies text starting with http or |
| 117 | ftp protocol prefixes. |
| 118 | |
| 119 | The documentation for the "add" method contains the test material. |
| 120 | """ |
| 121 | self.client.request("GET", "/") |
| 122 | response = self.client.getresponse() |
| 123 | |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 124 | self.assertIn( |
| 125 | ('<dl><dt><a name="-add"><strong>add</strong></a>(x, y)</dt><dd>' |
| 126 | '<tt>Add two instances together. This ' |
| 127 | 'follows <a href="http://www.python.org/dev/peps/pep-0008/">' |
| 128 | 'PEP008</a>, but has nothing<br>\nto do ' |
| 129 | 'with <a href="http://www.rfc-editor.org/rfc/rfc1952.txt">' |
| 130 | 'RFC1952</a>. Case should matter: pEp008 ' |
| 131 | 'and rFC1952. Things<br>\nthat start ' |
| 132 | 'with http and ftp should be ' |
| 133 | 'auto-linked, too:<br>\n<a href="http://google.com">' |
| 134 | 'http://google.com</a>.</tt></dd></dl>'), response.read()) |
Georg Brandl | e6daafb | 2007-12-09 22:39:12 +0000 | [diff] [blame] | 135 | |
| 136 | def test_system_methods(self): |
| 137 | """Test the precense of three consecutive system.* methods. |
| 138 | |
| 139 | This also tests their use of parameter type recognition and the systems |
| 140 | related to that process. |
| 141 | """ |
| 142 | self.client.request("GET", "/") |
| 143 | response = self.client.getresponse() |
| 144 | |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 145 | self.assertIn( |
| 146 | ('<dl><dt><a name="-system.listMethods"><strong>system.listMethods' |
| 147 | '</strong></a>()</dt><dd><tt><a href="#-system.listMethods">system' |
| 148 | '.listMethods</a>() => [\'add\', \'subtract\',' |
| 149 | ' \'multiple\']<br>\n <br>\nReturns a list' |
| 150 | ' of the methods supported by the' |
| 151 | ' server.</tt></dd></dl>\n <dl><dt><a name="-system.methodHelp">' |
| 152 | '<strong>system.methodHelp</strong></a>(method_name)</dt><dd><tt>' |
| 153 | '<a href="#-system.methodHelp">system.methodHelp</a>(\'add\') ' |
| 154 | '=> "Adds two integers together"<br>\n ' |
| 155 | '<br>\nReturns a string containing documentation' |
| 156 | ' for the specified method.</tt></dd></dl>\n ' |
| 157 | '<dl><dt><a name="-system.methodSignature"><strong>system.' |
| 158 | 'methodSignature</strong></a>(method_name)</dt><dd><tt><a href="#-' |
| 159 | 'system.methodSignature">system.methodSignature</a>(\'add\') ' |
| 160 | '=> [double, int, int]<br>\n <br>\nReturns' |
| 161 | ' a list describing the signature of' |
| 162 | ' the method. In the<br>\nabove example,' |
| 163 | ' the add method takes two integers' |
| 164 | ' as arguments<br>\nand returns a double' |
| 165 | ' result.<br>\n <br>\nThis server does ' |
| 166 | 'NOT support system.methodSignature.</tt></dd></dl>'), |
| 167 | response.read()) |
Georg Brandl | e6daafb | 2007-12-09 22:39:12 +0000 | [diff] [blame] | 168 | |
| 169 | def test_autolink_dotted_methods(self): |
| 170 | """Test that selfdot values are made strong automatically in the |
| 171 | documentation.""" |
| 172 | self.client.request("GET", "/") |
| 173 | response = self.client.getresponse() |
| 174 | |
Ezio Melotti | aa98058 | 2010-01-23 23:04:36 +0000 | [diff] [blame] | 175 | self.assertIn("""Try self.<strong>add</strong>, too.""", |
| 176 | response.read()) |
Georg Brandl | e6daafb | 2007-12-09 22:39:12 +0000 | [diff] [blame] | 177 | |
| 178 | def test_main(): |
| 179 | test_support.run_unittest(DocXMLRPCHTTPGETServer) |
| 180 | |
| 181 | if __name__ == '__main__': |
| 182 | test_main() |