Skip to content

Commit

Permalink
Merge branch 'main' into feature/use_hex_encoding
Browse files Browse the repository at this point in the history
  • Loading branch information
wojciak authored Nov 25, 2024
2 parents f82eb7f + 0da2722 commit 366a5f2
Show file tree
Hide file tree
Showing 13 changed files with 400 additions and 238 deletions.
4 changes: 2 additions & 2 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ repos:
- id: forbid-crlf
- id: remove-crlf
- repo: https://github.com/pre-commit/pre-commit-hooks
rev: v4.6.0
rev: v5.0.0
hooks:
- id: trailing-whitespace
- id: end-of-file-fixer
Expand All @@ -15,7 +15,7 @@ repos:
exclude: helm/
args: [ --unsafe ]
- repo: https://github.com/charliermarsh/ruff-pre-commit
rev: "v0.4.4"
rev: "v0.7.4"
hooks:
- id: ruff
args: [--fix, --exit-non-zero-on-fix]
Expand Down
6 changes: 5 additions & 1 deletion mocket/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,18 @@
from mocket.entry import MocketEntry
from mocket.mocket import Mocket
from mocket.mocketizer import Mocketizer, mocketize
from mocket.ssl import FakeSSLContext
from mocket.ssl.context import MocketSSLContext

# NOTE this is here for backwards-compat to keep old import-paths working
from mocket.ssl.context import MocketSSLContext as FakeSSLContext

__all__ = (
"async_mocketize",
"mocketize",
"Mocket",
"MocketEntry",
"Mocketizer",
"MocketSSLContext",
"FakeSSLContext",
)

Expand Down
85 changes: 41 additions & 44 deletions mocket/inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,6 @@
import ssl

import urllib3
from urllib3.connection import match_hostname as urllib3_match_hostname
from urllib3.util.ssl_ import ssl_wrap_socket as urllib3_ssl_wrap_socket

try:
from urllib3.util.ssl_ import wrap_socket as urllib3_wrap_socket
except ImportError:
urllib3_wrap_socket = None


try: # pragma: no cover
from urllib3.contrib.pyopenssl import extract_from_urllib3, inject_into_urllib3
Expand All @@ -21,31 +13,24 @@
except ImportError:
pyopenssl_override = False

true_socket = socket.socket
true_create_connection = socket.create_connection
true_gethostbyname = socket.gethostbyname
true_gethostname = socket.gethostname
true_getaddrinfo = socket.getaddrinfo
true_socketpair = socket.socketpair
true_ssl_wrap_socket = getattr(
ssl, "wrap_socket", None
) # from Py3.12 it's only under SSLContext
true_ssl_socket = ssl.SSLSocket
true_ssl_context = ssl.SSLContext
true_inet_pton = socket.inet_pton
true_urllib3_wrap_socket = urllib3_wrap_socket
true_urllib3_ssl_wrap_socket = urllib3_ssl_wrap_socket
true_urllib3_match_hostname = urllib3_match_hostname


def enable(
namespace: str | None = None,
truesocket_recording_dir: str | None = None,
use_hex_encoding=True,
) -> None:
from mocket.mocket import Mocket
from mocket.socket import MocketSocket, create_connection, socketpair
from mocket.ssl import FakeSSLContext
from mocket.socket import (
MocketSocket,
mock_create_connection,
mock_getaddrinfo,
mock_gethostbyname,
mock_gethostname,
mock_inet_pton,
mock_socketpair,
mock_urllib3_match_hostname,
)
from mocket.ssl.context import MocketSSLContext

