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