From 17bd16e89158461d82af7cab2a99a36063ff46b1 Mon Sep 17 00:00:00 2001 From: Scott Worley Date: Thu, 23 Nov 2023 13:09:18 -0800 Subject: [PATCH] fetch: test: Count requests --- paperdoorknob_test.py | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) 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): -- 2.44.1