| 1 |
import io |
| 2 |
import unittest |
| 3 |
|
| 4 |
from pygopherd import testutil |
| 5 |
from pygopherd.initialization import init_mimetypes |
| 6 |
from pygopherd.protocols.spartan import SpartanProtocol |
| 7 |
|
| 8 |
|
| 9 |
class TestSpartanProtocol(unittest.TestCase): |
| 10 |
def setUp(self): |
| 11 |
self.config = testutil.get_config() |
| 12 |
self.logfile = testutil.get_string_logger() |
| 13 |
self.rfile = io.BytesIO() |
| 14 |
self.wfile = io.BytesIO() |
| 15 |
self.handler = testutil.get_testing_handler(self.rfile, self.wfile, self.config) |
| 16 |
|
| 17 |
def get_protocol(self, request): |
| 18 |
return SpartanProtocol( |
| 19 |
request, |
| 20 |
self.handler.server, |
| 21 |
self.handler, |
| 22 |
self.rfile, |
| 23 |
self.wfile, |
| 24 |
self.config, |
| 25 |
) |
| 26 |
|
| 27 |
def test_spartan_handler(self): |
| 28 |
protocol = self.get_protocol("localhost / 0") |
| 29 |
self.assertTrue(protocol.canhandlerequest()) |
| 30 |
|
| 31 |
protocol.handle() |
| 32 |
|
| 33 |
response = self.wfile.getvalue().decode(errors="surrogateescape") |
| 34 |
self.assertTrue(response.startswith("2 text/gemini\r\n")) |
| 35 |
|
| 36 |
@unittest.skipUnless( |
| 37 |
testutil.supports_non_utf8_filenames(), |
| 38 |
reason="Filesystem does not support non-utf8 filenames.", |
| 39 |
) |
| 40 |
def test_non_utf8(self): |
| 41 |
protocol = self.get_protocol("localhost / 0") |
| 42 |
protocol.handle() |
| 43 |
|
| 44 |
response = self.wfile.getvalue().decode(errors="surrogateescape") |
| 45 |
|
| 46 |
|
| 47 |
self.assertIn("=> /%AE.txt \\xae\n", response) |
| 48 |
|
| 49 |
def test_not_found(self): |
| 50 |
protocol = self.get_protocol("localhost /dnsajd 0") |
| 51 |
protocol.handle() |
| 52 |
|
| 53 |
response = self.wfile.getvalue().decode() |
| 54 |
self.assertEqual("4 '/dnsajd' does not exist (no handler found)\r\n", response) |
| 55 |
|
| 56 |
def test_generate_query_link(self): |
| 57 |
protocol = self.get_protocol("localhost / 0") |
| 58 |
protocol.handle() |
| 59 |
|
| 60 |
response = self.wfile.getvalue().decode(errors="surrogateescape") |
| 61 |
self.assertIn("=: / Enter a query", response) |
| 62 |
|
| 63 |
def test_search_request(self): |
| 64 |
self.rfile = io.BytesIO(b"\xae") |
| 65 |
protocol = self.get_protocol("localhost / 2") |
| 66 |
protocol.handle() |
| 67 |
|
| 68 |
self.assertEqual(protocol.searchrequest, "\udcae") |
| 69 |
|
| 70 |
def test_detect_mime_type(self): |
| 71 |
init_mimetypes(self.config) |
| 72 |
|
| 73 |
protocol = self.get_protocol("localhost /testfile.gmi 0") |
| 74 |
protocol.handle() |
| 75 |
|
| 76 |
response = self.wfile.getvalue().decode(errors="surrogateescape") |
| 77 |
self.assertTrue(response.startswith("2 text/gemini\r\n")) |