Debian Codegraph @

cpython27/Lib/test/test_docxmlrpc.py

raw symbols SCIP
1 from DocXMLRPCServer import DocXMLRPCServer
2 import httplib
3 import re
4 import sys
5 from test import test_support
6 threading = test_support.import_module('threading')
7 import unittest
8
9 def make_request_and_skipIf(condition, reason):
10 # If we skip the test, we have to make a request because
11 # the server created in setUp blocks expecting one to come in.
12 if not condition:
13 return lambda func: func
14 def decorator(func):
15 def make_request_and_skip(self):
16 self.client.request("GET", "/")
17 self.client.getresponse()
18 raise unittest.SkipTest(reason)
19 return make_request_and_skip
20 return decorator
21
22
23 def make_server():
24 serv = DocXMLRPCServer(("localhost", 0), logRequests=False)
25
26 try:
27 # Add some documentation
28 serv.set_server_title("DocXMLRPCServer Test Documentation")
29 serv.set_server_name("DocXMLRPCServer Test Docs")
30 serv.set_server_documentation(
31 "This is an XML-RPC server's documentation, but the server "
32 "can be used by POSTing to /RPC2. Try self.add, too.")
33
34 # Create and register classes and functions
35 class TestClass(object):
36 def test_method(self, arg):
37 """Test method's docs. This method truly does very little."""
38 self.arg = arg
39
40 serv.register_introspection_functions()
41 serv.register_instance(TestClass())
42
43 def add(x, y):
44 """Add two instances together. This follows PEP008, but has nothing
45 to do with RFC1952. Case should matter: pEp008 and rFC1952. Things
46 that start with http and ftp should be auto-linked, too:
47 http://google.com.
48 """
49 return x + y
50
51 serv.register_function(add)
52 serv.register_function(lambda x, y: x-y)
53 return serv
54 except:
55 serv.server_close()
56 raise
57
58 class DocXMLRPCHTTPGETServer(unittest.TestCase):
59 def setUp(self):
60 # Enable server feedback
61 DocXMLRPCServer._send_traceback_header = True
62
63 self.serv = make_server()
64 self.thread = threading.Thread(target=self.serv.serve_forever)
65 self.thread.start()
66
67 PORT = self.serv.server_address[1]
68 self.client = httplib.HTTPConnection("localhost:%d" % PORT)
69
70 def tearDown(self):
71 self.client.close()
72
73 # Disable server feedback
74 DocXMLRPCServer._send_traceback_header = False
75 self.serv.shutdown()
76 self.thread.join()
77 self.serv.server_close()
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 raises 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.assertIn('<dl><dt><a name="-&lt;lambda&gt;"><strong>'
109 '&lt;lambda&gt;</strong></a>(x, y)</dt></dl>',
110 response.read())
111
112 @make_request_and_skipIf(sys.flags.optimize >= 2,
113 "Docstrings are omitted with -O2 and above")
114 def test_autolinking(self):
115 """Test that the server correctly automatically wraps references to
116 PEPS and RFCs with links, and that it linkifies text starting with
117 http or ftp protocol prefixes.
118
119 The documentation for the "add" method contains the test material.
120 """
121 self.client.request("GET", "/")
122 response = self.client.getresponse()
123
124 self.assertIn(
125 ('<dl><dt><a name="-add"><strong>add</strong></a>(x, y)</dt><dd>'
126 '<tt>Add&nbsp;two&nbsp;instances&nbsp;together.&nbsp;This&nbsp;'
127 'follows&nbsp;<a href="http://www.python.org/dev/peps/pep-0008/">'
128 'PEP008</a>,&nbsp;but&nbsp;has&nbsp;nothing<br>\nto&nbsp;do&nbsp;'
129 'with&nbsp;<a href="http://www.rfc-editor.org/rfc/rfc1952.txt">'
130 'RFC1952</a>.&nbsp;Case&nbsp;should&nbsp;matter:&nbsp;pEp008&nbsp;'
131 'and&nbsp;rFC1952.&nbsp;&nbsp;Things<br>\nthat&nbsp;start&nbsp;'
132 'with&nbsp;http&nbsp;and&nbsp;ftp&nbsp;should&nbsp;be&nbsp;'
133 'auto-linked,&nbsp;too:<br>\n<a href="http://google.com">'
134 'http://google.com</a>.</tt></dd></dl>'), response.read())
135
136 @make_request_and_skipIf(sys.flags.optimize >= 2,
137 "Docstrings are omitted with -O2 and above")
138 def test_system_methods(self):
139 """Test the presence of three consecutive system.* methods.
140
141 This also tests their use of parameter type recognition and the
142 systems related to that process.
143 """
144 self.client.request("GET", "/")
145 response = self.client.getresponse()
146
147 self.assertIn(
148 ('<dl><dt><a name="-system.listMethods"><strong>system.listMethods'
149 '</strong></a>()</dt><dd><tt><a href="#-system.listMethods">system'
150 '.listMethods</a>()&nbsp;=&gt;&nbsp;[\'add\',&nbsp;\'subtract\','
151 '&nbsp;\'multiple\']<br>\n&nbsp;<br>\nReturns&nbsp;a&nbsp;list'
152 '&nbsp;of&nbsp;the&nbsp;methods&nbsp;supported&nbsp;by&nbsp;the'
153 '&nbsp;server.</tt></dd></dl>\n <dl><dt><a name="-system.methodHelp">'
154 '<strong>system.methodHelp</strong></a>(method_name)</dt><dd><tt>'
155 '<a href="#-system.methodHelp">system.methodHelp</a>(\'add\')&nbsp;'
156 '=&gt;&nbsp;"Adds&nbsp;two&nbsp;integers&nbsp;together"<br>\n&nbsp;'
157 '<br>\nReturns&nbsp;a&nbsp;string&nbsp;containing&nbsp;documentation'
158 '&nbsp;for&nbsp;the&nbsp;specified&nbsp;method.</tt></dd></dl>\n '
159 '<dl><dt><a name="-system.methodSignature"><strong>system.'
160 'methodSignature</strong></a>(method_name)</dt><dd><tt><a href="#-'
161 'system.methodSignature">system.methodSignature</a>(\'add\')&nbsp;'
162 '=&gt;&nbsp;[double,&nbsp;int,&nbsp;int]<br>\n&nbsp;<br>\nReturns'
163 '&nbsp;a&nbsp;list&nbsp;describing&nbsp;the&nbsp;signature&nbsp;of'
164 '&nbsp;the&nbsp;method.&nbsp;In&nbsp;the<br>\nabove&nbsp;example,'
165 '&nbsp;the&nbsp;add&nbsp;method&nbsp;takes&nbsp;two&nbsp;integers'
166 '&nbsp;as&nbsp;arguments<br>\nand&nbsp;returns&nbsp;a&nbsp;double'
167 '&nbsp;result.<br>\n&nbsp;<br>\nThis&nbsp;server&nbsp;does&nbsp;'
168 'NOT&nbsp;support&nbsp;system.methodSignature.</tt></dd></dl>'),
169 response.read())
170
171 def test_autolink_dotted_methods(self):
172 """Test that selfdot values are made strong automatically in the
173 documentation."""
174 self.client.request("GET", "/")
175 response = self.client.getresponse()
176
177 self.assertIn("""Try&nbsp;self.<strong>add</strong>,&nbsp;too.""",
178 response.read())
179
180 def test_server_title_escape(self):
181 """Test that the server title and documentation
182 are escaped for HTML.
183 """
184 self.serv.set_server_title('test_title<script>')
185 self.serv.set_server_documentation('test_documentation<script>')
186 self.assertEqual('test_title<script>', self.serv.server_title)
187 self.assertEqual('test_documentation<script>',
188 self.serv.server_documentation)
189
190 generated = self.serv.generate_html_documentation()
191 title = re.search(r'<title>(.+?)</title>', generated).group()
192 documentation = re.search(r'<p><tt>(.+?)</tt></p>', generated).group()
193 self.assertEqual('<title>Python: test_title&lt;script&gt;</title>',
194 title)
195 self.assertEqual('<p><tt>test_documentation&lt;script&gt;</tt></p>',
196 documentation)
197
198
199 def test_main():
200 test_support.run_unittest(DocXMLRPCHTTPGETServer)
201
202 if __name__ == '__main__':
203 test_main()