ยปCore Development>Code coverage>Lib/test/test_docxmlrpc.py

Python code coverage for Lib/test/test_docxmlrpc.py

#countcontent
1n/afrom xmlrpc.server import DocXMLRPCServer
2n/aimport http.client
3n/aimport sys
4n/afrom test import support
5n/athreading = support.import_module('threading')
6n/aimport unittest
7n/a
8n/adef make_request_and_skipIf(condition, reason):
9n/a # If we skip the test, we have to make a request because
10n/a # the server created in setUp blocks expecting one to come in.
11n/a if not condition:
12n/a return lambda func: func
13n/a def decorator(func):
14n/a def make_request_and_skip(self):
15n/a self.client.request("GET", "/")
16n/a self.client.getresponse()
17n/a raise unittest.SkipTest(reason)
18n/a return make_request_and_skip
19n/a return decorator
20n/a
21n/a
22n/adef make_server():
23n/a serv = DocXMLRPCServer(("localhost", 0), logRequests=False)
24n/a
25n/a try:
26n/a # Add some documentation
27n/a serv.set_server_title("DocXMLRPCServer Test Documentation")
28n/a serv.set_server_name("DocXMLRPCServer Test Docs")
29n/a serv.set_server_documentation(
30n/a "This is an XML-RPC server's documentation, but the server "
31n/a "can be used by POSTing to /RPC2. Try self.add, too.")
32n/a
33n/a # Create and register classes and functions
34n/a class TestClass(object):
35n/a def test_method(self, arg):
36n/a """Test method's docs. This method truly does very little."""
37n/a self.arg = arg
38n/a
39n/a serv.register_introspection_functions()
40n/a serv.register_instance(TestClass())
41n/a
42n/a def add(x, y):
43n/a """Add two instances together. This follows PEP008, but has nothing
44n/a to do with RFC1952. Case should matter: pEp008 and rFC1952. Things
45n/a that start with http and ftp should be auto-linked, too:
46n/a http://google.com.
47n/a """
48n/a return x + y
49n/a
50n/a def annotation(x: int):
51n/a """ Use function annotations. """
52n/a return x
53n/a
54n/a class ClassWithAnnotation:
55n/a def method_annotation(self, x: bytes):
56n/a return x.decode()
57n/a
58n/a serv.register_function(add)
59n/a serv.register_function(lambda x, y: x-y)
60n/a serv.register_function(annotation)
61n/a serv.register_instance(ClassWithAnnotation())
62n/a return serv
63n/a except:
64n/a serv.server_close()
65n/a raise
66n/a
67n/aclass DocXMLRPCHTTPGETServer(unittest.TestCase):
68n/a def setUp(self):
69n/a # Enable server feedback
70n/a DocXMLRPCServer._send_traceback_header = True
71n/a
72n/a self.serv = make_server()
73n/a self.thread = threading.Thread(target=self.serv.serve_forever)
74n/a self.thread.start()
75n/a
76n/a PORT = self.serv.server_address[1]
77n/a self.client = http.client.HTTPConnection("localhost:%d" % PORT)
78n/a
79n/a def tearDown(self):
80n/a self.client.close()
81n/a
82n/a # Disable server feedback
83n/a DocXMLRPCServer._send_traceback_header = False
84n/a self.serv.shutdown()
85n/a self.thread.join()
86n/a self.serv.server_close()
87n/a
88n/a def test_valid_get_response(self):
89n/a self.client.request("GET", "/")
90n/a response = self.client.getresponse()
91n/a
92n/a self.assertEqual(response.status, 200)
93n/a self.assertEqual(response.getheader("Content-type"), "text/html")
94n/a
95n/a # Server raises an exception if we don't start to read the data
96n/a response.read()
97n/a
98n/a def test_invalid_get_response(self):
99n/a self.client.request("GET", "/spam")
100n/a response = self.client.getresponse()
101n/a
102n/a self.assertEqual(response.status, 404)
103n/a self.assertEqual(response.getheader("Content-type"), "text/plain")
104n/a
105n/a response.read()
106n/a
107n/a def test_lambda(self):
108n/a """Test that lambda functionality stays the same. The output produced
109n/a currently is, I suspect invalid because of the unencoded brackets in the
110n/a HTML, "<lambda>".
111n/a
112n/a The subtraction lambda method is tested.
113n/a """
114n/a self.client.request("GET", "/")
115n/a response = self.client.getresponse()
116n/a
117n/a self.assertIn((b'<dl><dt><a name="-&lt;lambda&gt;"><strong>'
118n/a b'&lt;lambda&gt;</strong></a>(x, y)</dt></dl>'),
119n/a response.read())
120n/a
121n/a @make_request_and_skipIf(sys.flags.optimize >= 2,
122n/a "Docstrings are omitted with -O2 and above")
123n/a def test_autolinking(self):
124n/a """Test that the server correctly automatically wraps references to
125n/a PEPS and RFCs with links, and that it linkifies text starting with
126n/a http or ftp protocol prefixes.
127n/a
128n/a The documentation for the "add" method contains the test material.
129n/a """
130n/a self.client.request("GET", "/")
131n/a response = self.client.getresponse().read()
132n/a
133n/a self.assertIn(
134n/a (b'<dl><dt><a name="-add"><strong>add</strong></a>(x, y)</dt><dd>'
135n/a b'<tt>Add&nbsp;two&nbsp;instances&nbsp;together.&nbsp;This&nbsp;'
136n/a b'follows&nbsp;<a href="http://www.python.org/dev/peps/pep-0008/">'
137n/a b'PEP008</a>,&nbsp;but&nbsp;has&nbsp;nothing<br>\nto&nbsp;do&nbsp;'
138n/a b'with&nbsp;<a href="http://www.rfc-editor.org/rfc/rfc1952.txt">'
139n/a b'RFC1952</a>.&nbsp;Case&nbsp;should&nbsp;matter:&nbsp;pEp008&nbsp;'
140n/a b'and&nbsp;rFC1952.&nbsp;&nbsp;Things<br>\nthat&nbsp;start&nbsp;'
141n/a b'with&nbsp;http&nbsp;and&nbsp;ftp&nbsp;should&nbsp;be&nbsp;'
142n/a b'auto-linked,&nbsp;too:<br>\n<a href="http://google.com">'
143n/a b'http://google.com</a>.</tt></dd></dl>'), response)
144n/a
145n/a @make_request_and_skipIf(sys.flags.optimize >= 2,
146n/a "Docstrings are omitted with -O2 and above")
147n/a def test_system_methods(self):
148n/a """Test the presence of three consecutive system.* methods.
149n/a
150n/a This also tests their use of parameter type recognition and the
151n/a systems related to that process.
152n/a """
153n/a self.client.request("GET", "/")
154n/a response = self.client.getresponse().read()
155n/a
156n/a self.assertIn(
157n/a (b'<dl><dt><a name="-system.methodHelp"><strong>system.methodHelp'
158n/a b'</strong></a>(method_name)</dt><dd><tt><a href="#-system.method'
159n/a b'Help">system.methodHelp</a>(\'add\')&nbsp;=&gt;&nbsp;"Adds&nbsp;'
160n/a b'two&nbsp;integers&nbsp;together"<br>\n&nbsp;<br>\nReturns&nbsp;a'
161n/a b'&nbsp;string&nbsp;containing&nbsp;documentation&nbsp;for&nbsp;'
162n/a b'the&nbsp;specified&nbsp;method.</tt></dd></dl>\n<dl><dt><a name'
163n/a b'="-system.methodSignature"><strong>system.methodSignature</strong>'
164n/a b'</a>(method_name)</dt><dd><tt><a href="#-system.methodSignature">'
165n/a b'system.methodSignature</a>(\'add\')&nbsp;=&gt;&nbsp;[double,&nbsp;'
166n/a b'int,&nbsp;int]<br>\n&nbsp;<br>\nReturns&nbsp;a&nbsp;list&nbsp;'
167n/a b'describing&nbsp;the&nbsp;signature&nbsp;of&nbsp;the&nbsp;method.'
168n/a b'&nbsp;In&nbsp;the<br>\nabove&nbsp;example,&nbsp;the&nbsp;add&nbsp;'
169n/a b'method&nbsp;takes&nbsp;two&nbsp;integers&nbsp;as&nbsp;arguments'
170n/a b'<br>\nand&nbsp;returns&nbsp;a&nbsp;double&nbsp;result.<br>\n&nbsp;'
171n/a b'<br>\nThis&nbsp;server&nbsp;does&nbsp;NOT&nbsp;support&nbsp;system'
172n/a b'.methodSignature.</tt></dd></dl>'), response)
173n/a
174n/a def test_autolink_dotted_methods(self):
175n/a """Test that selfdot values are made strong automatically in the
176n/a documentation."""
177n/a self.client.request("GET", "/")
178n/a response = self.client.getresponse()
179n/a
180n/a self.assertIn(b"""Try&nbsp;self.<strong>add</strong>,&nbsp;too.""",
181n/a response.read())
182n/a
183n/a def test_annotations(self):
184n/a """ Test that annotations works as expected """
185n/a self.client.request("GET", "/")
186n/a response = self.client.getresponse()
187n/a docstring = (b'' if sys.flags.optimize >= 2 else
188n/a b'<dd><tt>Use&nbsp;function&nbsp;annotations.</tt></dd>')
189n/a self.assertIn(
190n/a (b'<dl><dt><a name="-annotation"><strong>annotation</strong></a>'
191n/a b'(x: int)</dt>' + docstring + b'</dl>\n'
192n/a b'<dl><dt><a name="-method_annotation"><strong>'
193n/a b'method_annotation</strong></a>(x: bytes)</dt></dl>'),
194n/a response.read())
195n/a
196n/a
197n/aif __name__ == '__main__':
198n/a unittest.main()