| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import html |
| 4 |
import io |
| 5 |
import unittest |
| 6 |
|
| 7 |
from pygopherd import initialization, testutil |
| 8 |
from pygopherd.handlers.base import VFS_Real |
| 9 |
from pygopherd.handlers.tal import TALFileHandler, talavailable |
| 10 |
|
| 11 |
TEST_TEMPLATE = """ |
| 12 |
<html> |
| 13 |
<head> |
| 14 |
<title>TAL Test</title> |
| 15 |
</head> |
| 16 |
<body> |
| 17 |
My selector is: <b>{selector}</b><br> |
| 18 |
My MIME type is: <b>text/html</b><br> |
| 19 |
Another way of getting that is: <b>text/html</b><br> |
| 20 |
Gopher type is: <b>h</b><br> |
| 21 |
My handler is: <b>{handler}</b><br> |
| 22 |
My protocol is: <b>{protocol}</b><br> |
| 23 |
Python path enabling status: <b>1</b><br> |
| 24 |
My vfs is: <b>{vfs}</b><br> |
| 25 |
Math: <b>4</b> |
| 26 |
</body> |
| 27 |
</html> |
| 28 |
""" |
| 29 |
|
| 30 |
|
| 31 |
class TestTALHandler(unittest.TestCase): |
| 32 |
def setUp(self): |
| 33 |
self.config = testutil.get_config() |
| 34 |
self.vfs = VFS_Real(self.config) |
| 35 |
self.selector = "/talsample.html.tal" |
| 36 |
self.protocol = testutil.get_testing_protocol(self.selector, config=self.config) |
| 37 |
self.stat_result = self.vfs.stat(self.selector) |
| 38 |
|
| 39 |
|
| 40 |
initialization.init_logger(self.config, "") |
| 41 |
initialization.init_mimetypes(self.config) |
| 42 |
|
| 43 |
def test_tal_available(self): |
| 44 |
self.assertTrue(talavailable) |
| 45 |
|
| 46 |
def test_tal_handler(self): |
| 47 |
handler = TALFileHandler( |
| 48 |
self.selector, "", self.protocol, self.config, self.stat_result, self.vfs |
| 49 |
) |
| 50 |
|
| 51 |
self.assertTrue(handler.canhandlerequest()) |
| 52 |
|
| 53 |
entry = handler.getentry() |
| 54 |
self.assertEqual(entry.mimetype, "text/html") |
| 55 |
self.assertEqual(entry.type, "h") |
| 56 |
|
| 57 |
wfile = io.BytesIO() |
| 58 |
handler.write(wfile) |
| 59 |
rendered_data = wfile.getvalue().decode() |
| 60 |
|
| 61 |
expected_data = TEST_TEMPLATE.format( |
| 62 |
selector=handler.selector, |
| 63 |
handler=html.escape(str(handler)), |
| 64 |
protocol=html.escape(str(self.protocol)), |
| 65 |
vfs=html.escape(str(self.vfs)), |
| 66 |
) |
| 67 |
|
| 68 |
self.assertEqual(rendered_data.strip(), expected_data.strip()) |