File: test_url.py

package info (click to toggle)
pygopherd 3.0.0~git20221126.02c65d60-3
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,924 kB
  • sloc: python: 6,515; makefile: 40; sh: 28
file content (66 lines) | stat: -rw-r--r-- 2,187 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
import io
import unittest

from pygopherd import testutil
from pygopherd.handlers.base import VFS_Real
from pygopherd.handlers.file import FileHandler
from pygopherd.handlers.url import HTMLURLHandler, URLTypeRewriter


class TestHTMLURLHandler(unittest.TestCase):
    def setUp(self):
        self.config = testutil.get_config()
        self.vfs = VFS_Real(self.config)
        self.selector = "URL:http://gopher.quux.org/"
        self.protocol = testutil.get_testing_protocol(self.selector, config=self.config)
        self.stat_result = None

    def test_url_rewriter_handler(self):
        """
        The URL rewriter should drop the "/0" at the beginning of the selector
        and then pass it off to the appropriate handler.
        """
        handler = HTMLURLHandler(
            self.selector, "", self.protocol, self.config, self.stat_result, self.vfs
        )

        self.assertTrue(handler.isrequestforme())

        entry = handler.getentry()
        self.assertEqual(entry.mimetype, "text/html")
        self.assertEqual(entry.type, "h")

        wfile = io.BytesIO()
        handler.write(wfile)

        out = wfile.getvalue()
        self.assertIn(
            b'<A HREF="http://gopher.quux.org/">http://gopher.quux.org/</A>', out
        )


class TestURLTypeRewriterHandler(unittest.TestCase):
    def setUp(self):
        self.config = testutil.get_config()
        self.vfs = VFS_Real(self.config)
        self.selector = "/0/README"
        self.protocol = testutil.get_testing_protocol(self.selector, config=self.config)
        self.stat_result = None

    def test_url_rewriter_handler(self):
        """
        The URL rewriter should drop the "/0" at the beginning of the selector
        and then pass it off to the appropriate handler.
        """

        handler = URLTypeRewriter(
            self.selector, "", self.protocol, self.config, self.stat_result, self.vfs
        )

        self.assertTrue(handler.canhandlerequest())

        new_handler = handler.gethandler()
        self.assertIsInstance(new_handler, FileHandler)

        self.assertEqual(new_handler.selector, "/README")
        self.assertTrue(new_handler.canhandlerequest())