Skip to content

Commit 1cce25a

Browse files
committed
refactor: convert MocketSSLContext.wrap_socket and wrap_bio to instance-methods
1 parent ebe7b91 commit 1cce25a

File tree

2 files changed

+18
-5
lines changed

2 files changed

+18
-5
lines changed

mocket/inject.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ def enable(
3636
mock_inet_pton,
3737
mock_socketpair,
3838
)
39-
from mocket.ssl.context import MocketSSLContext
39+
from mocket.ssl.context import MocketSSLContext, mock_wrap_socket
4040
from mocket.urllib3 import (
4141
mock_match_hostname as mock_urllib3_match_hostname,
4242
)
@@ -56,7 +56,7 @@ def enable(
5656
(socket, "socketpair"): mock_socketpair,
5757
# stdlib: ssl
5858
(ssl, "SSLContext"): MocketSSLContext,
59-
(ssl, "wrap_socket"): MocketSSLContext.wrap_socket, # python < 3.12.0
59+
(ssl, "wrap_socket"): mock_wrap_socket, # python < 3.12.0
6060
# urllib3
6161
(urllib3.connection, "match_hostname"): mock_urllib3_match_hostname,
6262
(urllib3.connection, "ssl_wrap_socket"): mock_urllib3_ssl_wrap_socket,

mocket/ssl/context.py

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -49,12 +49,16 @@ def dummy_method(*args: Any, **kwargs: Any) -> Any:
4949
for m in self.DUMMY_METHODS:
5050
setattr(self, m, dummy_method)
5151

52-
@staticmethod
53-
def wrap_socket(sock: MocketSocket, *args: Any, **kwargs: Any) -> MocketSSLSocket:
52+
def wrap_socket(
53+
self,
54+
sock: MocketSocket,
55+
*args: Any,
56+
**kwargs: Any,
57+
) -> MocketSSLSocket:
5458
return MocketSSLSocket._create(sock, *args, **kwargs)
5559

56-
@staticmethod
5760
def wrap_bio(
61+
self,
5862
incoming: Any, # _ssl.MemoryBIO
5963
outgoing: Any, # _ssl.MemoryBIO
6064
server_side: bool = False,
@@ -63,3 +67,12 @@ def wrap_bio(
6367
ssl_obj = MocketSSLSocket()
6468
ssl_obj._host = server_hostname
6569
return ssl_obj
70+
71+
72+
def mock_wrap_socket(
73+
sock: MocketSocket,
74+
*args: Any,
75+
**kwargs: Any,
76+
) -> MocketSSLSocket:
77+
context = MocketSSLContext()
78+
return context.wrap_socket(sock, *args, **kwargs)

0 commit comments

Comments
 (0)