Skip to content

Commit 86872cd

Browse files
authored
Merge branch 'main' into feature/skip_response_cache
2 parents ab8e74f + 0da2722 commit 86872cd

13 files changed

+400
-238
lines changed

.pre-commit-config.yaml

+2-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ repos:
55
- id: forbid-crlf
66
- id: remove-crlf
77
- repo: https://github.com/pre-commit/pre-commit-hooks
8-
rev: v4.6.0
8+
rev: v5.0.0
99
hooks:
1010
- id: trailing-whitespace
1111
- id: end-of-file-fixer
@@ -15,7 +15,7 @@ repos:
1515
exclude: helm/
1616
args: [ --unsafe ]
1717
- repo: https://github.com/charliermarsh/ruff-pre-commit
18-
rev: "v0.4.4"
18+
rev: "v0.7.4"
1919
hooks:
2020
- id: ruff
2121
args: [--fix, --exit-non-zero-on-fix]

mocket/__init__.py

+5-1
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,18 @@
22
from mocket.entry import MocketEntry
33
from mocket.mocket import Mocket
44
from mocket.mocketizer import Mocketizer, mocketize
5-
from mocket.ssl import FakeSSLContext
5+
from mocket.ssl.context import MocketSSLContext
6+
7+
# NOTE this is here for backwards-compat to keep old import-paths working
8+
from mocket.ssl.context import MocketSSLContext as FakeSSLContext
69

710
__all__ = (
811
"async_mocketize",
912
"mocketize",
1013
"Mocket",
1114
"MocketEntry",
1215
"Mocketizer",
16+
"MocketSSLContext",
1317
"FakeSSLContext",
1418
)
1519

mocket/inject.py

+41-44
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,6 @@
55
import ssl
66

77
import urllib3
8-
from urllib3.connection import match_hostname as urllib3_match_hostname
9-
from urllib3.util.ssl_ import ssl_wrap_socket as urllib3_ssl_wrap_socket
10-
11-
try:
12-
from urllib3.util.ssl_ import wrap_socket as urllib3_wrap_socket
13-
except ImportError:
14-
urllib3_wrap_socket = None
15-
168

179
try: # pragma: no cover
1810
from urllib3.contrib.pyopenssl import extract_from_urllib3, inject_into_urllib3
@@ -21,31 +13,24 @@
2113
except ImportError:
2214
pyopenssl_override = False
2315

24-
true_socket = socket.socket
25-
true_create_connection = socket.create_connection
26-
true_gethostbyname = socket.gethostbyname
27-
true_gethostname = socket.gethostname
28-
true_getaddrinfo = socket.getaddrinfo
29-
true_socketpair = socket.socketpair
30-
true_ssl_wrap_socket = getattr(
31-
ssl, "wrap_socket", None
32-
) # from Py3.12 it's only under SSLContext
33-
true_ssl_socket = ssl.SSLSocket
34-
true_ssl_context = ssl.SSLContext
35-
true_inet_pton = socket.inet_pton
36-
true_urllib3_wrap_socket = urllib3_wrap_socket
37-
true_urllib3_ssl_wrap_socket = urllib3_ssl_wrap_socket
38-
true_urllib3_match_hostname = urllib3_match_hostname
39-
4016

4117
def enable(
4218
namespace: str | None = None,
4319
truesocket_recording_dir: str | None = None,
4420
skip_response_cache: bool = False,
4521
) -> None:
4622
from mocket.mocket import Mocket
47-
from mocket.socket import MocketSocket, create_connection, socketpair
48-
from mocket.ssl import FakeSSLContext
23+
from mocket.socket import (
24+
MocketSocket,
25+
mock_create_connection,
26+
mock_getaddrinfo,
27+
mock_gethostbyname,
28+
mock_gethostname,
29+
mock_inet_pton,
30+
mock_socketpair,
31+
mock_urllib3_match_hostname,
32+
)
33+
from mocket.ssl.context import MocketSSLContext
4934

