Skip to content

Commit

Permalink
refactor: move MocketEntry from mocket.mocket to mocket.entry
Browse files Browse the repository at this point in the history
  • Loading branch information
betaboon committed Nov 17, 2024
1 parent 2e9b640 commit 1df405c
Show file tree
Hide file tree
Showing 5 changed files with 65 additions and 58 deletions.
3 changes: 2 additions & 1 deletion mocket/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
from mocket.async_mocket import async_mocketize
from mocket.mocket import FakeSSLContext, Mocket, MocketEntry
from mocket.entry import MocketEntry
from mocket.mocket import FakeSSLContext, Mocket
from mocket.mocketizer import Mocketizer, mocketize

__all__ = (
Expand Down
59 changes: 59 additions & 0 deletions mocket/entry.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
import collections.abc

from mocket.compat import encode_to_bytes


class MocketEntry:
class Response(bytes):
@property
def data(self):
return self

response_index = 0
request_cls = bytes
response_cls = Response
responses = None
_served = None

def __init__(self, location, responses):
self._served = False
self.location = location

if not isinstance(responses, collections.abc.Iterable):
responses = [responses]

if not responses:
self.responses = [self.response_cls(encode_to_bytes(""))]
else:
self.responses = []
for r in responses:
if not isinstance(r, BaseException) and not getattr(r, "data", False):
if isinstance(r, str):
r = encode_to_bytes(r)
r = self.response_cls(r)
self.responses.append(r)

def __repr__(self):
return f"{self.__class__.__name__}(location={self.location})"

@staticmethod
def can_handle(data):
return True

def collect(self, data):
from mocket import Mocket

req = self.request_cls(data)
Mocket.collect(req)

def get_response(self):
response = self.responses[self.response_index]
if self.response_index < len(self.responses) - 1:
self.response_index += 1

self._served = True

if isinstance(response, BaseException):
raise response

return response.data
55 changes: 0 additions & 55 deletions mocket/mocket.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import collections
import collections.abc as collections_abc
import contextlib
import errno
import hashlib
Expand Down Expand Up @@ -588,57 +587,3 @@ def assert_fail_if_entries_not_served(cls):
"""Mocket checks that all entries have been served at least once."""
if not all(entry._served for entry in itertools.chain(*cls._entries.values())):
raise AssertionError("Some Mocket entries have not been served")


class MocketEntry:
class Response(bytes):
@property
def data(self):
return self

response_index = 0
request_cls = bytes
response_cls = Response
responses = None
_served = None

def __init__(self, location, responses):
self._served = False
self.location = location

if not isinstance(responses, collections_abc.Iterable):
responses = [responses]

if not responses:
self.responses = [self.response_cls(encode_to_bytes(""))]
else:
self.responses = []
for r in responses:
if not isinstance(r, BaseException) and not getattr(r, "data", False):
if isinstance(r, str):
r = encode_to_bytes(r)
r = self.response_cls(r)
self.responses.append(r)

def __repr__(self):
return f"{self.__class__.__name__}(location={self.location})"

@staticmethod
def can_handle(data):
return True

def collect(self, data):
req = self.request_cls(data)
Mocket.collect(req)

def get_response(self):
response = self.responses[self.response_index]
if self.response_index < len(self.responses) - 1:
self.response_index += 1

self._served = True

if isinstance(response, BaseException):
raise response

return response.data
3 changes: 2 additions & 1 deletion mocket/mockhttp.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@
from h11 import Request as H11Request

from mocket.compat import ENCODING, decode_from_bytes, do_the_magic, encode_to_bytes
from mocket.mocket import Mocket, MocketEntry
from mocket.entry import MocketEntry
from mocket.mocket import Mocket

STATUS = {k: v[0] for k, v in BaseHTTPRequestHandler.responses.items()}
CRLF = "\r\n"
Expand Down
3 changes: 2 additions & 1 deletion mocket/mockredis.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@
encode_to_bytes,
shsplit,
)
from mocket.mocket import Mocket, MocketEntry
from mocket.entry import MocketEntry
from mocket.mocket import Mocket


class Request:
Expand Down

0 comments on commit 1df405c

Please sign in to comment.