diff --git a/pytest_hoverfly/base.py b/pytest_hoverfly/base.py index 51daf07..c628f46 100644 --- a/pytest_hoverfly/base.py +++ b/pytest_hoverfly/base.py @@ -2,6 +2,7 @@ import dataclasses as dc import os +import socket import time import typing as t import urllib.error @@ -50,12 +51,23 @@ def try_from_env(cls, env: t.Mapping[str, str]) -> t.Optional[Hoverfly]: return Hoverfly(hoverfly_host, int(admin_port), int(proxy_port)) def is_ready(self) -> bool: + return self.admin_endpoint_is_ready() and self.proxy_is_ready() + + def admin_endpoint_is_ready(self): try: urllib.request.urlopen(f"{self.admin_endpoint}/state") return True except (urllib.error.URLError, RemoteDisconnected): return False + def proxy_is_ready(self): + try: + s = socket.socket(socket.AF_INET, socket.SOCK_STREAM) + s.connect((self.host, self.proxy_port)) + return True + except ConnectionRefusedError: + return False + def get_container( container_name: t.Optional[str] = None,