| 1 |
import io |
| 2 |
import unittest |
| 3 |
|
| 4 |
from pygopherd import testutil |
| 5 |
from pygopherd.protocols.http import HTTPProtocol |
| 6 |
|
| 7 |
|
| 8 |
class TestHTTPProtocol(unittest.TestCase): |
| 9 |
def setUp(self): |
| 10 |
self.config = testutil.get_config() |
| 11 |
self.logfile = testutil.get_string_logger() |
| 12 |
self.rfile = io.BytesIO(b"Accept:text/plain\nHost:localhost.com\n\n") |
| 13 |
self.wfile = io.BytesIO() |
| 14 |
self.handler = testutil.get_testing_handler(self.rfile, self.wfile, self.config) |
| 15 |
|
| 16 |
def test_http_handler(self): |
| 17 |
request = "GET / HTTP/1.1" |
| 18 |
protocol = HTTPProtocol( |
| 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.httpheaders["host"], "localhost.com") |
| 31 |
|
| 32 |
response = self.wfile.getvalue().decode(errors="surrogateescape") |
| 33 |
self.assertIn("HTTP/1.0 200 OK", response) |
| 34 |
self.assertIn("Content-Type: text/html", response) |
| 35 |
self.assertIn('SRC="/PYGOPHERD-HTTPPROTO-ICONS/text.gif"', response) |
| 36 |
|
| 37 |
@unittest.skipUnless( |
| 38 |
testutil.supports_non_utf8_filenames(), |
| 39 |
reason="Filesystem does not support non-utf8 filenames.", |
| 40 |
) |
| 41 |
def test_http_hander_non_utf8(self): |
| 42 |
request = "GET / HTTP/1.1" |
| 43 |
protocol = HTTPProtocol( |
| 44 |
request, |
| 45 |
self.handler.server, |
| 46 |
self.handler, |
| 47 |
self.rfile, |
| 48 |
self.wfile, |
| 49 |
self.config, |
| 50 |
) |
| 51 |
|
| 52 |
self.assertTrue(protocol.canhandlerequest()) |
| 53 |
|
| 54 |
protocol.handle() |
| 55 |
self.assertEqual(protocol.httpheaders["host"], "localhost.com") |
| 56 |
|
| 57 |
response = self.wfile.getvalue().decode(errors="surrogateescape") |
| 58 |
|
| 59 |
|
| 60 |
. |
| 61 |
self.assertIn('<TD> <A HREF="/%AE.txt"><TT>\udcae', response) |
| 62 |
|
| 63 |
def test_http_handler_icon(self): |
| 64 |
request = "GET /PYGOPHERD-HTTPPROTO-ICONS/text.gif HTTP/1.1" |
| 65 |
protocol = HTTPProtocol( |
| 66 |
request, |
| 67 |
self.handler.server, |
| 68 |
self.handler, |
| 69 |
self.rfile, |
| 70 |
self.wfile, |
| 71 |
self.config, |
| 72 |
) |
| 73 |
|
| 74 |
self.assertTrue(protocol.canhandlerequest()) |
| 75 |
|
| 76 |
protocol.handle() |
| 77 |
response = self.wfile.getvalue() |
| 78 |
self.assertIn(b"HTTP/1.0 200 OK", response) |
| 79 |
self.assertIn(b"Content-Type: image/gif", response) |
| 80 |
self.assertIn(b"This art is in the public domain", response) |
| 81 |
|
| 82 |
def test_http_handler_not_found(self): |
| 83 |
request = "GET /invalid-filename HTTP/1.1" |
| 84 |
protocol = HTTPProtocol( |
| 85 |
request, |
| 86 |
self.handler.server, |
| 87 |
self.handler, |
| 88 |
self.rfile, |
| 89 |
self.wfile, |
| 90 |
self.config, |
| 91 |
) |
| 92 |
|
| 93 |
self.assertTrue(protocol.canhandlerequest()) |
| 94 |
|
| 95 |
protocol.handle() |
| 96 |
response = self.wfile.getvalue().decode() |
| 97 |
self.assertIn("HTTP/1.0 404 Not Found", response) |
| 98 |
self.assertIn("Content-Type: text/html", response) |
| 99 |
self.assertIn( |
| 100 |
"'/invalid-filename' does not exist (no handler found)", response |
| 101 |
) |
| 102 |
|
| 103 |
def test_http_handler_search(self): |
| 104 |
request = "GET /?searchrequest=foo%20bar HTTP/1.1" |
| 105 |
protocol = HTTPProtocol( |
| 106 |
request, |
| 107 |
self.handler.server, |
| 108 |
self.handler, |
| 109 |
self.rfile, |
| 110 |
self.wfile, |
| 111 |
self.config, |
| 112 |
) |
| 113 |
|
| 114 |
self.assertTrue(protocol.canhandlerequest()) |
| 115 |
|
| 116 |
protocol.handle() |
| 117 |
self.assertEqual(protocol.searchrequest, "foo bar") |