File: test_gopherp.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 (136 lines) | stat: -rw-r--r-- 3,955 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
import io
import unittest

from pygopherd import testutil
from pygopherd.protocols.gopherp import GopherPlusProtocol


class TestGopherPlusProtocol(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 test_get_file(self):
        request = "/README\t+"
        protocol = GopherPlusProtocol(
            request,
            self.handler.server,
            self.handler,
            self.rfile,
            self.wfile,
            self.config,
        )

        self.assertTrue(protocol.canhandlerequest())

        protocol.handle()
        self.assertEqual(protocol.gopherpstring, "+")
        self.assertEqual(protocol.handlemethod, "documentonly")

        response = self.wfile.getvalue().decode()

        expected = [
            "+142",
            "This directory contains data for the unit tests.",
            "",
            "Some tests are dependant upon the precise length of files; those files are",
            "added with -kb.",
            "",
        ]
        self.assertListEqual(response.splitlines(keepends=False), expected)

        # 142 byte filesize + 6 bytes for the "+142\r\n"
        self.assertTrue(len(response.encode()), 148)

    def test_file_not_found(self):
        protocol = GopherPlusProtocol(
            "/invalid-selector\t+",
            self.handler.server,
            self.handler,
            self.rfile,
            self.wfile,
            self.config,
        )

        self.assertTrue(protocol.canhandlerequest())

        protocol.handle()
        self.assertEqual(protocol.gopherpstring, "+")
        self.assertEqual(protocol.handlemethod, "documentonly")

        response = self.wfile.getvalue().decode()

        expected = [
            "--2",
            "1 Unconfigured Pygopherd Admin <pygopherd@nowhere.nowhere>",
            "'/invalid-selector' does not exist (no handler found)",
        ]

        self.assertListEqual(response.splitlines(keepends=False), expected)

    def test_get_directory(self):
        protocol = GopherPlusProtocol(
            "/gopherplus\t+",
            self.handler.server,
            self.handler,
            self.rfile,
            self.wfile,
            self.config,
        )

        self.assertTrue(protocol.canhandlerequest())

        protocol.handle()
        response = self.wfile.getvalue().decode()

        lines = response.splitlines(keepends=False)

        self.assertEqual(lines[0], "+-2")
        self.assertEqual(lines[1][-2:], "\t+")
        self.assertEqual(lines[2][-2:], "\t+")

    def test_get_file_info(self):
        protocol = GopherPlusProtocol(
            "/gopherplus/README\t!",
            self.handler.server,
            self.handler,
            self.rfile,
            self.wfile,
            self.config,
        )

        self.assertTrue(protocol.canhandlerequest())

        protocol.handle()
        response = self.wfile.getvalue().decode()

        self.assertIn("+VIEWS:\r\n text/plain: <0k>", response)
        self.assertIn("+INFO: 0README\t", response)
        self.assertIn("+ADMIN:", response)
        self.assertIn("+3D:\r\n this is a gopher+ info attribute", response)

    def test_get_directory_info(self):
        protocol = GopherPlusProtocol(
            "/gopherplus\t$",
            self.handler.server,
            self.handler,
            self.rfile,
            self.wfile,
            self.config,
        )

        self.assertTrue(protocol.canhandlerequest())

        protocol.handle()
        response = self.wfile.getvalue().decode()

        self.assertIn("+INFO: 0README\t", response)
        self.assertIn("+INFO: 0testfile\t", response)

    def test_ask_query(self):
        """
        Not supported by pygopherd :(
        """