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