From: Scott Worley Date: Thu, 23 Nov 2023 21:09:18 +0000 (-0800) Subject: fetch: test: Count requests X-Git-Url: http://git.scottworley.com/paperdoorknob/commitdiff_plain/17bd16e89158461d82af7cab2a99a36063ff46b1?ds=sidebyside;hp=971e665870ae3d1f38e270e716364dd2ff3dd5a2 fetch: test: Count requests --- diff --git a/paperdoorknob_test.py b/paperdoorknob_test.py index c2fce2b..400570f 100644 --- a/paperdoorknob_test.py +++ b/paperdoorknob_test.py @@ -16,6 +16,9 @@ TIMEOUT = 8 class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler): + def _notify_test(self) -> None: + raise NotImplementedError() + def _response_code(self) -> int: if self.path == "/not_found": return 404 @@ -30,6 +33,7 @@ class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler): self.send_header("Content-Length", str(len(body))) self.end_headers() self.wfile.write(body) + self._notify_test() class TestFetch(unittest.TestCase): @@ -38,8 +42,14 @@ class TestFetch(unittest.TestCase): assert isinstance(port, int) return port + def _count_request(self) -> None: + self._request_counter += 1 + def setUp(self) -> None: - self._web_server = HTTPServer(('', 0), FakeGlowficHTTPRequestHandler) + self._request_counter = 0 + handler = type("Handler", (FakeGlowficHTTPRequestHandler,), { + '_notify_test': lambda _: self._count_request()}) + self._web_server = HTTPServer(('', 0), handler) self._thread = threading.Thread(target=self._web_server.serve_forever) self._thread.start() @@ -50,6 +60,9 @@ class TestFetch(unittest.TestCase): 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) def testFetchErrors(self) -> None: with self.assertRaises(requests.HTTPError):