Skip to content

Commit

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

__all__ = (
"async_mocketize",
Expand Down
57 changes: 1 addition & 56 deletions mocket/mocket.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
from mocket.compat import decode_from_bytes, encode_to_bytes
from mocket.io import MocketSocketCore
from mocket.mode import MocketMode
from mocket.ssl import FakeSSLContext
from mocket.utils import hexdump, hexload

xxh32 = None
Expand Down Expand Up @@ -59,62 +60,6 @@
true_urllib3_match_hostname = urllib3_match_hostname


class SuperFakeSSLContext:
"""For Python 3.6 and newer."""

class FakeSetter(int):
def __set__(self, *args):
pass

minimum_version = FakeSetter()
options = FakeSetter()
verify_mode = FakeSetter()
verify_flags = FakeSetter()


class FakeSSLContext(SuperFakeSSLContext):
DUMMY_METHODS = (
"load_default_certs",
"load_verify_locations",
"set_alpn_protocols",
"set_ciphers",
"set_default_verify_paths",
)
sock = None
post_handshake_auth = None
_check_hostname = False

@property
def check_hostname(self):
return self._check_hostname

@check_hostname.setter
def check_hostname(self, _):
self._check_hostname = False

def __init__(self, *args, **kwargs):
self._set_dummy_methods()

def _set_dummy_methods(self):
def dummy_method(*args, **kwargs):
pass

for m in self.DUMMY_METHODS:
setattr(self, m, dummy_method)

@staticmethod
def wrap_socket(sock, *args, **kwargs):
sock.kwargs = kwargs
sock._secure_socket = True
return sock

@staticmethod
def wrap_bio(incoming, outcoming, *args, **kwargs):
ssl_obj = MocketSocket()
ssl_obj._host = kwargs["server_hostname"]
return ssl_obj


def create_connection(address, timeout=None, source_address=None):
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
if timeout:
Expand Down
56 changes: 56 additions & 0 deletions mocket/ssl.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
class SuperFakeSSLContext:
"""For Python 3.6 and newer."""

class FakeSetter(int):
def __set__(self, *args):
pass

minimum_version = FakeSetter()
options = FakeSetter()
verify_mode = FakeSetter()
verify_flags = FakeSetter()


class FakeSSLContext(SuperFakeSSLContext):
DUMMY_METHODS = (
"load_default_certs",
"load_verify_locations",
"set_alpn_protocols",
"set_ciphers",
"set_default_verify_paths",
)
sock = None
post_handshake_auth = None
_check_hostname = False

@property
def check_hostname(self):
return self._check_hostname

@check_hostname.setter
def check_hostname(self, _):
self._check_hostname = False

def __init__(self, *args, **kwargs):
self._set_dummy_methods()

def _set_dummy_methods(self):
def dummy_method(*args, **kwargs):
pass

for m in self.DUMMY_METHODS:
setattr(self, m, dummy_method)

@staticmethod
def wrap_socket(sock, *args, **kwargs):
sock.kwargs = kwargs
sock._secure_socket = True
return sock

@staticmethod
def wrap_bio(incoming, outcoming, *args, **kwargs):
from mocket.mocket import MocketSocket

ssl_obj = MocketSocket()
ssl_obj._host = kwargs["server_hostname"]
return ssl_obj

0 comments on commit 012df13

Please sign in to comment.