]>
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 threading | |
10 | from http.server import BaseHTTPRequestHandler, HTTPServer | |
11 | import requests | |
12 | import requests_cache | |
13 | import paperdoorknob | |
14 | ||
15 | TIMEOUT = 8 | |
16 | ||
17 | ||
18 | class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler): | |
19 | ||
20 | def _notify_test(self) -> None: | |
21 | raise NotImplementedError() | |
22 | ||
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 | ||
30 | def do_GET(self) -> None: | |
31 | body = b'''<html> | |
32 | <body> | |
33 | <div class="post-container post-post"> | |
34 | This is glowfic | |
35 | </div> | |
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> | |
44 | </body> | |
45 | </html>''' | |
46 | self.send_response(self._response_code()) | |
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) | |
51 | self._notify_test() | |
52 | ||
53 | ||
54 | class TestFetch(unittest.TestCase): | |
55 | def _port(self) -> int: | |
56 | port = self._web_server.socket.getsockname()[1] | |
57 | assert isinstance(port, int) | |
58 | return port | |
59 | ||
60 | def _count_request(self) -> None: | |
61 | self._request_counter += 1 | |
62 | ||
63 | def setUp(self) -> None: | |
64 | self._request_counter = 0 | |
65 | handler = type("Handler", (FakeGlowficHTTPRequestHandler,), { | |
66 | '_notify_test': lambda _: self._count_request()}) | |
67 | self._web_server = HTTPServer(('', 0), handler) | |
68 | self._thread = threading.Thread(target=self._web_server.serve_forever) | |
69 | self._thread.start() | |
70 | ||
71 | def tearDown(self) -> None: | |
72 | self._web_server.shutdown() | |
73 | self._thread.join() | |
74 | self._web_server.server_close() | |
75 | ||
76 | def testFetch(self) -> None: | |
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) | |
82 | ||
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 | ||
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 | ||
98 | def testFetchConents(self) -> None: | |
99 | with requests.session() as s: | |
100 | post = paperdoorknob.Post(paperdoorknob.fetch( | |
101 | f"http://localhost:{self._port()}", s, TIMEOUT)) | |
102 | self.assertEqual(post.text().text.strip(), "This is glowfic") | |
103 | self.assertEqual([r.text.strip() for r in post.replies()], | |
104 | ["You sure?", "Pretty sure."]) | |
105 | ||
106 | def testFetchErrors(self) -> None: | |
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) | |
114 | ||
115 | ||
116 | if __name__ == '__main__': | |
117 | unittest.main() |