Skip to content

Commit e778928

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

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
@@ -38,7 +38,7 @@ def enable(
3838
mock_inet_pton,
3939
mock_socketpair,
4040
)
41-
from mocket.ssl.context import MocketSSLContext
41+
from mocket.ssl.context import MocketSSLContext, mock_wrap_socket
4242
from mocket.urllib3 import (
4343
mock_match_hostname as mock_urllib3_match_hostname,
4444
)
@@ -58,7 +58,7 @@ def enable(
5858
(socket, "socketpair"): mock_socketpair,
5959
# stdlib: ssl
6060
(ssl, "SSLContext"): MocketSSLContext,
61-
(ssl, "wrap_socket"): MocketSSLContext.wrap_socket, # python < 3.12.0
61+
(ssl, "wrap_socket"): mock_wrap_socket, # python < 3.12.0
6262
# urllib3
6363
(urllib3.connection, "match_hostname"): mock_urllib3_match_hostname,
6464
(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)