From aa140948ca2899c32888294edf8bed61b3b4035b Mon Sep 17 00:00:00 2001 From: Giorgio Salluzzo Date: Fri, 26 Apr 2024 19:55:39 +0200 Subject: [PATCH] Strict mode check should accept a location as `str`. --- mocket/utils.py | 11 ++++++++--- pyproject.toml | 2 +- tests/main/test_mode.py | 9 +++++---- 3 files changed, 14 insertions(+), 8 deletions(-) diff --git a/mocket/utils.py b/mocket/utils.py index 2f17838b..5a0a4208 100644 --- a/mocket/utils.py +++ b/mocket/utils.py @@ -57,12 +57,17 @@ def __init__(self): def is_allowed(self, location: Union[str, Tuple[str, int]]) -> bool: """ Checks if (`host`, `port`) or at least `host` - are allowed locationsto perform real `socket` calls + are allowed locations to perform real `socket` calls """ if not self.STRICT: return True - host, _ = location - return location in self.STRICT_ALLOWED or host in self.STRICT_ALLOWED + try: + host, _ = location + except ValueError: + host = None + return location in self.STRICT_ALLOWED or ( + host is not None and host in self.STRICT_ALLOWED + ) @staticmethod def raise_not_allowed(): diff --git a/pyproject.toml b/pyproject.toml index e8f58b6b..2d482f25 100644 --- a/pyproject.toml +++ b/pyproject.toml @@ -1,5 +1,5 @@ [build-system] -requires = ["hatchling"] +requires = ["hatchling>=1.22.2"] build-backend = "hatchling.build" [project] diff --git a/tests/main/test_mode.py b/tests/main/test_mode.py index 0d2d2e7c..746d0cab 100644 --- a/tests/main/test_mode.py +++ b/tests/main/test_mode.py @@ -65,7 +65,8 @@ def test_strict_mode_false_with_allowed_hosts(): Mocketizer(strict_mode=False, strict_mode_allowed=["foobar.local"]) -def test_strict_mode_false_always_allowed(): - with Mocketizer(strict_mode=False): - assert MocketMode().is_allowed("foobar.com") - assert MocketMode().is_allowed(("foobar.com", 443)) +@pytest.mark.parametrize("strict_mode_on", (False, True)) +def test_strict_mode_allowed_or_not(strict_mode_on): + with Mocketizer(strict_mode=strict_mode_on): + assert MocketMode().is_allowed("foobar.com") is not strict_mode_on + assert MocketMode().is_allowed(("foobar.com", 443)) is not strict_mode_on