]> git.scottworley.com Git - paperdoorknob/blobdiff - paperdoorknob_test.py
fetch: Cache
[paperdoorknob] / paperdoorknob_test.py
index 400570f99d33ec62a487a4715250e4e977765950..e966a6b5b0385789b593566531e3ead27e1a49fd 100644 (file)
@@ -9,6 +9,7 @@ import unittest
 import threading
 from http.server import BaseHTTPRequestHandler, HTTPServer
 import requests
+import requests_cache
 import paperdoorknob
 
 TIMEOUT = 8
@@ -59,18 +60,27 @@ class TestFetch(unittest.TestCase):
         self._web_server.server_close()
 
     def testFetch(self) -> None:
-        paperdoorknob.fetch(f"http://localhost:{self._port()}", TIMEOUT)
-        self.assertEqual(self._request_counter, 1)
-        paperdoorknob.fetch(f"http://localhost:{self._port()}", TIMEOUT)
-        self.assertEqual(self._request_counter, 2)
+        with requests.session() as s:
+            paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
+            self.assertEqual(self._request_counter, 1)
+            paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
+            self.assertEqual(self._request_counter, 2)
+
+    def testFetchCaching(self) -> None:
+        with requests_cache.CachedSession() as s:
+            paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
+            self.assertEqual(self._request_counter, 1)
+            paperdoorknob.fetch(f"http://localhost:{self._port()}", s, TIMEOUT)
+            self.assertEqual(self._request_counter, 1)
 
     def testFetchErrors(self) -> None:
-        with self.assertRaises(requests.HTTPError):
-            paperdoorknob.fetch(
-                f"http://localhost:{self._port()}/not_found", TIMEOUT)
-        with self.assertRaises(requests.HTTPError):
-            paperdoorknob.fetch(
-                f"http://localhost:{self._port()}/server_error", TIMEOUT)
+        with requests.session() as s:
+            with self.assertRaises(requests.HTTPError):
+                paperdoorknob.fetch(
+                    f"http://localhost:{self._port()}/not_found", s, TIMEOUT)
+            with self.assertRaises(requests.HTTPError):
+                paperdoorknob.fetch(
+                    f"http://localhost:{self._port()}/server_error", s, TIMEOUT)
 
 
 if __name__ == '__main__':