-
Notifications
You must be signed in to change notification settings - Fork 43
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #263 from betaboon/refactor-split-modules
Refactor: split modules
- Loading branch information
Showing
12 changed files
with
637 additions
and
590 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
Oops, something went wrong.