From: Scott Worley Date: Thu, 23 Nov 2023 20:49:40 +0000 (-0800) Subject: fetch: test: Hold reference to webserver X-Git-Url: http://git.scottworley.com/paperdoorknob/commitdiff_plain/681380e2377aba038aaa22e1992dea54d6697a70?ds=sidebyside;hp=e3ba205699db054723578a32d1bf73276b0e58ad fetch: test: Hold reference to webserver --- diff --git a/paperdoorknob_test.py b/paperdoorknob_test.py index b1d41d4..cf945d6 100644 --- a/paperdoorknob_test.py +++ b/paperdoorknob_test.py @@ -33,27 +33,30 @@ class FakeGlowficHTTPRequestHandler(BaseHTTPRequestHandler): class TestFetch(unittest.TestCase): + def _port(self) -> int: + port = self._web_server.socket.getsockname()[1] + assert isinstance(port, int) + return port + def setUp(self) -> None: - web_server = HTTPServer(('', 0), FakeGlowficHTTPRequestHandler) - self._port = web_server.socket.getsockname()[1] - self._thread = threading.Thread(target=web_server.serve_forever) + self._web_server = HTTPServer(('', 0), FakeGlowficHTTPRequestHandler) + self._thread = threading.Thread(target=self._web_server.serve_forever) self._thread.start() - self._stop_server = web_server.shutdown def tearDown(self) -> None: - self._stop_server() + self._web_server.shutdown() self._thread.join() def testFetch(self) -> None: - paperdoorknob.fetch(f"http://localhost:{self._port}", TIMEOUT) + paperdoorknob.fetch(f"http://localhost:{self._port()}", TIMEOUT) def testFetchErrors(self) -> None: with self.assertRaises(requests.HTTPError): paperdoorknob.fetch( - f"http://localhost:{self._port}/not_found", TIMEOUT) + f"http://localhost:{self._port()}/not_found", TIMEOUT) with self.assertRaises(requests.HTTPError): paperdoorknob.fetch( - f"http://localhost:{self._port}/server_error", TIMEOUT) + f"http://localhost:{self._port()}/server_error", TIMEOUT) if __name__ == '__main__':