home *** CD-ROM | disk | FTP | other *** search
/ Maximum CD 2011 June / maximum-cd-2011-06.iso / DiscContents / LibO_3.3.1_Win_x86_install_multi.exe / libreoffice1.cab / test_docxmlrpc.py < prev    next >
Encoding:
Python Source  |  2011-02-15  |  6.5 KB  |  153 lines

  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):
  12.     serv = DocXMLRPCServer(("localhost", 0), logRequests=False)
  13.  
  14.     try:
  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(
  22. """This is an XML-RPC server's documentation, but the server can be used by
  23. POSTing to /RPC2. Try self.add, too.""")
  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):
  57.         # Enable server feedback
  58.         DocXMLRPCServer._send_traceback_header = True
  59.  
  60.         self.evt = threading.Event()
  61.         threading.Thread(target=server, args=(self.evt, 1)).start()
  62.  
  63.         # wait for port to be assigned
  64.         n = 1000
  65.         while n > 0 and PORT is None:
  66.             time.sleep(0.001)
  67.             n -= 1
  68.  
  69.         self.client = httplib.HTTPConnection("localhost:%d" % PORT)
  70.  
  71.     def tearDown(self):
  72.         self.client.close()
  73.  
  74.         self.evt.wait()
  75.  
  76.         # Disable server feedback
  77.         DocXMLRPCServer._send_traceback_header = False
  78.  
  79.     def test_valid_get_response(self):
  80.         self.client.request("GET", "/")
  81.         response = self.client.getresponse()
  82.  
  83.         self.assertEqual(response.status, 200)
  84.         self.assertEqual(response.getheader("Content-type"), "text/html")
  85.  
  86.         # Server throws an exception if we don't start to read the data
  87.         response.read()
  88.  
  89.     def test_invalid_get_response(self):
  90.         self.client.request("GET", "/spam")
  91.         response = self.client.getresponse()
  92.  
  93.         self.assertEqual(response.status, 404)
  94.         self.assertEqual(response.getheader("Content-type"), "text/plain")
  95.  
  96.         response.read()
  97.  
  98.     def test_lambda(self):
  99.         """Test that lambda functionality stays the same.  The output produced
  100.         currently is, I suspect invalid because of the unencoded brackets in the
  101.         HTML, "<lambda>".
  102.  
  103.         The subtraction lambda method is tested.
  104.         """
  105.         self.client.request("GET", "/")
  106.         response = self.client.getresponse()
  107.  
  108.         self.assert_(
  109. """<dl><dt><a name="-<lambda>"><strong><lambda></strong></a>(x, y)</dt></dl>"""
  110.             in response.read())
  111.  
  112.     def test_autolinking(self):
  113.         """Test that the server correctly automatically wraps references to PEPS
  114.         and RFCs with links, and that it linkifies text starting with http or
  115.         ftp protocol prefixes.
  116.  
  117.         The documentation for the "add" method contains the test material.
  118.         """
  119.         self.client.request("GET", "/")
  120.         response = self.client.getresponse()
  121.  
  122.         self.assert_( # This is ugly ... how can it be made better?
  123. """<dl><dt><a name="-add"><strong>add</strong></a>(x, y)</dt><dd><tt>Add two instances together. This follows <a href="http://www.python.org/dev/peps/pep-0008/">PEP008</a>, but has nothing<br>\nto do with <a href="http://www.rfc-editor.org/rfc/rfc1952.txt">RFC1952</a>. Case should matter: pEp008 and rFC1952.  Things<br>\nthat start with http and ftp should be auto-linked, too:<br>\n<a href="http://google.com">http://google.com</a>.</tt></dd></dl>"""
  124.           in response.read())
  125.  
  126.     def test_system_methods(self):
  127.         """Test the precense of three consecutive system.* methods.
  128.  
  129.         This also tests their use of parameter type recognition and the systems
  130.         related to that process.
  131.         """
  132.         self.client.request("GET", "/")
  133.         response = self.client.getresponse()
  134.  
  135.         self.assert_(
  136. """<dl><dt><a name="-system.listMethods"><strong>system.listMethods</strong></a>()</dt><dd><tt><a href="#-system.listMethods">system.listMethods</a>() => [\'add\', \'subtract\', \'multiple\']<br>\n <br>\nReturns a list of the methods supported by the server.</tt></dd></dl>\n <dl><dt><a name="-system.methodHelp"><strong>system.methodHelp</strong></a>(method_name)</dt><dd><tt><a href="#-system.methodHelp">system.methodHelp</a>(\'add\') => "Adds two integers together"<br>\n <br>\nReturns a string containing documentation for the specified method.</tt></dd></dl>\n <dl><dt><a name="-system.methodSignature"><strong>system.methodSignature</strong></a>(method_name)</dt><dd><tt><a href="#-system.methodSignature">system.methodSignature</a>(\'add\') => [double, int, int]<br>\n <br>\nReturns a list describing the signature of the method. In the<br>\nabove example, the add method takes two integers as arguments<br>\nand returns a double result.<br>\n <br>\nThis server does NOT support system.methodSignature.</tt></dd></dl>"""
  137.             in response.read())
  138.  
  139.     def test_autolink_dotted_methods(self):
  140.         """Test that selfdot values are made strong automatically in the
  141.         documentation."""
  142.         self.client.request("GET", "/")
  143.         response = self.client.getresponse()
  144.  
  145.         self.assert_("""Try self.<strong>add</strong>, too.""" in
  146.             response.read())
  147.  
  148. def test_main():
  149.     test_support.run_unittest(DocXMLRPCHTTPGETServer)
  150.  
  151. if __name__ == '__main__':
  152.     test_main()
  153.