Skip to content

Commit

Permalink
Merge pull request #230 from mindflayer/bugfix/strict-mode-allowed
Browse files Browse the repository at this point in the history
Strict mode check should accept a location as `str`
  • Loading branch information
mindflayer authored Apr 26, 2024
2 parents 5aecbb5 + aa14094 commit 186689e
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 8 deletions.
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

0 comments on commit 186689e

Please sign in to comment.