MCPcopy
hub / github.com/urllib3/urllib3 / _set_up_fake_getaddrinfo

Function _set_up_fake_getaddrinfo

test/contrib/test_socks.py:97–122  ·  view source on GitHub ↗
(monkeypatch: pytest.MonkeyPatch)

Source from the content-addressed store, hash-verified

95
96
97def _set_up_fake_getaddrinfo(monkeypatch: pytest.MonkeyPatch) -> None:
98 # Work around https://github.com/urllib3/urllib3/pull/2034
99 # Nothing prevents localhost to point to two different IPs. For example, in the
100 # Ubuntu set up by GitHub Actions, localhost points both to 127.0.0.1 and ::1.
101 #
102 # In case of failure, PySocks will try the same request on both IPs, but our
103 # handle_socks[45]_negotiation functions don't handle retries, which leads either to
104 # a deadlock or a timeout in case of a failure on the first address.
105 #
106 # However, some tests need to exercise failure. We don't want retries there, but
107 # can't affect PySocks retries via its API. Instead, we monkeypatch PySocks so that
108 # it only sees a single address, which effectively disables retries.
109 def fake_getaddrinfo(addr: str, port: int, family: int, socket_type: int) -> list[
110 tuple[
111 socket.AddressFamily,
112 socket.SocketKind,
113 int,
114 str,
115 tuple[str, int] | tuple[str, int, int, int] | tuple[int, bytes],
116 ]
117 ]:
118 gai_list = real_getaddrinfo(addr, port, family, socket_type)
119 gai_list = [gai for gai in gai_list if gai[0] == socket.AF_INET]
120 return gai_list[:1]
121
122 monkeypatch.setattr(py_socks.socket, "getaddrinfo", fake_getaddrinfo)
123
124
125def handle_socks5_negotiation(

Callers 3

test_proxy_rejectionMethod · 0.85
test_proxy_rejectionMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected