]> git.scottworley.com Git - paperdoorknob/blob - paperdoorknob_test.py
Begin parsing glowfic html
[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 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 </body>
37 </html>'''
38 self.send_response(self._response_code())
39 self.send_header("Content-type", "text/html")
40 self.send_header("Content-Length", str(len(body)))
41 self.end_headers()
42 self.wfile.write(body)
43 self._notify_test()
44
45
46 class TestFetch(unittest.TestCase):
47 def _port(self) -> int:
48 port = self._web_server.socket.getsockname()[1]
49 assert isinstance(port, int)
50 return port
51
52 def _count_request(self) -> None:
53 self._request_counter += 1
54
55 def setUp(self) -> None:
56 self._request_counter = 0
57 handler = type("Handler", (FakeGlowficHTTPRequestHandler,), {
58 '_notify_test': lambda _: self._count_request()})
59 self._web_server = HTTPServer(('', 0), handler)
60 self._thread = threading.Thread(target=self._web_server.serve_forever)
61 self._thread.start()
62
63 def tearDown(self) -> None:
64 self._web_server.shutdown()
65 self._thread.join()
66 self._web_server.server_close()
67
68 def testFetch(self) -> None:
69 with requests.session() as s:
70 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
71 self.assertEqual(self._request_counter, 1)
72 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
73 self.assertEqual(self._request_counter, 2)
74
75 def testFetchCaching(self) -> None:
76 with requests_cache.CachedSession() as s:
77 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
78 self.assertEqual(self._request_counter, 1)
79 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
80 self.assertEqual(self._request_counter, 1)
81
82 def testFetchPersistentCaching(self) -> None:
83 with requests_cache.CachedSession() as s:
84 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
85 self.assertEqual(self._request_counter, 1)
86 with requests_cache.CachedSession() as s:
87 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
88 self.assertEqual(self._request_counter, 1)
89
90 def testFetchConents(self) -> None:
91 with requests.session() as s:
92 post = paperdoorknob.Post(paperdoorknob.fetch(
93 f"http://localhost:{self._port()}", s, TIMEOUT))
94 self.assertEqual(post.text().text.strip(), "This is glowfic")
95
96 def testFetchErrors(self) -> None:
97 with requests.session() as s:
98 with self.assertRaises(requests.HTTPError):
99 paperdoorknob.fetch(
100 f"http://localhost:{self._port()}/not_found", s, TIMEOUT)
101 with self.assertRaises(requests.HTTPError):
102 paperdoorknob.fetch(
103 f"http://localhost:{self._port()}/server_error", s, TIMEOUT)
104
105
106 if __name__ == '__main__':
107 unittest.main()