]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob_test.py
Drop Post as a class
[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">
170e50a0 34 <div class="post-edit-box">We don't want edit boxes</div>
6409066b 35 This is glowfic
170e50a0 36 <div class="post-footer">We don't want footers</div>
6409066b 37 </div>
a0d30541
SW
38 <div class="flat-post-replies">
39 <div class="post-container post-reply">
170e50a0 40 <div class="post-edit-box">We don't want edit boxes</div>
a0d30541 41 You sure?
170e50a0 42 <div class="post-footer">We don't want footers</div>
a0d30541
SW
43 </div>
44 <div class="post-container post-reply">
45 Pretty sure.
46 </div>
47 </div>
6409066b
SW
48 </body>
49</html>'''
6fdb8f01 50 self.send_response(self._response_code())
b25a2f90
SW
51 self.send_header("Content-type", "text/html")
52 self.send_header("Content-Length", str(len(body)))
53 self.end_headers()
54 self.wfile.write(body)
17bd16e8 55 self._notify_test()
b25a2f90
SW
56
57
58class TestFetch(unittest.TestCase):
681380e2
SW
59 def _port(self) -> int:
60 port = self._web_server.socket.getsockname()[1]
61 assert isinstance(port, int)
62 return port
63
17bd16e8
SW
64 def _count_request(self) -> None:
65 self._request_counter += 1
66
b25a2f90 67 def setUp(self) -> None:
17bd16e8
SW
68 self._request_counter = 0
69 handler = type("Handler", (FakeGlowficHTTPRequestHandler,), {
70 '_notify_test': lambda _: self._count_request()})
71 self._web_server = HTTPServer(('', 0), handler)
681380e2 72 self._thread = threading.Thread(target=self._web_server.serve_forever)
e3ba2056 73 self._thread.start()
b25a2f90
SW
74
75 def tearDown(self) -> None:
681380e2 76 self._web_server.shutdown()
e3ba2056 77 self._thread.join()
971e6658 78 self._web_server.server_close()
b25a2f90
SW
79
80 def testFetch(self) -> None:
e138a9b4
SW
81 with requests.session() as s:
82 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
83 self.assertEqual(self._request_counter, 1)
84 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
85 self.assertEqual(self._request_counter, 2)
b25a2f90 86
b34a368f
SW
87 def testFetchCaching(self) -> None:
88 with requests_cache.CachedSession() as s:
89 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
90 self.assertEqual(self._request_counter, 1)
91 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
92 self.assertEqual(self._request_counter, 1)
93
de7251fc
SW
94 def testFetchPersistentCaching(self) -> None:
95 with requests_cache.CachedSession() as s:
96 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
97 self.assertEqual(self._request_counter, 1)
98 with requests_cache.CachedSession() as s:
99 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
100 self.assertEqual(self._request_counter, 1)
101
47cfa3cd 102 def testProcess(self) -> None:
136277e3 103 with requests.session() as s:
47cfa3cd
SW
104 replies = paperdoorknob.process(
105 f"http://localhost:{self._port()}", s, TIMEOUT)
106 self.assertEqual([r.text.strip() for r in replies],
55958ec0 107 ["This is glowfic", "You sure?", "Pretty sure."])
136277e3 108
6fdb8f01 109 def testFetchErrors(self) -> None:
e138a9b4
SW
110 with requests.session() as s:
111 with self.assertRaises(requests.HTTPError):
112 paperdoorknob.fetch(
113 f"http://localhost:{self._port()}/not_found", s, TIMEOUT)
114 with self.assertRaises(requests.HTTPError):
115 paperdoorknob.fetch(
116 f"http://localhost:{self._port()}/server_error", s, TIMEOUT)
6fdb8f01 117
b25a2f90
SW
118
119if __name__ == '__main__':
120 unittest.main()