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