1 from DocXMLRPCServer
import DocXMLRPCServer
3 from test
import test_support
11 def server(evt
, numrequests
):
12 serv
= DocXMLRPCServer(("localhost", 0), logRequests
=False)
16 PORT
= serv
.socket
.getsockname()[1]
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.""")
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."""
31 serv
.register_introspection_functions()
32 serv
.register_instance(TestClass())
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:
42 serv
.register_function(add
)
43 serv
.register_function(lambda x
, y
: x
-y
)
45 while numrequests
> 0:
48 except socket
.timeout
:
55 class DocXMLRPCHTTPGETServer(unittest
.TestCase
):
57 self
._threads
= test_support
.threading_setup()
58 # Enable server feedback
59 DocXMLRPCServer
._send
_traceback
_header
= True
61 self
.evt
= threading
.Event()
62 threading
.Thread(target
=server
, args
=(self
.evt
, 1)).start()
64 # wait for port to be assigned
66 while n
> 0 and PORT
is None:
70 self
.client
= httplib
.HTTPConnection("localhost:%d" % PORT
)
77 # Disable server feedback
78 DocXMLRPCServer
._send
_traceback
_header
= False
79 test_support
.threading_cleanup(*self
._threads
)
81 def test_valid_get_response(self
):
82 self
.client
.request("GET", "/")
83 response
= self
.client
.getresponse()
85 self
.assertEqual(response
.status
, 200)
86 self
.assertEqual(response
.getheader("Content-type"), "text/html")
88 # Server throws an exception if we don't start to read the data
91 def test_invalid_get_response(self
):
92 self
.client
.request("GET", "/spam")
93 response
= self
.client
.getresponse()
95 self
.assertEqual(response
.status
, 404)
96 self
.assertEqual(response
.getheader("Content-type"), "text/plain")
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
105 The subtraction lambda method is tested.
107 self
.client
.request("GET", "/")
108 response
= self
.client
.getresponse()
111 """<dl><dt><a name="-<lambda>"><strong><lambda></strong></a>(x, y)</dt></dl>"""
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.
119 The documentation for the "add" method contains the test material.
121 self
.client
.request("GET", "/")
122 response
= self
.client
.getresponse()
124 self
.assertTrue( # This is ugly ... how can it be made better?
125 """<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>"""
128 def test_system_methods(self
):
129 """Test the precense of three consecutive system.* methods.
131 This also tests their use of parameter type recognition and the systems
132 related to that process.
134 self
.client
.request("GET", "/")
135 response
= self
.client
.getresponse()
138 """<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>"""
141 def test_autolink_dotted_methods(self
):
142 """Test that selfdot values are made strong automatically in the
144 self
.client
.request("GET", "/")
145 response
= self
.client
.getresponse()
147 self
.assertTrue("""Try self.<strong>add</strong>, too.""" in
151 test_support
.run_unittest(DocXMLRPCHTTPGETServer
)
153 if __name__
== '__main__':