Skip to content

Commit 186689e

Browse files
authored
Merge pull request #230 from mindflayer/bugfix/strict-mode-allowed
Strict mode check should accept a location as `str`
2 parents 5aecbb5 + aa14094 commit 186689e

File tree

3 files changed

+14
-8
lines changed

3 files changed

+14
-8
lines changed

mocket/utils.py

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -57,12 +57,17 @@ def __init__(self):
5757
def is_allowed(self, location: Union[str, Tuple[str, int]]) -> bool:
5858
"""
5959
Checks if (`host`, `port`) or at least `host`
60-
are allowed locationsto perform real `socket` calls
60+
are allowed locations to perform real `socket` calls
6161
"""
6262
if not self.STRICT:
6363
return True
64-
host, _ = location
65-
return location in self.STRICT_ALLOWED or host in self.STRICT_ALLOWED
64+
try:
65+
host, _ = location
66+
except ValueError:
67+
host = None
68+
return location in self.STRICT_ALLOWED or (
69+
host is not None and host in self.STRICT_ALLOWED
70+
)
6671

6772
@staticmethod
6873
def raise_not_allowed():

pyproject.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[build-system]
2-
requires = ["hatchling"]
2+
requires = ["hatchling>=1.22.2"]
33
build-backend = "hatchling.build"
44

55
[project]

tests/main/test_mode.py

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,8 @@ def test_strict_mode_false_with_allowed_hosts():
6565
Mocketizer(strict_mode=False, strict_mode_allowed=["foobar.local"])
6666

6767

68-
def test_strict_mode_false_always_allowed():
69-
with Mocketizer(strict_mode=False):
70-
assert MocketMode().is_allowed("foobar.com")
71-
assert MocketMode().is_allowed(("foobar.com", 443))
68+
@pytest.mark.parametrize("strict_mode_on", (False, True))
69+
def test_strict_mode_allowed_or_not(strict_mode_on):
70+
with Mocketizer(strict_mode=strict_mode_on):
71+
assert MocketMode().is_allowed("foobar.com") is not strict_mode_on
72+
assert MocketMode().is_allowed(("foobar.com", 443)) is not strict_mode_on

0 commit comments

Comments
 (0)