]> git.scottworley.com Git - paperdoorknob/blame - paperdoorknob_test.py
fetch: Honor Cache-Control headers
[paperdoorknob] / paperdoorknob_test.py
CommitLineData
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
8import unittest
9import threading
10from http.server import BaseHTTPRequestHandler, HTTPServer
6fdb8f01 11import requests
b34a368f 12import requests_cache
b25a2f90
SW
13import paperdoorknob
14
b25a2f90
SW
15TIMEOUT = 8
16
17
18class 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
SW
30 def do_GET(self) -> None:
31 body = b'<html><body>This is glowfic</body></html>'
6fdb8f01 32 self.send_response(self._response_code())
b25a2f90
SW
33 self.send_header("Content-type", "text/html")
34 self.send_header("Content-Length", str(len(body)))
35 self.end_headers()
36 self.wfile.write(body)
17bd16e8 37 self._notify_test()
b25a2f90
SW
38
39
40class TestFetch(unittest.TestCase):
681380e2
SW
41 def _port(self) -> int:
42 port = self._web_server.socket.getsockname()[1]
43 assert isinstance(port, int)
44 return port
45
17bd16e8
SW
46 def _count_request(self) -> None:
47 self._request_counter += 1
48
b25a2f90 49 def setUp(self) -> None:
17bd16e8
SW
50 self._request_counter = 0
51 handler = type("Handler", (FakeGlowficHTTPRequestHandler,), {
52 '_notify_test': lambda _: self._count_request()})
53 self._web_server = HTTPServer(('', 0), handler)
681380e2 54 self._thread = threading.Thread(target=self._web_server.serve_forever)
e3ba2056 55 self._thread.start()
b25a2f90
SW
56
57 def tearDown(self) -> None:
681380e2 58 self._web_server.shutdown()
e3ba2056 59 self._thread.join()
971e6658 60 self._web_server.server_close()
b25a2f90
SW
61
62 def testFetch(self) -> None:
e138a9b4
SW
63 with requests.session() as s:
64 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
65 self.assertEqual(self._request_counter, 1)
66 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
67 self.assertEqual(self._request_counter, 2)
b25a2f90 68
b34a368f
SW
69 def testFetchCaching(self) -> None:
70 with requests_cache.CachedSession() as s:
71 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
72 self.assertEqual(self._request_counter, 1)
73 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
74 self.assertEqual(self._request_counter, 1)
75
de7251fc
SW
76 def testFetchPersistentCaching(self) -> None:
77 with requests_cache.CachedSession() as s:
78 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
79 self.assertEqual(self._request_counter, 1)
80 with requests_cache.CachedSession() as s:
81 paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
82 self.assertEqual(self._request_counter, 1)
83
6fdb8f01 84 def testFetchErrors(self) -> None:
e138a9b4
SW
85 with requests.session() as s:
86 with self.assertRaises(requests.HTTPError):
87 paperdoorknob.fetch(
88 f"http://localhost:{self._port()}/not_found", s, TIMEOUT)
89 with self.assertRaises(requests.HTTPError):
90 paperdoorknob.fetch(
91 f"http://localhost:{self._port()}/server_error", s, TIMEOUT)
6fdb8f01 92
b25a2f90
SW
93
94if __name__ == '__main__':
95 unittest.main()