Skip to content

Commit 012df13

Browse files
committed
refactor: move FakeSSLContext from mocket.mocket to mocket.ssl
1 parent 207778a commit 012df13

File tree

3 files changed

+59
-57
lines changed

3 files changed

+59
-57
lines changed

mocket/__init__.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
11
from mocket.async_mocket import async_mocketize
22
from mocket.entry import MocketEntry
3-
from mocket.mocket import FakeSSLContext, Mocket
3+
from mocket.mocket import Mocket
44
from mocket.mocketizer import Mocketizer, mocketize
5+
from mocket.ssl import FakeSSLContext
56

67
__all__ = (
78
"async_mocketize",

mocket/mocket.py

Lines changed: 1 addition & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
from mocket.compat import decode_from_bytes, encode_to_bytes
2626
from mocket.io import MocketSocketCore
2727
from mocket.mode import MocketMode
28+
from mocket.ssl import FakeSSLContext
2829
from mocket.utils import hexdump, hexload
2930

3031
xxh32 = None
@@ -59,62 +60,6 @@
5960
true_urllib3_match_hostname = urllib3_match_hostname
6061

6162

62-
class SuperFakeSSLContext:
63-
"""For Python 3.6 and newer."""
64-
65-
class FakeSetter(int):
66-
def __set__(self, *args):
67-
pass
68-
69-
minimum_version = FakeSetter()
70-
options = FakeSetter()
71-
verify_mode = FakeSetter()
72-
verify_flags = FakeSetter()
73-
74-
75-
class FakeSSLContext(SuperFakeSSLContext):
76-
DUMMY_METHODS = (
77-
"load_default_certs",
78-
"load_verify_locations",
79-
"set_alpn_protocols",
80-
"set_ciphers",
81-
"set_default_verify_paths",
82-
)
83-
sock = None
84-
post_handshake_auth = None
85-
_check_hostname = False
86-
87-
@property
88-
def check_hostname(self):
89-
return self._check_hostname
90-
91-
@check_hostname.setter
92-
def check_hostname(self, _):
93-
self._check_hostname = False
94-
95-
def __init__(self, *args, **kwargs):
96-
self._set_dummy_methods()
97-
98-
def _set_dummy_methods(self):
99-
def dummy_method(*args, **kwargs):
100-
pass
101-
102-
for m in self.DUMMY_METHODS:
103-
setattr(self, m, dummy_method)
104-
105-
@staticmethod
106-
def wrap_socket(sock, *args, **kwargs):
107-
sock.kwargs = kwargs
108-
sock._secure_socket = True
109-
return sock
110-
111-
@staticmethod
112-
def wrap_bio(incoming, outcoming, *args, **kwargs):
113-
ssl_obj = MocketSocket()
114-
ssl_obj._host = kwargs["server_hostname"]
115-
return ssl_obj
116-
117-
11863
def create_connection(address, timeout=None, source_address=None):
11964
s = socket.socket(socket.AF_INET, socket.SOCK_STREAM, socket.IPPROTO_TCP)
12065
if timeout:

mocket/ssl.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class SuperFakeSSLContext:
2+
"""For Python 3.6 and newer."""
3+
4+
class FakeSetter(int):
5+
def __set__(self, *args):
6+
pass
7+
8+
minimum_version = FakeSetter()
9+
options = FakeSetter()
10+
verify_mode = FakeSetter()
11+
verify_flags = FakeSetter()
12+
13+
14+
class FakeSSLContext(SuperFakeSSLContext):
15+
DUMMY_METHODS = (
16+
"load_default_certs",
17+
"load_verify_locations",
18+
"set_alpn_protocols",
19+
"set_ciphers",
20+
"set_default_verify_paths",
21+
)
22+
sock = None
23+
post_handshake_auth = None
24+
_check_hostname = False
25+
26+
@property
27+
def check_hostname(self):
28+
return self._check_hostname
29+
30+
@check_hostname.setter
31+
def check_hostname(self, _):
32+
self._check_hostname = False
33+
34+
def __init__(self, *args, **kwargs):
35+
self._set_dummy_methods()
36+
37+
def _set_dummy_methods(self):
38+
def dummy_method(*args, **kwargs):
39+
pass
40+
41+
for m in self.DUMMY_METHODS:
42+
setattr(self, m, dummy_method)
43+
44+
@staticmethod
45+
def wrap_socket(sock, *args, **kwargs):
46+
sock.kwargs = kwargs
47+
sock._secure_socket = True
48+
return sock
49+
50+
@staticmethod
51+
def wrap_bio(incoming, outcoming, *args, **kwargs):
52+
from mocket.mocket import MocketSocket
53+
54+
ssl_obj = MocketSocket()
55+
ssl_obj._host = kwargs["server_hostname"]
56+
return ssl_obj

0 commit comments

Comments
 (0)