Debian Codegraph @

tests/handlers/test_url.py

raw symbols SCIP
1 import io
2 import unittest
3
4 from pygopherd import testutil
5 from pygopherd.handlers.base import VFS_Real
6 from pygopherd.handlers.file import FileHandler
7 from pygopherd.handlers.url import HTMLURLHandler, URLTypeRewriter
8
9
10 class TestHTMLURLHandler(unittest.TestCase):
11 def setUp(self):
12 self.config = testutil.get_config()
13 self.vfs = VFS_Real(self.config)
14 self.selector = "URL:http://gopher.quux.org/"
15 self.protocol = testutil.get_testing_protocol(self.selector, config=self.config)
16 self.stat_result = None
17
18 def test_url_rewriter_handler(self):
19 """
20 The URL rewriter should drop the "/0" at the beginning of the selector
21 and then pass it off to the appropriate handler.
22 """
23 handler = HTMLURLHandler(
24 self.selector, "", self.protocol, self.config, self.stat_result, self.vfs
25 )
26
27 self.assertTrue(handler.isrequestforme())
28
29 entry = handler.getentry()
30 self.assertEqual(entry.mimetype, "text/html")
31 self.assertEqual(entry.type, "h")
32
33 wfile = io.BytesIO()
34 handler.write(wfile)
35
36 out = wfile.getvalue()
37 self.assertIn(
38 b'<A HREF="http://gopher.quux.org/">http://gopher.quux.org/</A>', out
39 )
40
41 def test_handler_escape_urls(self):
42 """
43 URLs should be escaped in the generated HTML.
44 """
45 handler = HTMLURLHandler(
46 'URL:http://gopher.quux.org/"<script>',
47 "",
48 self.protocol,
49 self.config,
50 self.stat_result,
51 self.vfs,
52 )
53
54 entry = handler.getentry()
55 self.assertEqual(entry.mimetype, "text/html")
56 self.assertEqual(entry.type, "h")
57
58 wfile = io.BytesIO()
59 handler.write(wfile)
60
61 out = wfile.getvalue()
62 self.assertNotIn(b'http://gopher.quux.org/"<script>', out)
63 self.assertIn(b"http://gopher.quux.org/&quot;&lt;script&gt;", out)
64
65
66 class TestURLTypeRewriterHandler(unittest.TestCase):
67 def setUp(self):
68 self.config = testutil.get_config()
69 self.vfs = VFS_Real(self.config)
70 self.selector = "/0/README"
71 self.protocol = testutil.get_testing_protocol(self.selector, config=self.config)
72 self.stat_result = None
73
74 def test_url_rewriter_handler(self):
75 """
76 The URL rewriter should drop the "/0" at the beginning of the selector
77 and then pass it off to the appropriate handler.
78 """
79
80 handler = URLTypeRewriter(
81 self.selector, "", self.protocol, self.config, self.stat_result, self.vfs
82 )
83
84 self.assertTrue(handler.canhandlerequest())
85
86 new_handler = handler.gethandler()
87 self.assertIsInstance(new_handler, FileHandler)
88
89 self.assertEqual(new_handler.selector, "/README")
90 self.assertTrue(new_handler.canhandlerequest())