blob: 60a69dd91fa07208f32dbf3faf128351b1d4fc58 [file] [log] [blame]
Georg Brandl38eceaa2008-05-26 11:14:17 +00001from xmlrpc.server import DocXMLRPCServer
Georg Brandl24420152008-05-26 16:32:26 +00002import http.client
R. David Murray378c0cf2010-02-24 01:46:21 +00003import sys
Benjamin Petersonee8712c2008-05-20 21:35:26 +00004from test import support
Victor Stinner45df8202010-04-28 22:31:17 +00005threading = support.import_module('threading')
Christian Heimes2f1019e2007-12-10 16:18:49 +00006import time
Georg Brandl89fad142010-03-14 10:23:39 +00007import socket
Christian Heimes2f1019e2007-12-10 16:18:49 +00008import unittest
Christian Heimes2f1019e2007-12-10 16:18:49 +00009
10PORT = None
11
R. David Murray378c0cf2010-02-24 01:46:21 +000012def make_request_and_skipIf(condition, reason):
13 # If we skip the test, we have to make a request because the
14 # the server created in setUp blocks expecting one to come in.
15 if not condition:
16 return lambda func: func
17 def decorator(func):
18 def make_request_and_skip(self):
19 self.client.request("GET", "/")
20 self.client.getresponse()
21 raise unittest.SkipTest(reason)
22 return make_request_and_skip
23 return decorator
24
25
Christian Heimes2f1019e2007-12-10 16:18:49 +000026def server(evt, numrequests):
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000027 serv = DocXMLRPCServer(("localhost", 0), logRequests=False)
Christian Heimes2f1019e2007-12-10 16:18:49 +000028
Benjamin Petersonf10a79a2008-10-11 00:49:57 +000029 try:
Christian Heimes2f1019e2007-12-10 16:18:49 +000030 global PORT
31 PORT = serv.socket.getsockname()[1]
32
33 # Add some documentation
34 serv.set_server_title("DocXMLRPCServer Test Documentation")
35 serv.set_server_name("DocXMLRPCServer Test Docs")
36 serv.set_server_documentation(
Ezio Melottib58e0bd2010-01-23 15:40:09 +000037 "This is an XML-RPC server's documentation, but the server "
38 "can be used by POSTing to /RPC2. Try self.add, too.")
Christian Heimes2f1019e2007-12-10 16:18:49 +000039
40 # Create and register classes and functions
41 class TestClass(object):
42 def test_method(self, arg):
43 """Test method's docs. This method truly does very little."""
44 self.arg = arg
45
46 serv.register_introspection_functions()
47 serv.register_instance(TestClass())
48
49 def add(x, y):
50 """Add two instances together. This follows PEP008, but has nothing
51 to do with RFC1952. Case should matter: pEp008 and rFC1952. Things
52 that start with http and ftp should be auto-linked, too:
53 http://google.com.
54 """
55 return x + y
56
57 serv.register_function(add)
58 serv.register_function(lambda x, y: x-y)
59
60 while numrequests > 0:
61 serv.handle_request()
62 numrequests -= 1
63 except socket.timeout:
64 pass
65 finally:
66 serv.server_close()
67 PORT = None
68 evt.set()
69
70class DocXMLRPCHTTPGETServer(unittest.TestCase):
71 def setUp(self):
Antoine Pitroud52656b2009-10-30 17:34:49 +000072 self._threads = support.threading_setup()
Christian Heimes2f1019e2007-12-10 16:18:49 +000073 # Enable server feedback
74 DocXMLRPCServer._send_traceback_header = True
75
76 self.evt = threading.Event()
77 threading.Thread(target=server, args=(self.evt, 1)).start()
78
79 # wait for port to be assigned
80 n = 1000
81 while n > 0 and PORT is None:
82 time.sleep(0.001)
83 n -= 1
84
Georg Brandl24420152008-05-26 16:32:26 +000085 self.client = http.client.HTTPConnection("localhost:%d" % PORT)
Christian Heimes2f1019e2007-12-10 16:18:49 +000086
87 def tearDown(self):
88 self.client.close()
89
90 self.evt.wait()
91
92 # Disable server feedback
93 DocXMLRPCServer._send_traceback_header = False
Antoine Pitroud52656b2009-10-30 17:34:49 +000094 support.threading_cleanup(*self._threads)
Christian Heimes2f1019e2007-12-10 16:18:49 +000095
96 def test_valid_get_response(self):
97 self.client.request("GET", "/")
98 response = self.client.getresponse()
99
100 self.assertEqual(response.status, 200)
101 self.assertEqual(response.getheader("Content-type"), "text/html")
102
103 # Server throws an exception if we don't start to read the data
104 response.read()
105
106 def test_invalid_get_response(self):
107 self.client.request("GET", "/spam")
108 response = self.client.getresponse()
109
110 self.assertEqual(response.status, 404)
111 self.assertEqual(response.getheader("Content-type"), "text/plain")
112
113 response.read()
114
115 def test_lambda(self):
116 """Test that lambda functionality stays the same. The output produced
117 currently is, I suspect invalid because of the unencoded brackets in the
118 HTML, "<lambda>".
119
120 The subtraction lambda method is tested.
121 """
122 self.client.request("GET", "/")
123 response = self.client.getresponse()
124
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000125 self.assertIn((b'<dl><dt><a name="-&lt;lambda&gt;"><strong>'
126 b'&lt;lambda&gt;</strong></a>(x, y)</dt></dl>'),
127 response.read())
Christian Heimes2f1019e2007-12-10 16:18:49 +0000128
R. David Murray378c0cf2010-02-24 01:46:21 +0000129 @make_request_and_skipIf(sys.flags.optimize >= 2,
130 "Docstrings are omitted with -O2 and above")
Christian Heimes2f1019e2007-12-10 16:18:49 +0000131 def test_autolinking(self):
R. David Murray378c0cf2010-02-24 01:46:21 +0000132 """Test that the server correctly automatically wraps references to
133 PEPS and RFCs with links, and that it linkifies text starting with
134 http or ftp protocol prefixes.
Christian Heimes2f1019e2007-12-10 16:18:49 +0000135
136 The documentation for the "add" method contains the test material.
137 """
138 self.client.request("GET", "/")
Christian Heimes2202f872008-02-06 14:31:34 +0000139 response = self.client.getresponse().read()
Christian Heimes2f1019e2007-12-10 16:18:49 +0000140
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000141 self.assertIn(
142 (b'<dl><dt><a name="-add"><strong>add</strong></a>(x, y)</dt><dd>'
143 b'<tt>Add&nbsp;two&nbsp;instances&nbsp;together.&nbsp;This&nbsp;'
144 b'follows&nbsp;<a href="http://www.python.org/dev/peps/pep-0008/">'
145 b'PEP008</a>,&nbsp;but&nbsp;has&nbsp;nothing<br>\nto&nbsp;do&nbsp;'
146 b'with&nbsp;<a href="http://www.rfc-editor.org/rfc/rfc1952.txt">'
147 b'RFC1952</a>.&nbsp;Case&nbsp;should&nbsp;matter:&nbsp;pEp008&nbsp;'
148 b'and&nbsp;rFC1952.&nbsp;&nbsp;Things<br>\nthat&nbsp;start&nbsp;'
149 b'with&nbsp;http&nbsp;and&nbsp;ftp&nbsp;should&nbsp;be&nbsp;'
150 b'auto-linked,&nbsp;too:<br>\n<a href="http://google.com">'
151 b'http://google.com</a>.</tt></dd></dl>'), response)
Christian Heimes2f1019e2007-12-10 16:18:49 +0000152
R. David Murray378c0cf2010-02-24 01:46:21 +0000153 @make_request_and_skipIf(sys.flags.optimize >= 2,
154 "Docstrings are omitted with -O2 and above")
Christian Heimes2f1019e2007-12-10 16:18:49 +0000155 def test_system_methods(self):
156 """Test the precense of three consecutive system.* methods.
157
R. David Murray378c0cf2010-02-24 01:46:21 +0000158 This also tests their use of parameter type recognition and the
159 systems related to that process.
Christian Heimes2f1019e2007-12-10 16:18:49 +0000160 """
161 self.client.request("GET", "/")
Christian Heimesa5535f22007-12-10 20:18:07 +0000162 response = self.client.getresponse().read()
Christian Heimes2f1019e2007-12-10 16:18:49 +0000163
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000164 self.assertIn(
165 (b'<dl><dt><a name="-system.methodHelp"><strong>system.methodHelp'
166 b'</strong></a>(method_name)</dt><dd><tt><a href="#-system.method'
167 b'Help">system.methodHelp</a>(\'add\')&nbsp;=&gt;&nbsp;"Adds&nbsp;'
168 b'two&nbsp;integers&nbsp;together"<br>\n&nbsp;<br>\nReturns&nbsp;a'
169 b'&nbsp;string&nbsp;containing&nbsp;documentation&nbsp;for&nbsp;'
170 b'the&nbsp;specified&nbsp;method.</tt></dd></dl>\n<dl><dt><a name'
171 b'="-system.methodSignature"><strong>system.methodSignature</strong>'
172 b'</a>(method_name)</dt><dd><tt><a href="#-system.methodSignature">'
173 b'system.methodSignature</a>(\'add\')&nbsp;=&gt;&nbsp;[double,&nbsp;'
174 b'int,&nbsp;int]<br>\n&nbsp;<br>\nReturns&nbsp;a&nbsp;list&nbsp;'
175 b'describing&nbsp;the&nbsp;signature&nbsp;of&nbsp;the&nbsp;method.'
176 b'&nbsp;In&nbsp;the<br>\nabove&nbsp;example,&nbsp;the&nbsp;add&nbsp;'
177 b'method&nbsp;takes&nbsp;two&nbsp;integers&nbsp;as&nbsp;arguments'
178 b'<br>\nand&nbsp;returns&nbsp;a&nbsp;double&nbsp;result.<br>\n&nbsp;'
179 b'<br>\nThis&nbsp;server&nbsp;does&nbsp;NOT&nbsp;support&nbsp;system'
180 b'.methodSignature.</tt></dd></dl>\n<dl><dt><a name="-test_method">'
181 b'<strong>test_method</strong></a>(arg)</dt><dd><tt>Test&nbsp;'
182 b'method\'s&nbsp;docs.&nbsp;This&nbsp;method&nbsp;truly&nbsp;does'
183 b'&nbsp;very&nbsp;little.</tt></dd></dl>'), response)
Christian Heimes2f1019e2007-12-10 16:18:49 +0000184
185 def test_autolink_dotted_methods(self):
186 """Test that selfdot values are made strong automatically in the
187 documentation."""
188 self.client.request("GET", "/")
189 response = self.client.getresponse()
190
Ezio Melottib58e0bd2010-01-23 15:40:09 +0000191 self.assertIn(b"""Try&nbsp;self.<strong>add</strong>,&nbsp;too.""",
192 response.read())
Christian Heimes2f1019e2007-12-10 16:18:49 +0000193
194def test_main():
Benjamin Petersonee8712c2008-05-20 21:35:26 +0000195 support.run_unittest(DocXMLRPCHTTPGETServer)
Christian Heimes2f1019e2007-12-10 16:18:49 +0000196
197if __name__ == '__main__':
198 test_main()