File: test_base.py

package info (click to toggle)
pygopherd 3.0.1-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,928 kB
  • sloc: python: 6,534; makefile: 40; sh: 28
file content (139 lines) | stat: -rw-r--r-- 4,814 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
import unittest
from io import BytesIO

import pygopherd.handlers.file
from pygopherd import testutil
from pygopherd.gopherentry import GopherEntry
from pygopherd.protocols.base import BaseGopherProtocol


class BaseProtocolTestCase(unittest.TestCase):
    def setUp(self):
        self.config = testutil.get_config()
        self.rfile = BytesIO(b"/testfile.txt\n")
        self.wfile = BytesIO()
        self.logfile = testutil.get_string_logger()
        self.logstr = "10.77.77.77 [BaseGopherProtocol/FileHandler]: /testfile.txt\n"
        self.handler = testutil.get_testing_handler(self.rfile, self.wfile, self.config)
        self.server = self.handler.server
        self.proto = BaseGopherProtocol(
            "/testfile.txt\n",
            self.server,
            self.handler,
            self.rfile,
            self.wfile,
            self.config,
        )

    def testInitBasic(self):
        proto = BaseGopherProtocol(
            "/foo.txt\n", self.server, self.handler, self.rfile, self.wfile, self.config
        )
        assert proto.rfile == self.rfile
        assert proto.wfile == self.wfile
        assert proto.config == self.config
        assert proto.requesthandler == self.handler
        assert proto.requestlist == ["/foo.txt"]
        assert proto.searchrequest is None
        assert proto.handler is None
        assert proto.selector == "/foo.txt"

    def testInitEmpty(self):
        proto = BaseGopherProtocol(
            "\n", self.server, self.handler, self.rfile, self.wfile, self.config
        )
        # It should be rewritten to /
        assert proto.selector == "/"
        assert proto.requestlist == [""]

    def testInitMissingLeadingSlash(self):
        proto = BaseGopherProtocol(
            "foo.txt\n", self.server, self.handler, self.rfile, self.wfile, self.config
        )
        assert proto.selector == "/foo.txt"
        assert proto.requestlist == ["foo.txt"]

    def testInitAddedTrailingSlash(self):
        proto = BaseGopherProtocol(
            "/dir/\n", self.server, self.handler, self.rfile, self.wfile, self.config
        )
        assert proto.selector == "/dir"
        assert proto.requestlist == ["/dir/"]

    def testInitBothSlashProblems(self):
        proto = BaseGopherProtocol(
            "dir/\n", self.server, self.handler, self.rfile, self.wfile, self.config
        )
        assert proto.selector == "/dir"
        assert proto.requestlist == ["dir/"]

    def testInitSplit(self):
        proto = BaseGopherProtocol(
            "foo.txt\t+\n",
            self.server,
            self.handler,
            self.rfile,
            self.wfile,
            self.config,
        )
        assert proto.selector == "/foo.txt"
        assert proto.requestlist == ["foo.txt", "+"]

    def testcanhandlerequest(self):
        assert not self.proto.canhandlerequest()

    def testlog(self):
        # This is a file handler, not a request handler!
        handler = self.proto.gethandler()
        self.proto.log(handler)
        self.assertEqual(self.logfile.getvalue(), self.logstr)

    def testhandle_file(self):
        self.proto.handle()
        self.assertEqual(self.logfile.getvalue(), self.logstr)
        self.assertEqual(self.wfile.getvalue(), b"Test\n")

    def testhandle_notfound(self):
        proto = BaseGopherProtocol(
            "/NONEXISTANT.txt\n",
            self.server,
            self.handler,
            self.rfile,
            self.wfile,
            self.config,
        )
        proto.handle()
        self.assertEqual(
            self.logfile.getvalue(),
            "10.77.77.77 [BaseGopherProtocol/None] EXCEPTION FileNotFound: '/NONEXISTANT.txt' does not exist (no handler found)\n",
        )
        self.assertEqual(
            self.wfile.getvalue(),
            b"3'/NONEXISTANT.txt' does not exist (no handler found)\t\terror.host\t1\r\n",
        )

    # We cannot test handle_dir here because we don't have enough info.

    def testfilenotfound(self):
        self.proto.filenotfound("FOO")
        self.assertEqual(self.wfile.getvalue(), b"3FOO\t\terror.host\t1\r\n")

    def testgethandler(self):
        handler = self.proto.gethandler()
        assert isinstance(handler, pygopherd.handlers.file.FileHandler)
        # Make sure caching works.
        assert handler == self.proto.gethandler()

    # CANNOT TEST: writedir, renderabstract

    def testrenderdirstart(self):
        assert self.proto.renderdirstart(GopherEntry("foo", self.config)) is None

    def testrenderdirend(self):
        assert self.proto.renderdirend(GopherEntry("foo", self.config)) is None

    def testrenderobjinfo(self):
        assert self.proto.renderobjinfo(GopherEntry("foo", self.config)) is None

    def testgroksabstract(self):
        assert not self.proto.groksabstract()