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