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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
|
import io
import unittest
from pygopherd import testutil
from pygopherd.protocols.http import HTTPProtocol
class TestHTTPProtocol(unittest.TestCase):
def setUp(self):
self.config = testutil.get_config()
self.logfile = testutil.get_string_logger()
self.rfile = io.BytesIO(b"Accept:text/plain\nHost:localhost.com\n\n")
self.wfile = io.BytesIO()
self.handler = testutil.get_testing_handler(self.rfile, self.wfile, self.config)
def test_http_handler(self):
request = "GET / HTTP/1.1"
protocol = HTTPProtocol(
request,
self.handler.server,
self.handler,
self.rfile,
self.wfile,
self.config,
)
self.assertTrue(protocol.canhandlerequest())
protocol.handle()
self.assertEqual(protocol.httpheaders["host"], "localhost.com")
response = self.wfile.getvalue().decode(errors="surrogateescape")
self.assertIn("HTTP/1.0 200 OK", response)
self.assertIn("Content-Type: text/html", response)
self.assertIn('SRC="/PYGOPHERD-HTTPPROTO-ICONS/text.gif"', response)
@unittest.skipUnless(
testutil.supports_non_utf8_filenames(),
reason="Filesystem does not support non-utf8 filenames.",
)
def test_http_hander_non_utf8(self):
request = "GET / HTTP/1.1"
protocol = HTTPProtocol(
request,
self.handler.server,
self.handler,
self.rfile,
self.wfile,
self.config,
)
self.assertTrue(protocol.canhandlerequest())
protocol.handle()
self.assertEqual(protocol.httpheaders["host"], "localhost.com")
response = self.wfile.getvalue().decode(errors="surrogateescape")
# Non-UTF8 files should show up in the listing. I don't know how
# browsers are supposed to render these, but chrome seems to figure
# it out as a ® symbol.
self.assertIn('<TD> <A HREF="/%AE.txt"><TT>\udcae', response)
def test_http_handler_icon(self):
request = "GET /PYGOPHERD-HTTPPROTO-ICONS/text.gif HTTP/1.1"
protocol = HTTPProtocol(
request,
self.handler.server,
self.handler,
self.rfile,
self.wfile,
self.config,
)
self.assertTrue(protocol.canhandlerequest())
protocol.handle()
response = self.wfile.getvalue()
self.assertIn(b"HTTP/1.0 200 OK", response)
self.assertIn(b"Content-Type: image/gif", response)
self.assertIn(b"This art is in the public domain", response)
def test_http_handler_not_found(self):
request = "GET /invalid-filename HTTP/1.1"
protocol = HTTPProtocol(
request,
self.handler.server,
self.handler,
self.rfile,
self.wfile,
self.config,
)
self.assertTrue(protocol.canhandlerequest())
protocol.handle()
response = self.wfile.getvalue().decode()
self.assertIn("HTTP/1.0 404 Not Found", response)
self.assertIn("Content-Type: text/html", response)
self.assertIn(
"'/invalid-filename' does not exist (no handler found)", response
)
def test_http_handler_search(self):
request = "GET /?searchrequest=foo%20bar HTTP/1.1"
protocol = HTTPProtocol(
request,
self.handler.server,
self.handler,
self.rfile,
self.wfile,
self.config,
)
self.assertTrue(protocol.canhandlerequest())
protocol.handle()
self.assertEqual(protocol.searchrequest, "foo bar")
|