]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob_test.py
9ea877daa77ffdbe27469abaefc32ace1b493d4a
[paperdoorknob] / paperdoorknob_test.py
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
8 import unittest
9 import io
10 import subprocess
11 import threading
12 from http.server import BaseHTTPRequestHandler, HTTPServer
13 import requests
14 import requests_cache
15 import paperdoorknob
16
17 TIMEOUT = 8
18
19
20 class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler):
21
22 def _notify_test(self) -> None:
23 raise NotImplementedError()
24
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
32 def do_GET(self) -> None:
33 body = b'''<html>
34 <body>
35 <div class="post-container post-post">
36 <div class="post-edit-box">We don't want edit boxes</div>
37 This is glowfic
38 <div class="post-footer">We don't want footers</div>
39 </div>
40 <div class="flat-post-replies">
41 <div class="post-container post-reply">
42 <div class="post-edit-box">We don't want edit boxes</div>
43 You <em>sure</em>?
44 <div class="post-footer">We don't want footers</div>
45 </div>
46 <div class="post-container post-reply">
47 Pretty sure.
48 </div>
49 </div>
50 </body>
51 </html>'''
52 self.send_response(self._response_code())
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)
57 self._notify_test()
58
59
60 class TestFetch(unittest.TestCase):
61 def _port(self) -> int:
62 port = self._web_server.socket.getsockname()[1]
63 assert isinstance(port, int)
64 return port
65
66 def _count_request(self) -> None:
67 self._request_counter += 1
68
69 def setUp(self) -> None:
70 self._request_counter = 0
71 handler = type("Handler", (FakeGlowficHTTPRequestHandler,), {
72 '_notify_test': lambda _: self._count_request()})
73 self._web_server = HTTPServer(('', 0), handler)
74 self._thread = threading.Thread(target=self._web_server.serve_forever)
75 self._thread.start()
76
77 def tearDown(self) -> None:
78 self._web_server.shutdown()
79 self._thread.join()
80 self._web_server.server_close()
81
82 def testFetch(self) -> None:
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)
88
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
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
104 def testReplies(self) -> None:
105 with requests.session() as s:
106 replies = paperdoorknob.replies(
107 paperdoorknob.clean(
108 paperdoorknob.fetch(
109 f"http://localhost:{self._port()}",
110 s,
111 TIMEOUT)))
112 self.assertEqual([r.text.strip() for r in replies],
113 ["This is glowfic", "You sure?", "Pretty sure."])
114
115 def testFetchErrors(self) -> None:
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)
123
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')
133 self.assertEqual(buf.getvalue(), b'''\\documentclass{article}
134 \\begin{document}
135 This is glowfic
136 You \\emph{sure}?
137 Pretty 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)
152
153
154 if __name__ == '__main__':
155 unittest.main()