| 1 | import io |
| 2 | import unittest |
| 3 | |
| 4 | from pygopherd import testutil |
| 5 | from pygopherd.protocols.wap import WAPProtocol |
| 6 | |
| 7 | |
| 8 | class TestWAPProtocol(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_wap_handler(self): |
| 17 | request = "GET /wap HTTP/1.1" |
| 18 | protocol = WAPProtocol( |
| 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/vnd.wap.wml", response) |
| 35 | self.assertIn('href="/wap/README">README</a>', response) |
| 36 | |
| 37 | def test_wap_handler_not_found(self): |
| 38 | request = "GET /wap/invalid-filename HTTP/1.1" |
| 39 | protocol = WAPProtocol( |
| 40 | request, |
| 41 | self.handler.server, |
| 42 | self.handler, |
| 43 | self.rfile, |
| 44 | self.wfile, |
| 45 | self.config, |
| 46 | ) |
| 47 | |
| 48 | self.assertTrue(protocol.canhandlerequest()) |
| 49 | |
| 50 | protocol.handle() |
| 51 | response = self.wfile.getvalue().decode() |
| 52 | self.assertIn("HTTP/1.0 200 Not Found", response) |
| 53 | self.assertIn("Content-Type: text/vnd.wap.wml", response) |
| 54 | self.assertIn('<card id="index" title="404 Error" newcontext="true">', response) |
| 55 | |
| 56 | def test_wap_handler_search(self): |
| 57 | request = "GET /wap/?searchrequest=foo%20bar HTTP/1.1" |
| 58 | protocol = WAPProtocol( |
| 59 | request, |
| 60 | self.handler.server, |
| 61 | self.handler, |
| 62 | self.rfile, |
| 63 | self.wfile, |
| 64 | self.config, |
| 65 | ) |
| 66 | |
| 67 | self.assertTrue(protocol.canhandlerequest()) |
| 68 | |
| 69 | protocol.handle() |
| 70 | self.assertEqual(protocol.searchrequest, "foo bar") |