Skip to content

Commit

Permalink
Merge pull request #263 from betaboon/refactor-split-modules
Browse files Browse the repository at this point in the history
Refactor: split modules
  • Loading branch information
mindflayer authored Nov 17, 2024
2 parents 54618df + 14513de commit 2beb8f1
Show file tree
Hide file tree
Showing 12 changed files with 637 additions and 590 deletions.
5 changes: 4 additions & 1 deletion mocket/__init__.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
from mocket.async_mocket import async_mocketize
from mocket.mocket import FakeSSLContext, Mocket, MocketEntry, Mocketizer, mocketize
from mocket.entry import MocketEntry
from mocket.mocket import Mocket
from mocket.mocketizer import Mocketizer, mocketize
from mocket.ssl import FakeSSLContext

__all__ = (
"async_mocketize",
Expand Down
2 changes: 1 addition & 1 deletion mocket/async_mocket.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from mocket.mocket import Mocketizer
from mocket.mocketizer import Mocketizer
from mocket.utils import get_mocketize


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
17 changes: 17 additions & 0 deletions mocket/io.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import io
import os


class MocketSocketCore(io.BytesIO):
def __init__(self, address) -> None:
self._address = address
super().__init__()

def write(self, content):
from mocket import Mocket

super().write(content)

_, w_fd = Mocket.get_pair(self._address)
if w_fd:
os.write(w_fd, content)
Loading

0 comments on commit 2beb8f1

Please sign in to comment.