Skip to content

Commit 05ee3ff

Browse files
authored
Fix SSL host fallback check for empty server_hostname
1 parent 3ba91b5 commit 05ee3ff

2 files changed

Lines changed: 15 additions & 1 deletion

File tree

mocket/ssl/socket.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ def getpeercert(self, binary_form: bool = False) -> _PeerCertRetDictType:
7272
Returns:
7373
Mock certificate dictionary
7474
"""
75-
if not (self._host and self._port):
75+
if self._host is None or self._port is None:
7676
self._address = self._host, self._port = Mocket._address
7777

7878
now = datetime.now()

tests/test_socket.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,20 @@ def test_wrap_bio_uses_current_mocket_address(
147147
assert ssl_obj._address == (expected_host, 443)
148148

149149

150+
def test_wrap_bio_preserves_empty_server_hostname_on_getpeercert(monkeypatch):
151+
monkeypatch.setattr(Mocket, "_address", ("httpbin.local", 443))
152+
ssl_obj = MocketSSLContext().wrap_bio(
153+
incoming=None,
154+
outgoing=None,
155+
server_hostname="",
156+
)
157+
158+
ssl_obj.getpeercert()
159+
160+
assert ssl_obj._host == ""
161+
assert ssl_obj._address == ("", 443)
162+
163+
150164
def test_recvfrom_into():
151165
sock = MocketSocket(socket.AF_INET, socket.SOCK_STREAM)
152166
test_data = b"abc123"

0 commit comments

Comments
 (0)