Mocket._namespace = namespace
Mocket._truesocket_recording_dir = truesocket_recording_dir
Expand All @@ -58,42 +43,54 @@ def enable(
socket.socket = socket.__dict__["socket"] = MocketSocket
socket._socketobject = socket.__dict__["_socketobject"] = MocketSocket
socket.SocketType = socket.__dict__["SocketType"] = MocketSocket
socket.create_connection = socket.__dict__["create_connection"] = create_connection
socket.gethostname = socket.__dict__["gethostname"] = lambda: "localhost"
socket.gethostbyname = socket.__dict__["gethostbyname"] = lambda host: "127.0.0.1"
socket.getaddrinfo = socket.__dict__["getaddrinfo"] = (
lambda host, port, family=None, socktype=None, proto=None, flags=None: [
(2, 1, 6, "", (host, port))
]
)
socket.socketpair = socket.__dict__["socketpair"] = socketpair
ssl.wrap_socket = ssl.__dict__["wrap_socket"] = FakeSSLContext.wrap_socket
ssl.SSLContext = ssl.__dict__["SSLContext"] = FakeSSLContext
socket.inet_pton = socket.__dict__["inet_pton"] = lambda family, ip: bytes(
"\x7f\x00\x00\x01", "utf-8"
socket.create_connection = socket.__dict__["create_connection"] = (
mock_create_connection
)
socket.gethostname = socket.__dict__["gethostname"] = mock_gethostname
socket.gethostbyname = socket.__dict__["gethostbyname"] = mock_gethostbyname
socket.getaddrinfo = socket.__dict__["getaddrinfo"] = mock_getaddrinfo
socket.socketpair = socket.__dict__["socketpair"] = mock_socketpair
ssl.wrap_socket = ssl.__dict__["wrap_socket"] = MocketSSLContext.wrap_socket
ssl.SSLContext = ssl.__dict__["SSLContext"] = MocketSSLContext
socket.inet_pton = socket.__dict__["inet_pton"] = mock_inet_pton
urllib3.util.ssl_.wrap_socket = urllib3.util.ssl_.__dict__["wrap_socket"] = (
FakeSSLContext.wrap_socket
MocketSSLContext.wrap_socket
)
urllib3.util.ssl_.ssl_wrap_socket = urllib3.util.ssl_.__dict__[
"ssl_wrap_socket"
] = FakeSSLContext.wrap_socket
] = MocketSSLContext.wrap_socket
urllib3.util.ssl_wrap_socket = urllib3.util.__dict__["ssl_wrap_socket"] = (
FakeSSLContext.wrap_socket
MocketSSLContext.wrap_socket
)
urllib3.connection.ssl_wrap_socket = urllib3.connection.__dict__[
"ssl_wrap_socket"
] = FakeSSLContext.wrap_socket
] = MocketSSLContext.wrap_socket
urllib3.connection.match_hostname = urllib3.connection.__dict__[
"match_hostname"
] = lambda *args: None
] = mock_urllib3_match_hostname
if pyopenssl_override: # pragma: no cover
# Take out the pyopenssl version - use the default implementation
extract_from_urllib3()


def disable() -> None:
from mocket.mocket import Mocket
from mocket.socket import (
true_create_connection,
true_getaddrinfo,
true_gethostbyname,
true_gethostname,
true_inet_pton,
true_socket,
true_socketpair,
true_urllib3_match_hostname,
)
from mocket.ssl.context import (
true_ssl_context,
true_ssl_wrap_socket,
true_urllib3_ssl_wrap_socket,
true_urllib3_wrap_socket,
)

socket.socket = socket.__dict__["socket"] = true_socket
socket._socketobject = socket.__dict__["_socketobject"] = true_socket
Expand Down
2 changes: 1 addition & 1 deletion mocket/io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from mocket.mocket import Mocket


class MocketSocketCore(io.BytesIO):
class MocketSocketIO(io.BytesIO):
def __init__(self, address) -> None:
self._address = address
super().__init__()
Expand Down
6 changes: 3 additions & 3 deletions mocket/plugins/aiohttp_connector.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import contextlib

from mocket import FakeSSLContext
from mocket import MocketSSLContext

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

def _get_ssl_context(self, req: ClientRequest) -> FakeSSLContext:
return FakeSSLContext()
def _get_ssl_context(self, req: ClientRequest) -> MocketSSLContext:
return MocketSSLContext()
Loading

0 comments on commit 366a5f2

Please sign in to comment.