Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Refactor rename ssl classes #266

Merged
merged 2 commits into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.context 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
14 changes: 7 additions & 7 deletions mocket/inject.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ def enable(
mock_socketpair,
mock_urllib3_match_hostname,
)
from mocket.ssl.context import FakeSSLContext
from mocket.ssl.context import MocketSSLContext

Mocket._namespace = namespace
Mocket._truesocket_recording_dir = truesocket_recording_dir
Expand All @@ -48,21 +48,21 @@ def enable(
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"] = FakeSSLContext.wrap_socket
ssl.SSLContext = ssl.__dict__["SSLContext"] = FakeSSLContext
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"
] = mock_urllib3_match_hostname
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()
8 changes: 4 additions & 4 deletions mocket/socket.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@

from mocket.compat import decode_from_bytes, encode_to_bytes
from mocket.entry import MocketEntry
from mocket.io import MocketSocketCore
from mocket.io import MocketSocketIO
from mocket.mocket import Mocket
from mocket.mode import MocketMode
from mocket.types import (
Expand Down Expand Up @@ -148,9 +148,9 @@ def proto(self) -> int:
return self._proto

@property
def io(self) -> MocketSocketCore:
def io(self) -> MocketSocketIO:
if self._io is None:
self._io = MocketSocketCore((self._host, self._port))
self._io = MocketSocketIO((self._host, self._port))
return self._io

def fileno(self) -> int:
Expand Down Expand Up @@ -196,7 +196,7 @@ def connect(self, address: Address) -> None:
self._address = self._host, self._port = address
Mocket._address = address

def makefile(self, mode: str = "r", bufsize: int = -1) -> MocketSocketCore:
def makefile(self, mode: str = "r", bufsize: int = -1) -> MocketSocketIO:
return self.io

def get_entry(self, data: bytes) -> MocketEntry | None:
Expand Down
4 changes: 2 additions & 2 deletions mocket/ssl/context.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@
true_urllib3_wrap_socket = urllib3_wrap_socket


class SuperFakeSSLContext:
class _MocketSSLContext:
"""For Python 3.6 and newer."""

class FakeSetter(int):
Expand All @@ -40,7 +40,7 @@ def __set__(self, *args: Any) -> None:
verify_flags = FakeSetter()


class FakeSSLContext(SuperFakeSSLContext):
class MocketSSLContext(_MocketSSLContext):
DUMMY_METHODS = (
"load_default_certs",
"load_verify_locations",
Expand Down
2 changes: 1 addition & 1 deletion mocket/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from mocket.compat import decode_from_bytes, encode_to_bytes

# NOTE this is here for backwards-compat to keep old import-paths working
from mocket.io import MocketSocketCore as MocketSocketCore
from mocket.io import MocketSocketIO as MocketSocketCore

# NOTE this is here for backwards-compat to keep old import-paths working
from mocket.mode import MocketMode as MocketMode
Expand Down