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
|
import io
import unittest
from pygopherd import testutil
from pygopherd.initialization import init_mimetypes
from pygopherd.protocols.gemini import GeminiProtocol
class TestGeminiProtocol(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, use_tls=True
)
def get_protocol(self, request):
return GeminiProtocol(
request,
self.handler.server,
self.handler,
self.rfile,
self.wfile,
self.config,
)
def test_gemini_handler(self):
protocol = self.get_protocol("gemini://localhost")
self.assertTrue(protocol.canhandlerequest())
protocol.handle()
response = self.wfile.getvalue().decode(errors="surrogateescape")
self.assertTrue(response.startswith("20 text/gemini\r\n"))
def test_optional_trailing_slash(self):
# The page should load with or without the trailing slash
protocol = self.get_protocol("gemini://localhost/")
protocol.handle()
response = self.wfile.getvalue().decode(errors="surrogateescape")
self.assertTrue(response.startswith("20 text/gemini\r\n"))
@unittest.skipUnless(
testutil.supports_non_utf8_filenames(),
reason="Filesystem does not support non-utf8 filenames.",
)
def test_non_utf8(self):
protocol = self.get_protocol("gemini://localhost/")
protocol.handle()
response = self.wfile.getvalue().decode(errors="surrogateescape")
# The URL should be percent-sign encoded, the description should be
# backslash replaced.
self.assertIn("=> /%AE.txt \\xae\n", response)
def test_not_found(self):
protocol = self.get_protocol("gemini://localhost/dnsajd")
protocol.handle()
response = self.wfile.getvalue().decode()
self.assertEqual("51 '/dnsajd' does not exist (no handler found)\r\n", response)
def test_generate_query_link(self):
protocol = self.get_protocol("gemini://localhost")
protocol.handle()
response = self.wfile.getvalue().decode(errors="surrogateescape")
self.assertIn("=> /GEMINI-QUERY/ Enter a query", response)
def test_prompt_query(self):
protocol = self.get_protocol("gemini://localhost/GEMINI-QUERY/some/path")
protocol.handle()
response = self.wfile.getvalue().decode(errors="surrogateescape")
self.assertEqual("10 Enter input\r\n", response)
def test_submit_query(self):
protocol = self.get_protocol("gemini://localhost/GEMINI-QUERY/some/path?%AE")
protocol.handle()
response = self.wfile.getvalue().decode(errors="surrogateescape")
self.assertEqual("30 /some/path?%AE\r\n", response)
def test_search_request(self):
protocol = self.get_protocol("gemini://localhost?%AE")
protocol.handle()
self.assertEqual(protocol.searchrequest, "\udcae")
def test_detect_mime_type(self):
init_mimetypes(self.config)
protocol = self.get_protocol("gemini://localhost/testfile.gmi")
protocol.handle()
response = self.wfile.getvalue().decode(errors="surrogateescape")
self.assertTrue(response.startswith("20 text/gemini\r\n"))
|