diff --git a/mocket/__init__.py b/mocket/__init__.py index 30ec55a7..d64cb11d 100644 --- a/mocket/__init__.py +++ b/mocket/__init__.py @@ -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", diff --git a/mocket/mocket.py b/mocket/mocket.py index fb7ec8a0..8f791ea3 100644 --- a/mocket/mocket.py +++ b/mocket/mocket.py @@ -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 @@ -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: diff --git a/mocket/ssl.py b/mocket/ssl.py new file mode 100644 index 00000000..2e367f16 --- /dev/null +++ b/mocket/ssl.py @@ -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