]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob_test.py
Invoke pandoc to convert HTML to LaTeX
[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
36ae1d5f 9import io
b25a2f90
SW
10import threading
11from http.server import BaseHTTPRequestHandler, HTTPServer
6fdb8f01 12import requests
b34a368f 13import requests_cache
b25a2f90
SW
14import paperdoorknob
15
b25a2f90
SW
16TIMEOUT = 8
17
18
19class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler):
20
17bd16e8
SW
21 def _notify_test(self) -> None:
22 raise NotImplementedError()
23
6fdb8f01
SW
24 def _response_code(self) -> int:
25 if self.path == "/not_found":
26 return 404
27 if self.path == "/server_error":
28 return 500
29 return 200
30
b25a2f90 31 def do_GET(self) -> None:
6409066b
SW
32 body = b'''<html>
33 <body>
34 <div class="post-container post-post">
170e50a0 35 <div class="post-edit-box">We don't want edit boxes</div>
6409066b 36 This is glowfic
170e50a0 37 <div class="post-footer">We don't want footers</div>
6409066b 38 </div>
a0d30541
SW
39 <div class="flat-post-replies">
40 <div class="post-container post-reply">
170e50a0 41 <div class="post-edit-box">We don't want edit boxes</div>
a0d30541 42 You sure?
170e50a0 43 <div class="post-footer">We don't want footers</div>
a0d30541
SW
44 </div>
45 <div class="post-container post-reply">
46 Pretty sure.
47 </div>
48 </div>
6409066b
SW
49 </body>
50</html>'''
6fdb8f01 51 self.send_response(self._response_code())
b25a2f90
SW
52 self.send_header("Content-type", "text/html")
53 self.send_header("Content-Length", str(len(body)))
54 self.end_headers()
55 self.wfile.write(body)
17bd16e8 56 self._notify_test()
b25a2f90
SW
57
58
59class TestFetch(unittest.TestCase):
681380e2
SW
60 def _port(self) -> int:
61 port = self._web_server.socket.getsockname()[1]
62 assert isinstance(port, int)
63 return port
64
17bd16e8
SW
65 def _count_request(self) -> None:
66 self._request_counter += 1
67
b25a2f90 68 def setUp(self) -> None:
17bd16e8
SW
69 self._request_counter = 0
70 handler = type("Handler", (FakeGlowficHTTPRequestHandler,), {
71 '_notify_test': lambda _: self._count_request()})
72 self._web_server = HTTPServer(('', 0), handler)
681380e2 73 self._thread = threading.Thread(target=self._web_server.serve_forever)
e3ba2056 74 self._thread.start()
b25a2f90
SW
75
76 def tearDown(self) -> None:
681380e2 77 self._web_server.shutdown()
e3ba2056 78 self._thread.join()
971e6658 79 self._web_server.server_close()
b25a2f90
SW
80
81 def testFetch(self) -> None:
e138a9b4
SW
82 with requests.session() as s:
83 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
84 self.assertEqual(self._request_counter, 1)
85 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
86 self.assertEqual(self._request_counter, 2)
b25a2f90 87
b34a368f
SW
88 def testFetchCaching(self) -> None:
89 with requests_cache.CachedSession() as s:
90 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
91 self.assertEqual(self._request_counter, 1)
92 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
93 self.assertEqual(self._request_counter, 1)
94
de7251fc
SW
95 def testFetchPersistentCaching(self) -> None:
96 with requests_cache.CachedSession() as s:
97 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
98 self.assertEqual(self._request_counter, 1)
99 with requests_cache.CachedSession() as s:
100 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
101 self.assertEqual(self._request_counter, 1)
102
a2d42468 103 def testReplies(self) -> None:
136277e3 104 with requests.session() as s:
a2d42468
SW
105 replies = paperdoorknob.replies(
106 paperdoorknob.clean(
107 paperdoorknob.fetch(
108 f"http://localhost:{self._port()}",
109 s,
110 TIMEOUT)))
47cfa3cd 111 self.assertEqual([r.text.strip() for r in replies],
55958ec0 112 ["This is glowfic", "You sure?", "Pretty sure."])
136277e3 113
6fdb8f01 114 def testFetchErrors(self) -> None:
e138a9b4
SW
115 with requests.session() as s:
116 with self.assertRaises(requests.HTTPError):
117 paperdoorknob.fetch(
118 f"http://localhost:{self._port()}/not_found", s, TIMEOUT)
119 with self.assertRaises(requests.HTTPError):
120 paperdoorknob.fetch(
121 f"http://localhost:{self._port()}/server_error", s, TIMEOUT)
6fdb8f01 122
36ae1d5f
SW
123 def testProcess(self) -> None:
124 with requests.session() as s:
125 buf = io.BytesIO()
126 paperdoorknob.process(
127 f"http://localhost:{self._port()}",
128 s,
129 TIMEOUT,
130 buf,
131 'pandoc')
132 self.assertEqual(buf.getvalue(),
133 b'This is glowfic\nYou sure?\nPretty sure.\n')
134
b25a2f90
SW
135
136if __name__ == '__main__':
137 unittest.main()