diff --git a/mocket/__init__.py b/mocket/__init__.py index b8f1e032..30ec55a7 100644 --- a/mocket/__init__.py +++ b/mocket/__init__.py @@ -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__ = ( diff --git a/mocket/entry.py b/mocket/entry.py new file mode 100644 index 00000000..8fa28bc7 --- /dev/null +++ b/mocket/entry.py @@ -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 diff --git a/mocket/mocket.py b/mocket/mocket.py index f420c27d..e9bb27e3 100644 --- a/mocket/mocket.py +++ b/mocket/mocket.py @@ -1,5 +1,4 @@ import collections -import collections.abc as collections_abc import contextlib import errno import hashlib @@ -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 diff --git a/mocket/mockhttp.py b/mocket/mockhttp.py index beb312c0..245a11af 100644 --- a/mocket/mockhttp.py +++ b/mocket/mockhttp.py @@ -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" diff --git a/mocket/mockredis.py b/mocket/mockredis.py index 4ed69e1f..fc386e2d 100644 --- a/mocket/mockredis.py +++ b/mocket/mockredis.py @@ -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: