File: test_spartan.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 (77 lines) | stat: -rw-r--r-- 2,556 bytes parent folder | download | duplicates (2)
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
67
68
69
70
71
72
73
74
75
76
77
import io
import unittest

from pygopherd import testutil
from pygopherd.initialization import init_mimetypes
from pygopherd.protocols.spartan import SpartanProtocol


class TestSpartanProtocol(unittest.TestCase):
    def setUp(self):
        self.config = testutil.get_config()
        self.logfile = testutil.get_string_logger()
        self.rfile = io.BytesIO()
        self.wfile = io.BytesIO()
        self.handler = testutil.get_testing_handler(self.rfile, self.wfile, self.config)

    def get_protocol(self, request):
        return SpartanProtocol(
            request,
            self.handler.server,
            self.handler,
            self.rfile,
            self.wfile,
            self.config,
        )

    def test_spartan_handler(self):
        protocol = self.get_protocol("localhost / 0")
        self.assertTrue(protocol.canhandlerequest())

        protocol.handle()

        response = self.wfile.getvalue().decode(errors="surrogateescape")
        self.assertTrue(response.startswith("2 text/gemini\r\n"))

    @unittest.skipUnless(
        testutil.supports_non_utf8_filenames(),
        reason="Filesystem does not support non-utf8 filenames.",
    )
    def test_non_utf8(self):
        protocol = self.get_protocol("localhost / 0")
        protocol.handle()

        response = self.wfile.getvalue().decode(errors="surrogateescape")
        # The URL should be percent-sign encoded, the description should be
        # backslash replaced.
        self.assertIn("=> /%AE.txt \\xae\n", response)

    def test_not_found(self):
        protocol = self.get_protocol("localhost /dnsajd 0")
        protocol.handle()

        response = self.wfile.getvalue().decode()
        self.assertEqual("4 '/dnsajd' does not exist (no handler found)\r\n", response)

    def test_generate_query_link(self):
        protocol = self.get_protocol("localhost / 0")
        protocol.handle()

        response = self.wfile.getvalue().decode(errors="surrogateescape")
        self.assertIn("=: / Enter a query", response)

    def test_search_request(self):
        self.rfile = io.BytesIO(b"\xae")
        protocol = self.get_protocol("localhost / 2")
        protocol.handle()

        self.assertEqual(protocol.searchrequest, "\udcae")

    def test_detect_mime_type(self):
        init_mimetypes(self.config)

        protocol = self.get_protocol("localhost /testfile.gmi 0")
        protocol.handle()

        response = self.wfile.getvalue().decode(errors="surrogateescape")
        self.assertTrue(response.startswith("2 text/gemini\r\n"))