5035
Mocket._namespace = namespace
5136
Mocket._truesocket_recording_dir = truesocket_recording_dir
@@ -58,42 +43,54 @@ def enable(
5843
socket.socket = socket.__dict__["socket"] = MocketSocket
5944
socket._socketobject = socket.__dict__["_socketobject"] = MocketSocket
6045
socket.SocketType = socket.__dict__["SocketType"] = MocketSocket
61-
socket.create_connection = socket.__dict__["create_connection"] = create_connection
62-
socket.gethostname = socket.__dict__["gethostname"] = lambda: "localhost"
63-
socket.gethostbyname = socket.__dict__["gethostbyname"] = lambda host: "127.0.0.1"
64-
socket.getaddrinfo = socket.__dict__["getaddrinfo"] = (
65-
lambda host, port, family=None, socktype=None, proto=None, flags=None: [
66-
(2, 1, 6, "", (host, port))
67-
]
68-
)
69-
socket.socketpair = socket.__dict__["socketpair"] = socketpair
70-
ssl.wrap_socket = ssl.__dict__["wrap_socket"] = FakeSSLContext.wrap_socket
71-
ssl.SSLContext = ssl.__dict__["SSLContext"] = FakeSSLContext
72-
socket.inet_pton = socket.__dict__["inet_pton"] = lambda family, ip: bytes(
73-
"\x7f\x00\x00\x01", "utf-8"
46+
socket.create_connection = socket.__dict__["create_connection"] = (
47+
mock_create_connection
7448
)
49+
socket.gethostname = socket.__dict__["gethostname"] = mock_gethostname
50+
socket.gethostbyname = socket.__dict__["gethostbyname"] = mock_gethostbyname
51+
socket.getaddrinfo = socket.__dict__["getaddrinfo"] = mock_getaddrinfo
52+
socket.socketpair = socket.__dict__["socketpair"] = mock_socketpair
53+
ssl.wrap_socket = ssl.__dict__["wrap_socket"] = MocketSSLContext.wrap_socket
54+
ssl.SSLContext = ssl.__dict__["SSLContext"] = MocketSSLContext
55+
socket.inet_pton = socket.__dict__["inet_pton"] = mock_inet_pton
7556
urllib3.util.ssl_.wrap_socket = urllib3.util.ssl_.__dict__["wrap_socket"] = (
76-
FakeSSLContext.wrap_socket
57+
MocketSSLContext.wrap_socket
7758
)
7859
urllib3.util.ssl_.ssl_wrap_socket = urllib3.util.ssl_.__dict__[
7960
"ssl_wrap_socket"
80-
] = FakeSSLContext.wrap_socket
61+
] = MocketSSLContext.wrap_socket
8162
urllib3.util.ssl_wrap_socket = urllib3.util.__dict__["ssl_wrap_socket"] = (
82-
FakeSSLContext.wrap_socket
63+
MocketSSLContext.wrap_socket
8364
)
8465
urllib3.connection.ssl_wrap_socket = urllib3.connection.__dict__[
8566
"ssl_wrap_socket"
86-
] = FakeSSLContext.wrap_socket
67+
] = MocketSSLContext.wrap_socket
8768
urllib3.connection.match_hostname = urllib3.connection.__dict__[
8869
"match_hostname"
89-
] = lambda *args: None
70+
] = mock_urllib3_match_hostname
9071
if pyopenssl_override: # pragma: no cover
9172
# Take out the pyopenssl version - use the default implementation
9273
extract_from_urllib3()
9374

9475

9576
def disable() -> None:
9677
from mocket.mocket import Mocket
78+
from mocket.socket import (
79+
true_create_connection,
80+
true_getaddrinfo,
81+
true_gethostbyname,
82+
true_gethostname,
83+
true_inet_pton,
84+
true_socket,
85+
true_socketpair,
86+
true_urllib3_match_hostname,
87+
)
88+
from mocket.ssl.context import (
89+
true_ssl_context,
90+
true_ssl_wrap_socket,
91+
true_urllib3_ssl_wrap_socket,
92+
true_urllib3_wrap_socket,
93+
)
9794

9895
socket.socket = socket.__dict__["socket"] = true_socket
9996
socket._socketobject = socket.__dict__["_socketobject"] = true_socket

mocket/io.py

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
from mocket.mocket import Mocket
55

66

7-
class MocketSocketCore(io.BytesIO):
7+
class MocketSocketIO(io.BytesIO):
88
def __init__(self, address) -> None:
99
self._address = address
1010
super().__init__()

mocket/plugins/aiohttp_connector.py

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import contextlib
22

3-
from mocket import FakeSSLContext
3+
from mocket import MocketSSLContext
44

55
with contextlib.suppress(ModuleNotFoundError):
66
from aiohttp import ClientRequest
@@ -14,5 +14,5 @@ class MocketTCPConnector(TCPConnector):
1414
slightly patching the `ClientSession` while testing.
1515
"""
1616

17-
def _get_ssl_context(self, req: ClientRequest) -> FakeSSLContext:
18-
return FakeSSLContext()
17+
def _get_ssl_context(self, req: ClientRequest) -> MocketSSLContext:
18+
return MocketSSLContext()

0 commit comments

Comments
 (0)