Skip to content

Commit

Permalink
refactor: convert MocketSSLContext.wrap_socket and wrap_bio to instan…
Browse files Browse the repository at this point in the history
…ce-methods
  • Loading branch information
betaboon committed Nov 25, 2024
1 parent ebe7b91 commit 1cce25a
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 5 deletions.
4 changes: 2 additions & 2 deletions mocket/inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
Expand All @@ -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,
Expand Down
19 changes: 16 additions & 3 deletions mocket/ssl/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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)

0 comments on commit 1cce25a

Please sign in to comment.