Skip to content
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
2 changes: 1 addition & 1 deletion src/pytest_socket/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ def resolve_hostnames(hostname: str) -> set[str]:
try:
return {
addr_struct[0] # type: ignore[misc]
for *_, addr_struct in socket.getaddrinfo(hostname, None)
for *_, addr_struct in _true_getaddrinfo(hostname, None)
}
except socket.gaierror:
return set()
Expand Down
20 changes: 19 additions & 1 deletion tests/test_restrict_hosts.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

import pytest

import pytest_socket
from pytest_socket import (
SocketConnectBlockedError,
_remove_restrictions,
Expand Down Expand Up @@ -110,7 +111,7 @@ def _getaddrinfo(host, port, family=0, type=0, proto=0, flags=0):
)
return [v4]

monkeypatch.setattr(socket, "getaddrinfo", _getaddrinfo)
monkeypatch.setattr(pytest_socket, "_true_getaddrinfo", _getaddrinfo)
return hosts


Expand Down Expand Up @@ -598,3 +599,20 @@ def test_blocked_message_shows_ip_without_redundant_parenthetical(assert_connect
"""
result = assert_connect(False, cli_arg="1.2.3.4", host="2.2.2.2")
result.stdout.fnmatch_lines('*allowed: "1.2.3.4"*')


def test_hostname_allow_list_resolves_after_conftest_disable_socket(pytester):
"""Resolving allowed hostnames uses the real getaddrinfo, so it works even
when a conftest has already called disable_socket()."""
pytester.makeconftest("""
from pytest_socket import disable_socket

def pytest_runtest_setup():
disable_socket()
""")
pytester.makepyfile("""
def test_math():
assert 1 + 1 == 2
""")
result = pytester.runpytest("--allow-hosts=localhost")
result.assert_outcomes(passed=1)
Loading