from DocXMLRPCServer import DocXMLRPCServer import httplib import sys from test import test_support threading = test_support.import_module('threading') import unittest def make_request_and_skipIf(condition, reason): # If we skip the test, we have to make a request because # the server created in setUp blocks expecting one to come in. if not condition: return lambda func: func def decorator(func): def make_request_and_skip(self): self.client.request("GET", "/") self.client.getresponse() raise unittest.SkipTest(reason) return make_request_and_skip return decorator def make_server(): serv = DocXMLRPCServer(("localhost", 0), logRequests=False) try: # Add some documentation serv.set_server_title("DocXMLRPCServer Test Documentation") serv.set_server_name("DocXMLRPCServer Test Docs") serv.set_server_documentation( "This is an XML-RPC server's documentation, but the server " "can be used by POSTing to /RPC2. Try self.add, too.") # Create and register classes and functions class TestClass(object): def test_method(self, arg): """Test method's docs. This method truly does very little.""" self.arg = arg serv.register_introspection_functions() serv.register_instance(TestClass()) def add(x, y): """Add two instances together. This follows PEP008, but has nothing to do with RFC1952. Case should matter: pEp008 and rFC1952. Things that start with http and ftp should be auto-linked, too: http://google.com. """ return x + y serv.register_function(add) serv.register_function(lambda x, y: x-y) return serv except: serv.server_close() raise class DocXMLRPCHTTPGETServer(unittest.TestCase): def setUp(self): # Enable server feedback DocXMLRPCServer._send_traceback_header = True self.serv = make_server() self.thread = threading.Thread(target=self.serv.serve_forever) self.thread.start() PORT = self.serv.server_address[1] self.client = httplib.HTTPConnection("localhost:%d" % PORT) def tearDown(self): self.client.close() # Disable server feedback DocXMLRPCServer._send_traceback_header = False self.serv.shutdown() self.thread.join() self.serv.server_close() def test_valid_get_response(self): self.client.request("GET", "/") response = self.client.getresponse() self.assertEqual(response.status, 200) self.assertEqual(response.getheader("Content-type"), "text/html") # Server raises an exception if we don't start to read the data response.read() def test_invalid_get_response(self): self.client.request("GET", "/spam") response = self.client.getresponse() self.assertEqual(response.status, 404) self.assertEqual(response.getheader("Content-type"), "text/plain") response.read() def test_lambda(self): """Test that lambda functionality stays the same. The output produced currently is, I suspect invalid because of the unencoded brackets in the HTML, "". The subtraction lambda method is tested. """ self.client.request("GET", "/") response = self.client.getresponse() self.assertIn('
' '<lambda>(x, y)
', response.read()) @make_request_and_skipIf(sys.flags.optimize >= 2, "Docstrings are omitted with -O2 and above") def test_autolinking(self): """Test that the server correctly automatically wraps references to PEPS and RFCs with links, and that it linkifies text starting with http or ftp protocol prefixes. The documentation for the "add" method contains the test material. """ self.client.request("GET", "/") response = self.client.getresponse() self.assertIn( ('
add(x, y)
' 'Add two instances together. This ' 'follows ' 'PEP008, but has nothing
\nto do ' 'with ' 'RFC1952. Case should matter: pEp008 ' 'and rFC1952.  Things
\nthat start ' 'with http and ftp should be ' 'auto-linked, too:
\n' 'http://google.com.
'), response.read()) @make_request_and_skipIf(sys.flags.optimize >= 2, "Docstrings are omitted with -O2 and above") def test_system_methods(self): """Test the presence of three consecutive system.* methods. This also tests their use of parameter type recognition and the systems related to that process. """ self.client.request("GET", "/") response = self.client.getresponse() self.assertIn( ('
system.listMethods' '()
system' '.listMethods() => [\'add\', \'subtract\',' ' \'multiple\']
\n 
\nReturns a list' ' of the methods supported by the' ' server.
\n
' 'system.methodHelp(method_name)
' 'system.methodHelp(\'add\') ' '=> "Adds two integers together"
\n ' '
\nReturns a string containing documentation' ' for the specified method.
\n ' '
system.' 'methodSignature(method_name)
system.methodSignature(\'add\') ' '=> [double, int, int]
\n 
\nReturns' ' a list describing the signature of' ' the method. In the
\nabove example,' ' the add method takes two integers' ' as arguments
\nand returns a double' ' result.
\n 
\nThis server does ' 'NOT support system.methodSignature.
'), response.read()) def test_autolink_dotted_methods(self): """Test that selfdot values are made strong automatically in the documentation.""" self.client.request("GET", "/") response = self.client.getresponse() self.assertIn("""Try self.add, too.""", response.read()) def test_main(): test_support.run_unittest(DocXMLRPCHTTPGETServer) if __name__ == '__main__': test_main()