| 1 |
import io |
| 2 |
import unittest |
| 3 |
|
| 4 |
from pygopherd import testutil |
| 5 |
from pygopherd.initialization import init_mimetypes |
| 6 |
from pygopherd.protocols.gemini import GeminiProtocol |
| 7 |
|
| 8 |
|
| 9 |
class TestGeminiProtocol(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( |
| 16 |
self.rfile, self.wfile, self.config, use_tls=True |
| 17 |
) |
| 18 |
|
| 19 |
def get_protocol(self, request): |
| 20 |
return GeminiProtocol( |
| 21 |
request, |
| 22 |
self.handler.server, |
| 23 |
self.handler, |
| 24 |
self.rfile, |
| 25 |
self.wfile, |
| 26 |
self.config, |
| 27 |
) |
| 28 |
|
| 29 |
def test_gemini_handler(self): |
| 30 |
protocol = self.get_protocol("gemini://localhost") |
| 31 |
self.assertTrue(protocol.canhandlerequest()) |
| 32 |
|
| 33 |
protocol.handle() |
| 34 |
|
| 35 |
response = self.wfile.getvalue().decode(errors="surrogateescape") |
| 36 |
self.assertTrue(response.startswith("20 text/gemini\r\n")) |
| 37 |
|
| 38 |
def test_optional_trailing_slash(self): |
| 39 |
|
| 40 |
protocol = self.get_protocol("gemini://localhost/") |
| 41 |
protocol.handle() |
| 42 |
|
| 43 |
response = self.wfile.getvalue().decode(errors="surrogateescape") |
| 44 |
self.assertTrue(response.startswith("20 text/gemini\r\n")) |
| 45 |
|
| 46 |
@unittest.skipUnless( |
| 47 |
testutil.supports_non_utf8_filenames(), |
| 48 |
reason="Filesystem does not support non-utf8 filenames.", |
| 49 |
) |
| 50 |
def test_non_utf8(self): |
| 51 |
protocol = self.get_protocol("gemini://localhost/") |
| 52 |
protocol.handle() |
| 53 |
|
| 54 |
response = self.wfile.getvalue().decode(errors="surrogateescape") |
| 55 |
|
| 56 |
|
| 57 |
self.assertIn("=> /%AE.txt \\xae\n", response) |
| 58 |
|
| 59 |
def test_not_found(self): |
| 60 |
protocol = self.get_protocol("gemini://localhost/dnsajd") |
| 61 |
protocol.handle() |
| 62 |
|
| 63 |
response = self.wfile.getvalue().decode() |
| 64 |
self.assertEqual("51 '/dnsajd' does not exist (no handler found)\r\n", response) |
| 65 |
|
| 66 |
def test_generate_query_link(self): |
| 67 |
protocol = self.get_protocol("gemini://localhost") |
| 68 |
protocol.handle() |
| 69 |
|
| 70 |
response = self.wfile.getvalue().decode(errors="surrogateescape") |
| 71 |
self.assertIn("=> /GEMINI-QUERY/ Enter a query", response) |
| 72 |
|
| 73 |
def test_prompt_query(self): |
| 74 |
protocol = self.get_protocol("gemini://localhost/GEMINI-QUERY/some/path") |
| 75 |
protocol.handle() |
| 76 |
|
| 77 |
response = self.wfile.getvalue().decode(errors="surrogateescape") |
| 78 |
self.assertEqual("10 Enter input\r\n", response) |
| 79 |
|
| 80 |
def test_submit_query(self): |
| 81 |
protocol = self.get_protocol("gemini://localhost/GEMINI-QUERY/some/path?%AE") |
| 82 |
protocol.handle() |
| 83 |
|
| 84 |
response = self.wfile.getvalue().decode(errors="surrogateescape") |
| 85 |
self.assertEqual("30 /some/path?%AE\r\n", response) |
| 86 |
|
| 87 |
def test_search_request(self): |
| 88 |
protocol = self.get_protocol("gemini://localhost?%AE") |
| 89 |
protocol.handle() |
| 90 |
|
| 91 |
self.assertEqual(protocol.searchrequest, "\udcae") |
| 92 |
|
| 93 |
def test_detect_mime_type(self): |
| 94 |
init_mimetypes(self.config) |
| 95 |
|
| 96 |
protocol = self.get_protocol("gemini://localhost/testfile.gmi") |
| 97 |
protocol.handle() |
| 98 |
|
| 99 |
response = self.wfile.getvalue().decode(errors="surrogateescape") |
| 100 |
self.assertTrue(response.startswith("20 text/gemini\r\n")) |