Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 8 additions & 3 deletions mocket/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[build-system]
requires = ["hatchling"]
requires = ["hatchling>=1.22.2"]
build-backend = "hatchling.build"

[project]
Expand Down
9 changes: 5 additions & 4 deletions tests/main/test_mode.py
Original file line number Diff line number Diff line change
Expand Up @@ -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