]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob_test.py
Begin parsing glowfic html
[paperdoorknob] / paperdoorknob_test.py
CommitLineData
b25a2f90
SW
1# paperdoorknob: Print glowfic
2#
3# This program is free software: you can redistribute it and/or modify it
4# under the terms of the GNU General Public License as published by the
5# Free Software Foundation, version 3.
6
7
8import unittest
9import threading
10from http.server import BaseHTTPRequestHandler, HTTPServer
6fdb8f01 11import requests
b34a368f 12import requests_cache
b25a2f90
SW
13import paperdoorknob
14
b25a2f90
SW
15TIMEOUT = 8
16
17
18class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler):
19
17bd16e8
SW
20 def _notify_test(self) -> None:
21 raise NotImplementedError()
22
6fdb8f01
SW
23 def _response_code(self) -> int:
24 if self.path == "/not_found":
25 return 404
26 if self.path == "/server_error":
27 return 500
28 return 200
29
b25a2f90 30 def do_GET(self) -> None:
6409066b
SW
31 body = b'''<html>
32 <body>
33 <div class="post-container post-post">
34 This is glowfic
35 </div>
36 </body>
37</html>'''
6fdb8f01 38 self.send_response(self._response_code())
b25a2f90
SW
39 self.send_header("Content-type", "text/html")
40 self.send_header("Content-Length", str(len(body)))
41 self.end_headers()
42 self.wfile.write(body)
17bd16e8 43 self._notify_test()
b25a2f90
SW
44
45
46class TestFetch(unittest.TestCase):
681380e2
SW
47 def _port(self) -> int:
48 port = self._web_server.socket.getsockname()[1]
49 assert isinstance(port, int)
50 return port
51
17bd16e8
SW
52 def _count_request(self) -> None:
53 self._request_counter += 1
54
b25a2f90 55 def setUp(self) -> None:
17bd16e8
SW
56 self._request_counter = 0
57 handler = type("Handler", (FakeGlowficHTTPRequestHandler,), {
58 '_notify_test': lambda _: self._count_request()})
59 self._web_server = HTTPServer(('', 0), handler)
681380e2 60 self._thread = threading.Thread(target=self._web_server.serve_forever)
e3ba2056 61 self._thread.start()
b25a2f90
SW
62
63 def tearDown(self) -> None:
681380e2 64 self._web_server.shutdown()
e3ba2056 65 self._thread.join()
971e6658 66 self._web_server.server_close()
b25a2f90
SW
67
68 def testFetch(self) -> None:
e138a9b4
SW
69 with requests.session() as s:
70 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
71 self.assertEqual(self._request_counter, 1)
72 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
73 self.assertEqual(self._request_counter, 2)
b25a2f90 74
b34a368f
SW
75 def testFetchCaching(self) -> None:
76 with requests_cache.CachedSession() as s:
77 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
78 self.assertEqual(self._request_counter, 1)
79 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
80 self.assertEqual(self._request_counter, 1)
81
de7251fc
SW
82 def testFetchPersistentCaching(self) -> None:
83 with requests_cache.CachedSession() as s:
84 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
85 self.assertEqual(self._request_counter, 1)
86 with requests_cache.CachedSession() as s:
87 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
88 self.assertEqual(self._request_counter, 1)
89
136277e3
SW
90 def testFetchConents(self) -> None:
91 with requests.session() as s:
6409066b
SW
92 post = paperdoorknob.Post(paperdoorknob.fetch(
93 f"http://localhost:{self._port()}", s, TIMEOUT))
94 self.assertEqual(post.text().text.strip(), "This is glowfic")
136277e3 95
6fdb8f01 96 def testFetchErrors(self) -> None:
e138a9b4
SW
97 with requests.session() as s:
98 with self.assertRaises(requests.HTTPError):
99 paperdoorknob.fetch(
100 f"http://localhost:{self._port()}/not_found", s, TIMEOUT)
101 with self.assertRaises(requests.HTTPError):
102 paperdoorknob.fetch(
103 f"http://localhost:{self._port()}/server_error", s, TIMEOUT)
6fdb8f01 104
b25a2f90
SW
105
106if __name__ == '__main__':
107 unittest.main()