Debian Codegraph @

tests/protocols/test_base.py

raw symbols SCIP
1 import unittest
2 from io import BytesIO
3
4 import pygopherd.handlers.file
5 from pygopherd import testutil
6 from pygopherd.gopherentry import GopherEntry
7 from pygopherd.protocols.base import BaseGopherProtocol
8
9
10 class BaseProtocolTestCase(unittest.TestCase):
11 def setUp(self):
12 self.config = testutil.get_config()
13 self.rfile = BytesIO(b"/testfile.txt\n")
14 self.wfile = BytesIO()
15 self.logfile = testutil.get_string_logger()
16 self.logstr = "10.77.77.77 [BaseGopherProtocol/FileHandler]: /testfile.txt\n"
17 self.handler = testutil.get_testing_handler(self.rfile, self.wfile, self.config)
18 self.server = self.handler.server
19 self.proto = BaseGopherProtocol(
20 "/testfile.txt\n",
21 self.server,
22 self.handler,
23 self.rfile,
24 self.wfile,
25 self.config,
26 )
27
28 def testInitBasic(self):
29 proto = BaseGopherProtocol(
30 "/foo.txt\n", self.server, self.handler, self.rfile, self.wfile, self.config
31 )
32 assert proto.rfile == self.rfile
33 assert proto.wfile == self.wfile
34 assert proto.config == self.config
35 assert proto.requesthandler == self.handler
36 assert proto.requestlist == ["/foo.txt"]
37 assert proto.searchrequest is None
38 assert proto.handler is None
39 assert proto.selector == "/foo.txt"
40
41 def testInitEmpty(self):
42 proto = BaseGopherProtocol(
43 "\n", self.server, self.handler, self.rfile, self.wfile, self.config
44 )
45 # It should be rewritten to /
46 assert proto.selector == "/"
47 assert proto.requestlist == [""]
48
49 def testInitMissingLeadingSlash(self):
50 proto = BaseGopherProtocol(
51 "foo.txt\n", self.server, self.handler, self.rfile, self.wfile, self.config
52 )
53 assert proto.selector == "/foo.txt"
54 assert proto.requestlist == ["foo.txt"]
55
56 def testInitAddedTrailingSlash(self):
57 proto = BaseGopherProtocol(
58 "/dir/\n", self.server, self.handler, self.rfile, self.wfile, self.config
59 )
60 assert proto.selector == "/dir"
61 assert proto.requestlist == ["/dir/"]
62
63 def testInitBothSlashProblems(self):
64 proto = BaseGopherProtocol(
65 "dir/\n", self.server, self.handler, self.rfile, self.wfile, self.config
66 )
67 assert proto.selector == "/dir"
68 assert proto.requestlist == ["dir/"]
69
70 def testInitSplit(self):
71 proto = BaseGopherProtocol(
72 "foo.txt\t+\n",
73 self.server,
74 self.handler,
75 self.rfile,
76 self.wfile,
77 self.config,
78 )
79 assert proto.selector == "/foo.txt"
80 assert proto.requestlist == ["foo.txt", "+"]
81
82 def testcanhandlerequest(self):
83 assert not self.proto.canhandlerequest()
84
85 def testlog(self):
86 # This is a file handler, not a request handler!
87 handler = self.proto.gethandler()
88 self.proto.log(handler)
89 self.assertEqual(self.logfile.getvalue(), self.logstr)
90
91 def testhandle_file(self):
92 self.proto.handle()
93 self.assertEqual(self.logfile.getvalue(), self.logstr)
94 self.assertEqual(self.wfile.getvalue(), b"Test\n")
95
96 def testhandle_notfound(self):
97 proto = BaseGopherProtocol(
98 "/NONEXISTANT.txt\n",
99 self.server,
100 self.handler,
101 self.rfile,
102 self.wfile,
103 self.config,
104 )
105 proto.handle()
106 self.assertEqual(
107 self.logfile.getvalue(),
108 "10.77.77.77 [BaseGopherProtocol/None] EXCEPTION FileNotFound: '/NONEXISTANT.txt' does not exist (no handler found)\n",
109 )
110 self.assertEqual(
111 self.wfile.getvalue(),
112 b"3'/NONEXISTANT.txt' does not exist (no handler found)\t\terror.host\t1\r\n",
113 )
114
115 # We cannot test handle_dir here because we don't have enough info.
116
117 def testfilenotfound(self):
118 self.proto.filenotfound("FOO")
119 self.assertEqual(self.wfile.getvalue(), b"3FOO\t\terror.host\t1\r\n")
120
121 def testgethandler(self):
122 handler = self.proto.gethandler()
123 assert isinstance(handler, pygopherd.handlers.file.FileHandler)
124 # Make sure caching works.
125 assert handler == self.proto.gethandler()
126
127 # CANNOT TEST: writedir, renderabstract
128
129 def testrenderdirstart(self):
130 assert self.proto.renderdirstart(GopherEntry("foo", self.config)) is None
131
132 def testrenderdirend(self):
133 assert self.proto.renderdirend(GopherEntry("foo", self.config)) is None
134
135 def testrenderobjinfo(self):
136 assert self.proto.renderobjinfo(GopherEntry("foo", self.config)) is None
137
138 def testgroksabstract(self):
139 assert not self.proto.groksabstract()