Index: Lib/test/test_pydoc.py
===================================================================
--- Lib/test/test_pydoc.py (revision 86635)
+++ Lib/test/test_pydoc.py (working copy)
@@ -9,6 +9,7 @@
import unittest
import test.support
import xml.etree
+import time
from contextlib import contextmanager
from test.support import (
TESTFN, forget, rmtree, EnvironmentVarGuard, reap_children)
@@ -232,7 +233,12 @@
tofile='got')
print('\n' + ''.join(diffs))
+def get_html_title(text):
+ _, _, text = text.rpartition("
")
+ title, _, _ = text.rpartition("")
+ return title
+
class PyDocDocTest(unittest.TestCase):
@unittest.skipIf(sys.flags.optimize >= 2,
@@ -352,10 +358,74 @@
self.assertEqual(pydoc.describe(c), 'C')
expected = 'C in module %s object' % __name__
self.assertIn(expected, pydoc.render_doc(c))
+
+class PyDocServerTest(unittest.TestCase):
+ """ Test pydoc._start_server(). """
+
+ def test_server(self):
+ # Minimal test that starts the server, then stops it.
+ def my_url_handler(url, content_type):
+ text = 'the URL sent was: (%s, %s)' % (url, content_type)
+ return text
+ serverthread = pydoc._start_server(my_url_handler, port=0)
+ starttime = time.time()
+ timeout = 1 #seconds
+ while serverthread.serving:
+ time.sleep(.01)
+ if serverthread.serving and time.time() - starttime > timeout:
+ serverthread.stop()
+ break
+ self.assertEqual(serverthread.error, None)
+
+class PyDocUrlHandlerTest(unittest.TestCase):
+ """ Test pydoc._url_handler(). """
+
+ def test_content_type_err(self):
+ err = 'Error: unknown content type '
+ f = pydoc._url_handler
+ result = f("", "")
+ self.assertEqual(result, err + "")
+ result = f("", "foobar")
+ self.assertEqual(result, err + "foobar")
+
+ def test_url_requests(self):
+ # Test for the correct title in the html pages returned.
+ # This tests the different parts of the URL handler without
+ # getting too picky about the exact html.
+ requests = [
+ ("", "Python: Index of Modules"),
+ ("get?key=", "Python: Index of Modules"),
+ ("index", "Python: Index of Modules"),
+ ("topics", "Python: Topics"),
+ ("keywords", "Python: Keywords"),
+ ("pydoc", "Python: module pydoc"),
+ ("get?key=pydoc", "Python: module pydoc"),
+ ("search?key=pydoc", "Python: Search Results"),
+ ("def", "Python: KEYWORD def"),
+ ("STRINGS", "Python: TOPIC STRINGS"),
+ ("foobar", "Python: Error"),
+ ("getfile?key=foobar", "Python: Read Error"),
+ ]
+
+ for url, title in requests:
+ text = pydoc._url_handler(url, "text/html")
+ result = get_html_title(text)
+ self.assertEqual(result, title)
+
+ import string
+ path = string.__file__
+ title = "Python: getfile /" + path
+ url = "getfile?key=" + path
+ text = pydoc._url_handler(url, "text/html")
+ result = get_html_title(text)
+ self.assertEqual(result, title)
+
+
def test_main():
- test.support.run_unittest(PyDocDocTest, TestDescriptions)
+ test.support.run_unittest( PyDocDocTest, TestDescriptions,
+ PyDocServerTest, PyDocUrlHandlerTest )
if __name__ == "__main__":
test_main()