]> git.scottworley.com Git - paperdoorknob/blobdiff - fetch.py
FakeFetcher for faster tests
[paperdoorknob] / fetch.py
index 3c0c6a3aa9c47bc63999e0adde5cb750df8c85e6..7776f93085943ef8f04948858eeadb0d69323d86 100644 (file)
--- a/fetch.py
+++ b/fetch.py
@@ -41,3 +41,14 @@ def DirectFetcher(timeout: int) -> Iterator[_SessionFetcher]:
 def CachingFetcher(cache_path: str, timeout: int) -> Iterator[_SessionFetcher]:
     with requests_cache.CachedSession(cache_path, cache_control=True) as session:
         yield _SessionFetcher(session, timeout)
+
+
+class FakeFetcher(Fetcher):
+
+    def __init__(self, resources: dict[str, bytes]) -> None:
+        self._resources = resources
+
+    def fetch(self, url: str) -> bytes:
+        if url not in self._resources:
+            raise requests.HTTPError("URL not found")
+        return self._resources[url]