Debian Codegraph @

tests/protocols/test_rfc1436.py

raw symbols SCIP
1 import typing
2 import unittest
3 from io import BytesIO
4
5 from pygopherd import testutil
6 from pygopherd.handlers import HandlerMultiplexer
7 from pygopherd.protocols.rfc1436 import GopherProtocol
8
9
10 class RFC1436TestCase(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.handler = testutil.get_testing_handler(self.rfile, self.wfile, self.config)
17 self.server = self.handler.server
18 self.proto = GopherProtocol(
19 "/testfile.txt\n",
20 self.server,
21 self.handler,
22 self.rfile,
23 self.wfile,
24 self.config,
25 )
26
27 def inject_handler(self, handler_name: str, index: typing.Optional[int] = None):
28 """
29 Add a handler to the HandlerMultiplexer in the configuration.
30 """
31 handlerlist = self.config.get("handlers.HandlerMultiplexer", "handlers")
32 handlers = [x.strip() for x in handlerlist.strip("[] \n").split(",")]
33 if index is not None:
34 handlers.insert(index, handler_name)
35 else:
36 handlers.append(handler_name)
37
38 handlerlist = "[" + ",".join(handlers) + "]"
39 self.config.set("handlers.HandlerMultiplexer", "handlers", handlerlist)
40
41 # Clear the cache
42 HandlerMultiplexer.handlers = None
43
44 def testcanhandlerequest(self):
45 assert self.proto.canhandlerequest()
46 proto = GopherProtocol(
47 "/testfile.txt\tsearch\n",
48 self.server,
49 self.handler,
50 self.rfile,
51 self.wfile,
52 self.config,
53 )
54 assert proto.canhandlerequest()
55 self.assertEqual(proto.selector, "/testfile.txt")
56 self.assertEqual(proto.searchrequest, "search")
57
58 def testrenderobjinfo(self):
59 expected = "0testfile.txt\t/testfile.txt\t%s\t%d\t+\r\n" % (
60 self.server.server_name,
61 self.server.server_port,
62 )
63 self.assertEqual(
64 self.proto.renderobjinfo(self.proto.gethandler().getentry()), expected
65 )
66
67 def testhandle_file(self):
68 self.proto.handle()
69 self.assertEqual(
70 self.logfile.getvalue(),
71 "10.77.77.77 [GopherProtocol/FileHandler]: /testfile.txt\n",
72 )
73 self.assertEqual(self.wfile.getvalue(), b"Test\n")
74
75 def testhandle_file_zipped(self):
76 self.config.set("handlers.ZIP.ZIPHandler", "enabled", "true")
77 self.inject_handler("ZIP.ZIPHandler", index=0)
78
79 self.proto = GopherProtocol(
80 "/testdata.zip/pygopherd/ziponly\n",
81 self.server,
82 self.handler,
83 self.rfile,
84 self.wfile,
85 self.config,
86 )
87 self.proto.handle()
88 self.assertEqual(self.wfile.getvalue(), b"ZIPonly\n")
89 self.config.set("handlers.ZIP.ZIPHandler", "enabled", "false")
90
91 def testhandle_dir_abstracts(self):
92 proto = GopherProtocol(
93 "", self.server, self.handler, self.rfile, self.wfile, self.config
94 )
95 proto.handle()
96 self.assertEqual(proto.selector, "/")
97 self.assertEqual(
98 self.logfile.getvalue(), "10.77.77.77 [GopherProtocol/UMNDirHandler]: /\n"
99 )
100 # Try to make this easy on us to fix.
101 actuallines = (
102 self.wfile.getvalue().decode(errors="surrogateescape").splitlines()
103 )
104 expectedlines = [
105 "iThis is the abstract for the testdata directory.\tfake\t(NULL)\t0",
106 "iThis is the abstract\tfake\t(NULL)\t0",
107 "ifor testfile.txt.gz\tfake\t(NULL)\t0",
108 ]
109
110 for line in expectedlines:
111 self.assertIn(line, actuallines)
112
113 # Make sure proper line endings are present.
114 self.assertEqual(
115 "\r\n".join(actuallines) + "\r\n",
116 self.wfile.getvalue().decode(errors="surrogateescape"),
117 )
118
119 def testhandle_dir_noabstract(self):
120 self.config.set("pygopherd", "abstract_headers", "off")
121 self.config.set("pygopherd", "abstract_entries", "off")
122 proto = GopherProtocol(
123 "", self.server, self.handler, self.rfile, self.wfile, self.config
124 )
125 proto.handle()
126 actuallines = (
127 self.wfile.getvalue().decode(errors="surrogateescape").splitlines()
128 )
129 expectedlines = [
130 "iThis is the abstract for the testdata directory.\tfake\t(NULL)\t0",
131 "iThis is the abstract\tfake\t(NULL)\t0",
132 "ifor testfile.txt.gz\tfake\t(NULL)\t0",
133 ]
134
135 for line in expectedlines:
136 self.assertNotIn(line, actuallines)