diff --git a/mocket/inject.py b/mocket/inject.py index 4a9bb5ee..469ab30b 100644 --- a/mocket/inject.py +++ b/mocket/inject.py @@ -36,7 +36,7 @@ def enable( mock_inet_pton, mock_socketpair, ) - from mocket.ssl.context import MocketSSLContext + from mocket.ssl.context import MocketSSLContext, mock_wrap_socket from mocket.urllib3 import ( mock_match_hostname as mock_urllib3_match_hostname, ) @@ -56,7 +56,7 @@ def enable( (socket, "socketpair"): mock_socketpair, # stdlib: ssl (ssl, "SSLContext"): MocketSSLContext, - (ssl, "wrap_socket"): MocketSSLContext.wrap_socket, # python < 3.12.0 + (ssl, "wrap_socket"): mock_wrap_socket, # python < 3.12.0 # urllib3 (urllib3.connection, "match_hostname"): mock_urllib3_match_hostname, (urllib3.connection, "ssl_wrap_socket"): mock_urllib3_ssl_wrap_socket, diff --git a/mocket/ssl/context.py b/mocket/ssl/context.py index 84b848d5..6d5e7307 100644 --- a/mocket/ssl/context.py +++ b/mocket/ssl/context.py @@ -49,12 +49,16 @@ def dummy_method(*args: Any, **kwargs: Any) -> Any: for m in self.DUMMY_METHODS: setattr(self, m, dummy_method) - @staticmethod - def wrap_socket(sock: MocketSocket, *args: Any, **kwargs: Any) -> MocketSSLSocket: + def wrap_socket( + self, + sock: MocketSocket, + *args: Any, + **kwargs: Any, + ) -> MocketSSLSocket: return MocketSSLSocket._create(sock, *args, **kwargs) - @staticmethod def wrap_bio( + self, incoming: Any, # _ssl.MemoryBIO outgoing: Any, # _ssl.MemoryBIO server_side: bool = False, @@ -63,3 +67,12 @@ def wrap_bio( ssl_obj = MocketSSLSocket() ssl_obj._host = server_hostname return ssl_obj + + +def mock_wrap_socket( + sock: MocketSocket, + *args: Any, + **kwargs: Any, +) -> MocketSSLSocket: + context = MocketSSLContext() + return context.wrap_socket(sock, *args, **kwargs)