Skip to content
Closed
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
8 changes: 7 additions & 1 deletion mocket/async_mocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,17 @@ async def wrapper(
truesocket_recording_dir=None,
strict_mode=False,
strict_mode_allowed=None,
skip_response_cache=False,
*args,
**kwargs,
):
async with Mocketizer.factory(
test, truesocket_recording_dir, strict_mode, strict_mode_allowed, args
test,
truesocket_recording_dir,
strict_mode,
strict_mode_allowed,
skip_response_cache,
args,
):
return await test(*args, **kwargs)

Expand Down
2 changes: 2 additions & 0 deletions mocket/inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
def enable(
namespace: str | None = None,
truesocket_recording_dir: str | None = None,
skip_response_cache: bool = False,
) -> None:
from mocket.mocket import Mocket
from mocket.socket import (
Expand All @@ -33,6 +34,7 @@ def enable(

Mocket._namespace = namespace
Mocket._truesocket_recording_dir = truesocket_recording_dir
Mocket._skip_response_cache = skip_response_cache

if truesocket_recording_dir and not os.path.isdir(truesocket_recording_dir):
# JSON dumps will be saved here
Expand Down
5 changes: 5 additions & 0 deletions mocket/mocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ class Mocket:
_requests: ClassVar[list] = []
_namespace: ClassVar[str] = str(id(_entries))
_truesocket_recording_dir: ClassVar[str | None] = None
_skip_response_cache: bool = False

enable = mocket.inject.enable
disable = mocket.inject.disable
Expand Down Expand Up @@ -96,6 +97,10 @@ def get_namespace(cls) -> str:
def get_truesocket_recording_dir(cls) -> str | None:
return cls._truesocket_recording_dir

@classmethod
def get_skip_response_cache(cls) -> bool:
return cls._skip_response_cache

@classmethod
def assert_fail_if_entries_not_served(cls) -> None:
"""Mocket checks that all entries have been served at least once."""
Expand Down
21 changes: 19 additions & 2 deletions mocket/mocketizer.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ def __init__(
truesocket_recording_dir=None,
strict_mode=False,
strict_mode_allowed=None,
skip_response_cache=False,
):
self.instance = instance
self.truesocket_recording_dir = truesocket_recording_dir
self.skip_response_cache = skip_response_cache
self.namespace = namespace or str(id(self))
MocketMode().STRICT = strict_mode
if strict_mode:
Expand All @@ -27,6 +29,7 @@ def enter(self):
Mocket.enable(
namespace=self.namespace,
truesocket_recording_dir=self.truesocket_recording_dir,
skip_response_cache=self.skip_response_cache,
)
if self.instance:
self.check_and_call("mocketize_setup")
Expand Down Expand Up @@ -57,7 +60,14 @@ def check_and_call(self, method_name):
method()

@staticmethod
def factory(test, truesocket_recording_dir, strict_mode, strict_mode_allowed, args):
def factory(
test,
truesocket_recording_dir,
strict_mode,
strict_mode_allowed,
skip_response_cache,
args,
):
instance = args[0] if args else None
namespace = None
if truesocket_recording_dir:
Expand All @@ -75,6 +85,7 @@ def factory(test, truesocket_recording_dir, strict_mode, strict_mode_allowed, ar
truesocket_recording_dir=truesocket_recording_dir,
strict_mode=strict_mode,
strict_mode_allowed=strict_mode_allowed,
skip_response_cache=skip_response_cache,
)


Expand All @@ -83,11 +94,17 @@ def wrapper(
truesocket_recording_dir=None,
strict_mode=False,
strict_mode_allowed=None,
skip_response_cache=False,
*args,
**kwargs,
):
with Mocketizer.factory(
test, truesocket_recording_dir, strict_mode, strict_mode_allowed, args
test,
truesocket_recording_dir,
strict_mode,
strict_mode_allowed,
skip_response_cache,
args,
):
return test(*args, **kwargs)

Expand Down
8 changes: 6 additions & 2 deletions mocket/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
import os
import select
import socket
import uuid
from json.decoder import JSONDecodeError
from types import TracebackType
from typing import Any, Type
Expand Down Expand Up @@ -252,7 +253,10 @@ def true_sendall(self, data: ReadableBuffer, *args: Any, **kwargs: Any) -> int:

req = decode_from_bytes(data)
# make request unique again
req_signature = _hash_request(hasher, req)
req_signature_source = (
str(uuid.uuid4()) if Mocket.get_skip_response_cache() else req
)
req_signature = _hash_request(hasher, req_signature_source)
# port should be always a string
port = str(self._port)

Expand All @@ -278,7 +282,7 @@ def true_sendall(self, data: ReadableBuffer, *args: Any, **kwargs: Any) -> int:
except KeyError:
if hasher is not hashlib.md5:
# Fallback for backwards compatibility
req_signature = _hash_request(hashlib.md5, req)
req_signature = _hash_request(hashlib.md5, req_signature_source)
response_dict = responses[self._host][port][req_signature]
else:
raise
Expand Down
19 changes: 19 additions & 0 deletions tests/test_http.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,25 @@ def test_truesendall_with_chunk_recording(self):

assert len(responses["httpbin.local"]["80"].keys()) == 1

def test_truesendall_with_recording_and_skip_response_cache(self):
with tempfile.TemporaryDirectory() as temp_dir, Mocketizer(
truesocket_recording_dir=temp_dir,
skip_response_cache=True,
):
url = "http://httpbin.local/ip"

requests.get(url)
requests.get(url)

dump_filename = os.path.join(
Mocket.get_truesocket_recording_dir(),
Mocket.get_namespace() + ".json",
)
with open(dump_filename) as f:
responses = json.load(f)

self.assertEqual(len(responses["httpbin.local"]["80"].keys()), 2)

@mocketize
def test_wrongpath_truesendall(self):
Entry.register(
Expand Down
Loading