| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import contextlib |
| 4 |
import sys |
| 5 |
import typing |
| 6 |
from http.client import ResponseNotReady |
| 7 |
from unittest import mock |
| 8 |
|
| 9 |
import pytest |
| 10 |
|
| 11 |
from dummyserver.testcase import HypercornDummyServerTestCase as server |
| 12 |
from urllib3 import HTTPConnectionPool |
| 13 |
from urllib3.response import HTTPResponse |
| 14 |
|
| 15 |
# See https://github.com/python/cpython/issues/146211 |
| 16 |
# Xfail only on Python versions where neither the stdlib nor urllib3's |
| 17 |
# compatibility _tunnel implementation rejects invalid tunnel input. |
| 18 |
_MISSING_TUNNEL_CONTROL_CHAR_FIX = ( |
| 19 |
# 3.13 before 3.13.14 missing fix (no ported implementation for this version) |
| 20 |
(sys.version_info[:2] == (3, 13) and sys.version_info[2] < 14) |
| 21 |
# 3.14 before 3.14.5 missing fix (no ported implementation for this version) |
| 22 |
or (sys.version_info[:2] == (3, 14) and sys.version_info[2] < 5) |
| 23 |
) |
| 24 |
|
| 25 |
|
| 26 |
@pytest.fixture() |
| 27 |
def pool() -> typing.Generator[HTTPConnectionPool]: |
| 28 |
server.setup_class() |
| 29 |
|
| 30 |
with HTTPConnectionPool(server.host, server.port) as pool: |
| 31 |
yield pool |
| 32 |
|
| 33 |
server.teardown_class() |
| 34 |
|
| 35 |
|
| 36 |
def test_returns_urllib3_HTTPResponse(pool: HTTPConnectionPool) -> None: |
| 37 |
with contextlib.closing(pool._get_conn()) as conn: |
| 38 |
conn.request("GET", "/") |
| 39 |
response = conn.getresponse() |
| 40 |
assert isinstance(response, HTTPResponse) |
| 41 |
|
| 42 |
|
| 43 |
@mock.patch("urllib3.connection.sys.audit") |
| 44 |
def test_audit_event(audit_mock: mock.Mock, pool: HTTPConnectionPool) -> None: |
| 45 |
with contextlib.closing(pool._get_conn()) as conn: |
| 46 |
conn.request("GET", "/") |
| 47 |
audit_mock.assert_any_call("http.client.connect", conn, conn.host, conn.port) |
| 48 |
# Ensure the event is raised only once. |
| 49 |
connect_events = [ |
| 50 |
call |
| 51 |
for call in audit_mock.mock_calls |
| 52 |
if call.args[0] == "http.client.connect" |
| 53 |
] |
| 54 |
assert len(connect_events) == 1 |
| 55 |
|
| 56 |
|
| 57 |
def test_does_not_release_conn(pool: HTTPConnectionPool) -> None: |
| 58 |
with contextlib.closing(pool._get_conn()) as conn: |
| 59 |
conn.request("GET", "/") |
| 60 |
response = conn.getresponse() |
| 61 |
|
| 62 |
response.release_conn() |
| 63 |
assert pool.pool.qsize() == 0 # type: ignore[union-attr] |
| 64 |
|
| 65 |
|
| 66 |
def test_releases_conn(pool: HTTPConnectionPool) -> None: |
| 67 |
with contextlib.closing(pool._get_conn()) as conn: |
| 68 |
conn.request("GET", "/") |
| 69 |
response = conn.getresponse() |
| 70 |
|
| 71 |
# If these variables are set by the pool |
| 72 |
# then the response can release the connection |
| 73 |
# back into the pool. |
| 74 |
response._pool = pool # type: ignore[attr-defined] |
| 75 |
response._connection = conn # type: ignore[attr-defined] |
| 76 |
|
| 77 |
response.release_conn() |
| 78 |
assert pool.pool.qsize() == 1 # type: ignore[union-attr] |
| 79 |
|
| 80 |
|
| 81 |
def test_double_getresponse(pool: HTTPConnectionPool) -> None: |
| 82 |
with contextlib.closing(pool._get_conn()) as conn: |
| 83 |
conn.request("GET", "/") |
| 84 |
_ = conn.getresponse() |
| 85 |
|
| 86 |
# Calling getrepsonse() twice should cause an error |
| 87 |
with pytest.raises(ResponseNotReady): |
| 88 |
conn.getresponse() |
| 89 |
|
| 90 |
|
| 91 |
def test_connection_state_properties(pool: HTTPConnectionPool) -> None: |
| 92 |
conn = pool._get_conn() |
| 93 |
|
| 94 |
assert conn.is_closed is True |
| 95 |
assert conn.is_connected is False |
| 96 |
assert conn.has_connected_to_proxy is False |
| 97 |
assert conn.is_verified is False |
| 98 |
assert conn.proxy_is_verified is None |
| 99 |
|
| 100 |
conn.connect() |
| 101 |
|
| 102 |
assert conn.is_closed is False |
| 103 |
assert conn.is_connected is True |
| 104 |
assert conn.has_connected_to_proxy is False |
| 105 |
assert conn.is_verified is False |
| 106 |
assert conn.proxy_is_verified is None |
| 107 |
|
| 108 |
conn.request("GET", "/") |
| 109 |
resp = conn.getresponse() |
| 110 |
assert resp.status == 200 |
| 111 |
|
| 112 |
conn.close() |
| 113 |
|
| 114 |
assert conn.is_closed is True |
| 115 |
assert conn.is_connected is False |
| 116 |
assert conn.has_connected_to_proxy is False |
| 117 |
assert conn.is_verified is False |
| 118 |
assert conn.proxy_is_verified is None |
| 119 |
|
| 120 |
|
| 121 |
def test_set_tunnel_is_reset(pool: HTTPConnectionPool) -> None: |
| 122 |
conn = pool._get_conn() |
| 123 |
|
| 124 |
assert conn.is_closed is True |
| 125 |
assert conn.is_connected is False |
| 126 |
assert conn.has_connected_to_proxy is False |
| 127 |
assert conn.is_verified is False |
| 128 |
assert conn.proxy_is_verified is None |
| 129 |
|
| 130 |
conn.set_tunnel(host="host", port=8080, scheme="http") |
| 131 |
|
| 132 |
assert conn._tunnel_host == "host" # type: ignore[attr-defined] |
| 133 |
assert conn._tunnel_port == 8080 # type: ignore[attr-defined] |
| 134 |
assert conn._tunnel_scheme == "http" # type: ignore[attr-defined] |
| 135 |
|
| 136 |
conn.close() |
| 137 |
|
| 138 |
assert conn._tunnel_host is None # type: ignore[attr-defined] |
| 139 |
assert conn._tunnel_port is None # type: ignore[attr-defined] |
| 140 |
assert conn._tunnel_scheme is None # type: ignore[attr-defined] |
| 141 |
|
| 142 |
|
| 143 |
def test_invalid_tunnel_scheme(pool: HTTPConnectionPool) -> None: |
| 144 |
conn = pool._get_conn() |
| 145 |
|
| 146 |
with pytest.raises(ValueError) as e: |
| 147 |
conn.set_tunnel(host="host", port=8080, scheme="socks") |
| 148 |
assert ( |
| 149 |
str(e.value) |
| 150 |
== "Invalid proxy scheme for tunneling: 'socks', must be either 'http' or 'https'" |
| 151 |
) |
| 152 |
|
| 153 |
|
| 154 |
@pytest.mark.parametrize( |
| 155 |
"name,value", |
| 156 |
[ |
| 157 |
("Invalid\r\nName", "ValidValue"), |
| 158 |
("Invalid\rName", "ValidValue"), |
| 159 |
("Invalid\nName", "ValidValue"), |
| 160 |
("\r\nInvalidName", "ValidValue"), |
| 161 |
("\rInvalidName", "ValidValue"), |
| 162 |
("\nInvalidName", "ValidValue"), |
| 163 |
(" InvalidName", "ValidValue"), |
| 164 |
("\tInvalidName", "ValidValue"), |
| 165 |
("Invalid:Name", "ValidValue"), |
| 166 |
(":InvalidName", "ValidValue"), |
| 167 |
("ValidName", "Invalid\r\nValue"), |
| 168 |
("ValidName", "Invalid\rValue"), |
| 169 |
("ValidName", "Invalid\nValue"), |
| 170 |
("ValidName", "InvalidValue\r\n"), |
| 171 |
("ValidName", "InvalidValue\r"), |
| 172 |
("ValidName", "InvalidValue\n"), |
| 173 |
], |
| 174 |
) |
| 175 |
@pytest.mark.xfail( |
| 176 |
_MISSING_TUNNEL_CONTROL_CHAR_FIX, |
| 177 |
reason="Control characters in tunnel headers not rejected in older Python versions", |
| 178 |
) |
| 179 |
def test_invalid_tunnel_headers( |
| 180 |
pool: HTTPConnectionPool, name: str, value: str |
| 181 |
) -> None: |
| 182 |
conn = pool._get_conn() |
| 183 |
conn.set_tunnel("tunnel", headers={name: value}) |
| 184 |
with pytest.raises(ValueError, match="Invalid header"): |
| 185 |
conn.connect() |
| 186 |
conn.close() |
| 187 |
|
| 188 |
|
| 189 |
@pytest.mark.parametrize( |
| 190 |
"tunnel_host", |
| 191 |
[ |
| 192 |
"invalid\r.host", |
| 193 |
"\ninvalid.host", |
| 194 |
"invalid.host\r\n", |
| 195 |
"invalid.host\x00", |
| 196 |
"invalid host", |
| 197 |
], |
| 198 |
) |
| 199 |
@pytest.mark.xfail( |
| 200 |
_MISSING_TUNNEL_CONTROL_CHAR_FIX, |
| 201 |
reason="Control characters in tunnel host not rejected in older Python versions", |
| 202 |
) |
| 203 |
def test_invalid_tunnel_host(pool: HTTPConnectionPool, tunnel_host: str) -> None: |
| 204 |
conn = pool._get_conn() |
| 205 |
conn.set_tunnel(tunnel_host) |
| 206 |
with pytest.raises( |
| 207 |
ValueError, match="Tunnel host can't contain control characters" |
| 208 |
): |
| 209 |
conn.connect() |
| 210 |
conn.close() |
| 211 |
|
| 212 |
|
| 213 |
def test_response_after_drain_conn(pool: HTTPConnectionPool) -> None: |
| 214 |
""" |
| 215 |
Test that a connection can be reused after calling `drain_conn` on |
| 216 |
an unread response. |
| 217 |
""" |
| 218 |
conn = pool._get_conn() |
| 219 |
|
| 220 |
conn.request("GET", "/", preload_content=False) |
| 221 |
response = conn.getresponse() |
| 222 |
assert response.status == 200 |
| 223 |
response.drain_conn() |
| 224 |
|
| 225 |
conn.request("GET", "/", preload_content=False) |
| 226 |
response = conn.getresponse() |
| 227 |
assert response.status == 200 |
| 228 |
|
| 229 |
conn.close() |