Skip to content

Commit 1fa45dd

Browse files
committed
Make it more HTTP-agnostic.
1 parent e2f51b8 commit 1fa45dd

File tree

4 files changed

+11
-8
lines changed

4 files changed

+11
-8
lines changed

mocket/decorators/mocketizer.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,9 @@ def __init__(
1515
self.instance = instance
1616
self.truesocket_recording_dir = truesocket_recording_dir
1717
self.namespace = namespace or str(id(self))
18-
MocketMode().STRICT = strict_mode
18+
MocketMode.STRICT = strict_mode
1919
if strict_mode:
20-
MocketMode().STRICT_ALLOWED = strict_mode_allowed or []
20+
MocketMode.STRICT_ALLOWED = strict_mode_allowed or []
2121
elif strict_mode_allowed:
2222
raise ValueError(
2323
"Allowed locations are only accepted when STRICT mode is active."

mocket/mode.py

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
from typing import NoReturn
1010

1111

12-
class MocketMode:
12+
class _MocketMode:
1313
__shared_state: ClassVar[dict[str, Any]] = {}
1414
STRICT: ClassVar = None
1515
STRICT_ALLOWED: ClassVar = None
@@ -52,7 +52,10 @@ def raise_not_allowed(
5252
from mocket.compat import decode_from_bytes
5353

5454
preview = decode_from_bytes(data).split("\r\n", 1)[0][:200]
55-
msg += f"\nFirst request line: {preview}"
55+
msg += f"\nSent data: {preview}"
5656

5757
msg += f"\nRegistered entries:\n{formatted_entries}"
5858
raise StrictMocketException(msg)
59+
60+
61+
MocketMode = _MocketMode()

mocket/socket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -228,7 +228,7 @@ def recv(self, buffersize: int, flags: int | None = None) -> bytes:
228228
raise exc
229229

230230
def true_sendall(self, data: bytes, *args: Any, **kwargs: Any) -> bytes:
231-
if not MocketMode().is_allowed(self._address):
231+
if not MocketMode.is_allowed(self._address):
232232
MocketMode.raise_not_allowed(self._address, data)
233233

234234
# try to get the response from recordings

tests/test_mode.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def test_strict_mode_error_message():
5353
== """
5454
Mocket tried to use the real `socket` module while STRICT mode was active.
5555
Attempted address: httpbin.local:80
56-
First request line: GET /ip HTTP/1.1
56+
Sent data: GET /ip HTTP/1.1
5757
Registered entries:
5858
('httpbin.local', 80):
5959
Entry(method='GET', schema='http', location=('httpbin.local', 80), path='/user.agent', query='')
@@ -69,5 +69,5 @@ def test_strict_mode_false_with_allowed_hosts():
6969
@pytest.mark.parametrize("strict_mode_on", (False, True))
7070
def test_strict_mode_allowed_or_not(strict_mode_on):
7171
with Mocketizer(strict_mode=strict_mode_on):
72-
assert MocketMode().is_allowed("foobar.com") is not strict_mode_on
73-
assert MocketMode().is_allowed(("foobar.com", 443)) is not strict_mode_on
72+
assert MocketMode.is_allowed("foobar.com") is not strict_mode_on
73+
assert MocketMode.is_allowed(("foobar.com", 443)) is not strict_mode_on

0 commit comments

Comments
 (0)