| 1 |
import io |
| 2 |
import unittest |
| 3 |
|
| 4 |
from pygopherd import testutil |
| 5 |
from pygopherd.protocols.gopherp import GopherPlusProtocol |
| 6 |
|
| 7 |
|
| 8 |
class TestGopherPlusProtocol(unittest.TestCase): |
| 9 |
def setUp(self): |
| 10 |
self.config = testutil.get_config() |
| 11 |
self.logfile = testutil.get_string_logger() |
| 12 |
self.rfile = io.BytesIO() |
| 13 |
self.wfile = io.BytesIO() |
| 14 |
self.handler = testutil.get_testing_handler(self.rfile, self.wfile, self.config) |
| 15 |
|
| 16 |
def test_get_file(self): |
| 17 |
request = "/README\t+" |
| 18 |
protocol = GopherPlusProtocol( |
| 19 |
request, |
| 20 |
self.handler.server, |
| 21 |
self.handler, |
| 22 |
self.rfile, |
| 23 |
self.wfile, |
| 24 |
self.config, |
| 25 |
) |
| 26 |
|
| 27 |
self.assertTrue(protocol.canhandlerequest()) |
| 28 |
|
| 29 |
protocol.handle() |
| 30 |
self.assertEqual(protocol.gopherpstring, "+") |
| 31 |
self.assertEqual(protocol.handlemethod, "documentonly") |
| 32 |
|
| 33 |
response = self.wfile.getvalue().decode() |
| 34 |
|
| 35 |
expected = [ |
| 36 |
"+142", |
| 37 |
"This directory contains data for the unit tests.", |
| 38 |
"", |
| 39 |
"Some tests are dependant upon the precise length of files; those files are", |
| 40 |
"added with -kb.", |
| 41 |
"", |
| 42 |
] |
| 43 |
self.assertListEqual(response.splitlines(keepends=False), expected) |
| 44 |
|
| 45 |
|
| 46 |
self.assertTrue(len(response.encode()), 148) |
| 47 |
|
| 48 |
def test_file_not_found(self): |
| 49 |
protocol = GopherPlusProtocol( |
| 50 |
"/invalid-selector\t+", |
| 51 |
self.handler.server, |
| 52 |
self.handler, |
| 53 |
self.rfile, |
| 54 |
self.wfile, |
| 55 |
self.config, |
| 56 |
) |
| 57 |
|
| 58 |
self.assertTrue(protocol.canhandlerequest()) |
| 59 |
|
| 60 |
protocol.handle() |
| 61 |
self.assertEqual(protocol.gopherpstring, "+") |
| 62 |
self.assertEqual(protocol.handlemethod, "documentonly") |
| 63 |
|
| 64 |
response = self.wfile.getvalue().decode() |
| 65 |
|
| 66 |
expected = [ |
| 67 |
"--2", |
| 68 |
"1 Unconfigured Pygopherd Admin <[email protected]>", |
| 69 |
"'/invalid-selector' does not exist (no handler found)", |
| 70 |
] |
| 71 |
|
| 72 |
self.assertListEqual(response.splitlines(keepends=False), expected) |
| 73 |
|
| 74 |
def test_get_directory(self): |
| 75 |
protocol = GopherPlusProtocol( |
| 76 |
"/gopherplus\t+", |
| 77 |
self.handler.server, |
| 78 |
self.handler, |
| 79 |
self.rfile, |
| 80 |
self.wfile, |
| 81 |
self.config, |
| 82 |
) |
| 83 |
|
| 84 |
self.assertTrue(protocol.canhandlerequest()) |
| 85 |
|
| 86 |
protocol.handle() |
| 87 |
response = self.wfile.getvalue().decode() |
| 88 |
|
| 89 |
lines = response.splitlines(keepends=False) |
| 90 |
|
| 91 |
self.assertEqual(lines[0], "+-2") |
| 92 |
self.assertEqual(lines[1][-2:], "\t+") |
| 93 |
self.assertEqual(lines[2][-2:], "\t+") |
| 94 |
|
| 95 |
def test_get_file_info(self): |
| 96 |
protocol = GopherPlusProtocol( |
| 97 |
"/gopherplus/README\t!", |
| 98 |
self.handler.server, |
| 99 |
self.handler, |
| 100 |
self.rfile, |
| 101 |
self.wfile, |
| 102 |
self.config, |
| 103 |
) |
| 104 |
|
| 105 |
self.assertTrue(protocol.canhandlerequest()) |
| 106 |
|
| 107 |
protocol.handle() |
| 108 |
response = self.wfile.getvalue().decode() |
| 109 |
|
| 110 |
self.assertIn("+VIEWS:\r\n text/plain: <0k>", response) |
| 111 |
self.assertIn("+INFO: 0README\t", response) |
| 112 |
self.assertIn("+ADMIN:", response) |
| 113 |
self.assertIn("+3D:\r\n this is a gopher+ info attribute", response) |
| 114 |
|
| 115 |
def test_get_directory_info(self): |
| 116 |
protocol = GopherPlusProtocol( |
| 117 |
"/gopherplus\t$", |
| 118 |
self.handler.server, |
| 119 |
self.handler, |
| 120 |
self.rfile, |
| 121 |
self.wfile, |
| 122 |
self.config, |
| 123 |
) |
| 124 |
|
| 125 |
self.assertTrue(protocol.canhandlerequest()) |
| 126 |
|
| 127 |
protocol.handle() |
| 128 |
response = self.wfile.getvalue().decode() |
| 129 |
|
| 130 |
self.assertIn("+INFO: 0README\t", response) |
| 131 |
self.assertIn("+INFO: 0testfile\t", response) |
| 132 |
|
| 133 |
def test_ask_query(self): |
| 134 |
""" |
| 135 |
Not supported by pygopherd :( |
| 136 |
""" |