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