| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import gzip |
| 4 |
import typing |
| 5 |
from test import LONG_TIMEOUT |
| 6 |
from unittest import mock |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from dummyserver.socketserver import HAS_IPV6 |
| 11 |
from dummyserver.testcase import ( |
| 12 |
HypercornDummyServerTestCase, |
| 13 |
IPv6HypercornDummyServerTestCase, |
| 14 |
) |
| 15 |
from urllib3 import HTTPHeaderDict, HTTPResponse, request |
| 16 |
from urllib3.connectionpool import port_by_scheme |
| 17 |
from urllib3.exceptions import MaxRetryError, URLSchemeUnknown |
| 18 |
from urllib3.poolmanager import PoolManager |
| 19 |
from urllib3.util.retry import Retry |
| 20 |
|
| 21 |
|
| 22 |
class TestPoolManager(HypercornDummyServerTestCase): |
| 23 |
@classmethod |
| 24 |
def setup_class(cls) -> None: |
| 25 |
super().setup_class() |
| 26 |
cls.base_url = f"http://{cls.host}:{cls.port}" |
| 27 |
cls.base_url_alt = f"http://{cls.host_alt}:{cls.port}" |
| 28 |
|
| 29 |
@pytest.mark.parametrize( |
| 30 |
"pool_manager_kwargs", |
| 31 |
({}, {"retries": None}, {"retries": 1}, {"retries": Retry(1)}), |
| 32 |
) |
| 33 |
def test_redirect(self, pool_manager_kwargs: dict[str, typing.Any]) -> None: |
| 34 |
with PoolManager(**pool_manager_kwargs) as http: |
| 35 |
r = http.request( |
| 36 |
"GET", |
| 37 |
f"{self.base_url}/redirect", |
| 38 |
fields={"target": f"{self.base_url}/"}, |
| 39 |
redirect=False, |
| 40 |
) |
| 41 |
|
| 42 |
assert r.status == 303 |
| 43 |
|
| 44 |
r = http.request( |
| 45 |
"GET", |
| 46 |
f"{self.base_url}/redirect", |
| 47 |
fields={"target": f"{self.base_url}/"}, |
| 48 |
) |
| 49 |
|
| 50 |
assert r.status == 200 |
| 51 |
assert r.data == b"Dummy server!" |
| 52 |
|
| 53 |
@pytest.mark.parametrize( |
| 54 |
"pool_manager_kwargs", |
| 55 |
({}, {"retries": None}, {"retries": 2}, {"retries": Retry(2)}), |
| 56 |
) |
| 57 |
def test_redirect_twice(self, pool_manager_kwargs: dict[str, typing.Any]) -> None: |
| 58 |
with PoolManager(**pool_manager_kwargs) as http: |
| 59 |
r = http.request( |
| 60 |
"GET", |
| 61 |
f"{self.base_url}/redirect", |
| 62 |
fields={"target": f"{self.base_url}/redirect"}, |
| 63 |
redirect=False, |
| 64 |
) |
| 65 |
|
| 66 |
assert r.status == 303 |
| 67 |
|
| 68 |
r = http.request( |
| 69 |
"GET", |
| 70 |
f"{self.base_url}/redirect", |
| 71 |
fields={"target": f"{self.base_url}/redirect?target={self.base_url}/"}, |
| 72 |
) |
| 73 |
|
| 74 |
assert r.status == 200 |
| 75 |
assert r.data == b"Dummy server!" |
| 76 |
|
| 77 |
def test_redirect_to_relative_url(self) -> None: |
| 78 |
with PoolManager() as http: |
| 79 |
r = http.request( |
| 80 |
"GET", |
| 81 |
f"{self.base_url}/redirect", |
| 82 |
fields={"target": "/redirect"}, |
| 83 |
redirect=False, |
| 84 |
) |
| 85 |
|
| 86 |
assert r.status == 303 |
| 87 |
|
| 88 |
r = http.request( |
| 89 |
"GET", f"{self.base_url}/redirect", fields={"target": "/redirect"} |
| 90 |
) |
| 91 |
|
| 92 |
assert r.status == 200 |
| 93 |
assert r.data == b"Dummy server!" |
| 94 |
|
| 95 |
@pytest.mark.parametrize( |
| 96 |
"retries", |
| 97 |
(0, Retry(total=0), Retry(redirect=0), Retry(total=0, redirect=0)), |
| 98 |
) |
| 99 |
def test_redirects_disabled_for_pool_manager_with_0( |
| 100 |
self, retries: typing.Literal[0] | Retry |
| 101 |
) -> None: |
| 102 |
""" |
| 103 |
Check handling redirects when retries is set to 0 on the pool |
| 104 |
manager. |
| 105 |
""" |
| 106 |
with PoolManager(retries=retries) as http: |
| 107 |
with pytest.raises(MaxRetryError): |
| 108 |
http.request("GET", f"{self.base_url}/redirect") |
| 109 |
|
| 110 |
# Setting redirect=True should not change the behavior. |
| 111 |
with pytest.raises(MaxRetryError): |
| 112 |
http.request("GET", f"{self.base_url}/redirect", redirect=True) |
| 113 |
|
| 114 |
# Setting redirect=False should not make it follow the redirect, |
| 115 |
# but MaxRetryError should not be raised. |
| 116 |
response = http.request("GET", f"{self.base_url}/redirect", redirect=False) |
| 117 |
assert response.status == 303 |
| 118 |
|
| 119 |
@pytest.mark.parametrize( |
| 120 |
"retries", |
| 121 |
( |
| 122 |
False, |
| 123 |
Retry(total=False), |
| 124 |
Retry(redirect=False), |
| 125 |
Retry(total=False, redirect=False), |
| 126 |
), |
| 127 |
) |
| 128 |
def test_redirects_disabled_for_pool_manager_with_false( |
| 129 |
self, retries: typing.Literal[False] | Retry |
| 130 |
) -> None: |
| 131 |
""" |
| 132 |
Check that setting retries set to False on the pool manager disables |
| 133 |
raising MaxRetryError and redirect=True does not change the |
| 134 |
behavior. |
| 135 |
""" |
| 136 |
with PoolManager(retries=retries) as http: |
| 137 |
response = http.request("GET", f"{self.base_url}/redirect") |
| 138 |
assert response.status == 303 |
| 139 |
|
| 140 |
response = http.request("GET", f"{self.base_url}/redirect", redirect=True) |
| 141 |
assert response.status == 303 |
| 142 |
|
| 143 |
response = http.request("GET", f"{self.base_url}/redirect", redirect=False) |
| 144 |
assert response.status == 303 |
| 145 |
|
| 146 |
def test_redirects_disabled_for_individual_request(self) -> None: |
| 147 |
""" |
| 148 |
Check handling redirects when they are meant to be disabled |
| 149 |
on the request level. |
| 150 |
""" |
| 151 |
with PoolManager() as http: |
| 152 |
# Check when redirect is not passed. |
| 153 |
with pytest.raises(MaxRetryError): |
| 154 |
http.request("GET", f"{self.base_url}/redirect", retries=0) |
| 155 |
response = http.request("GET", f"{self.base_url}/redirect", retries=False) |
| 156 |
assert response.status == 303 |
| 157 |
|
| 158 |
# Check when redirect=True. |
| 159 |
with pytest.raises(MaxRetryError): |
| 160 |
http.request( |
| 161 |
"GET", f"{self.base_url}/redirect", retries=0, redirect=True |
| 162 |
) |
| 163 |
response = http.request( |
| 164 |
"GET", f"{self.base_url}/redirect", retries=False, redirect=True |
| 165 |
) |
| 166 |
assert response.status == 303 |
| 167 |
|
| 168 |
# Check when redirect=False. |
| 169 |
response = http.request( |
| 170 |
"GET", f"{self.base_url}/redirect", retries=0, redirect=False |
| 171 |
) |
| 172 |
assert response.status == 303 |
| 173 |
response = http.request( |
| 174 |
"GET", f"{self.base_url}/redirect", retries=False, redirect=False |
| 175 |
) |
| 176 |
assert response.status == 303 |
| 177 |
|
| 178 |
def test_cross_host_redirect(self) -> None: |
| 179 |
with PoolManager() as http: |
| 180 |
cross_host_location = f"{self.base_url_alt}/echo?a=b" |
| 181 |
with pytest.raises(MaxRetryError): |
| 182 |
http.request( |
| 183 |
"GET", |
| 184 |
f"{self.base_url}/redirect", |
| 185 |
fields={"target": cross_host_location}, |
| 186 |
timeout=LONG_TIMEOUT, |
| 187 |
retries=0, |
| 188 |
) |
| 189 |
|
| 190 |
r = http.request( |
| 191 |
"GET", |
| 192 |
f"{self.base_url}/redirect", |
| 193 |
fields={"target": f"{self.base_url_alt}/echo?a=b"}, |
| 194 |
timeout=LONG_TIMEOUT, |
| 195 |
retries=1, |
| 196 |
) |
| 197 |
|
| 198 |
assert isinstance(r, HTTPResponse) |
| 199 |
assert r._pool is not None |
| 200 |
assert r._pool.host == self.host_alt |
| 201 |
|
| 202 |
def test_too_many_redirects(self) -> None: |
| 203 |
with PoolManager() as http: |
| 204 |
with pytest.raises(MaxRetryError): |
| 205 |
http.request( |
| 206 |
"GET", |
| 207 |
f"{self.base_url}/redirect", |
| 208 |
fields={ |
| 209 |
"target": f"{self.base_url}/redirect?target={self.base_url}/" |
| 210 |
}, |
| 211 |
retries=1, |
| 212 |
preload_content=False, |
| 213 |
) |
| 214 |
|
| 215 |
with pytest.raises(MaxRetryError): |
| 216 |
http.request( |
| 217 |
"GET", |
| 218 |
f"{self.base_url}/redirect", |
| 219 |
fields={ |
| 220 |
"target": f"{self.base_url}/redirect?target={self.base_url}/" |
| 221 |
}, |
| 222 |
retries=Retry(total=None, redirect=1), |
| 223 |
preload_content=False, |
| 224 |
) |
| 225 |
|
| 226 |
# Even with preload_content=False and raise on redirects, we reused the same |
| 227 |
# connection |
| 228 |
assert len(http.pools) == 1 |
| 229 |
pool = http.connection_from_host(self.host, self.port) |
| 230 |
assert pool.num_connections == 1 |
| 231 |
|
| 232 |
# Check when retries are configured for the pool manager. |
| 233 |
with PoolManager(retries=1) as http: |
| 234 |
with pytest.raises(MaxRetryError): |
| 235 |
http.request( |
| 236 |
"GET", |
| 237 |
f"{self.base_url}/redirect", |
| 238 |
fields={"target": f"/redirect?target={self.base_url}/"}, |
| 239 |
) |
| 240 |
|
| 241 |
# Here we allow more retries for the request. |
| 242 |
response = http.request( |
| 243 |
"GET", |
| 244 |
f"{self.base_url}/redirect", |
| 245 |
fields={"target": f"/redirect?target={self.base_url}/"}, |
| 246 |
retries=2, |
| 247 |
) |
| 248 |
assert response.status == 200 |
| 249 |
|
| 250 |
def test_redirect_cross_host_remove_headers(self) -> None: |
| 251 |
with PoolManager() as http: |
| 252 |
r = http.request( |
| 253 |
"GET", |
| 254 |
f"{self.base_url}/redirect", |
| 255 |
fields={"target": f"{self.base_url_alt}/headers"}, |
| 256 |
headers={ |
| 257 |
"Authorization": "foo", |
| 258 |
"Proxy-Authorization": "bar", |
| 259 |
"Cookie": "foo=bar", |
| 260 |
}, |
| 261 |
) |
| 262 |
|
| 263 |
assert r.status == 200 |
| 264 |
|
| 265 |
data = r.json() |
| 266 |
|
| 267 |
assert "Authorization" not in data |
| 268 |
assert "Proxy-Authorization" not in data |
| 269 |
assert "Cookie" not in data |
| 270 |
|
| 271 |
r = http.request( |
| 272 |
"GET", |
| 273 |
f"{self.base_url}/redirect", |
| 274 |
fields={"target": f"{self.base_url_alt}/headers"}, |
| 275 |
headers={ |
| 276 |
"authorization": "foo", |
| 277 |
"proxy-authorization": "baz", |
| 278 |
"cookie": "foo=bar", |
| 279 |
}, |
| 280 |
) |
| 281 |
|
| 282 |
assert r.status == 200 |
| 283 |
|
| 284 |
data = r.json() |
| 285 |
|
| 286 |
assert "authorization" not in data |
| 287 |
assert "Authorization" not in data |
| 288 |
assert "proxy-authorization" not in data |
| 289 |
assert "Proxy-Authorization" not in data |
| 290 |
assert "cookie" not in data |
| 291 |
assert "Cookie" not in data |
| 292 |
|
| 293 |
def test_redirect_cross_host_no_remove_headers(self) -> None: |
| 294 |
with PoolManager() as http: |
| 295 |
r = http.request( |
| 296 |
"GET", |
| 297 |
f"{self.base_url}/redirect", |
| 298 |
fields={"target": f"{self.base_url_alt}/headers"}, |
| 299 |
headers={ |
| 300 |
"Authorization": "foo", |
| 301 |
"Proxy-Authorization": "bar", |
| 302 |
"Cookie": "foo=bar", |
| 303 |
}, |
| 304 |
retries=Retry(remove_headers_on_redirect=[]), |
| 305 |
) |
| 306 |
|
| 307 |
assert r.status == 200 |
| 308 |
|
| 309 |
data = r.json() |
| 310 |
|
| 311 |
assert data["Authorization"] == "foo" |
| 312 |
assert data["Proxy-Authorization"] == "bar" |
| 313 |
assert data["Cookie"] == "foo=bar" |
| 314 |
|
| 315 |
def test_redirect_cross_host_set_removed_headers(self) -> None: |
| 316 |
with PoolManager() as http: |
| 317 |
r = http.request( |
| 318 |
"GET", |
| 319 |
f"{self.base_url}/redirect", |
| 320 |
fields={"target": f"{self.base_url_alt}/headers"}, |
| 321 |
headers={ |
| 322 |
"X-API-Secret": "foo", |
| 323 |
"Authorization": "bar", |
| 324 |
"Proxy-Authorization": "baz", |
| 325 |
"Cookie": "foo=bar", |
| 326 |
}, |
| 327 |
retries=Retry(remove_headers_on_redirect=["X-API-Secret"]), |
| 328 |
) |
| 329 |
|
| 330 |
assert r.status == 200 |
| 331 |
|
| 332 |
data = r.json() |
| 333 |
|
| 334 |
assert "X-API-Secret" not in data |
| 335 |
assert data["Authorization"] == "bar" |
| 336 |
assert data["Proxy-Authorization"] == "baz" |
| 337 |
assert data["Cookie"] == "foo=bar" |
| 338 |
|
| 339 |
headers = { |
| 340 |
"x-api-secret": "foo", |
| 341 |
"authorization": "bar", |
| 342 |
"proxy-authorization": "baz", |
| 343 |
"cookie": "foo=bar", |
| 344 |
} |
| 345 |
r = http.request( |
| 346 |
"GET", |
| 347 |
f"{self.base_url}/redirect", |
| 348 |
fields={"target": f"{self.base_url_alt}/headers"}, |
| 349 |
headers=headers, |
| 350 |
retries=Retry(remove_headers_on_redirect=["X-API-Secret"]), |
| 351 |
) |
| 352 |
|
| 353 |
assert r.status == 200 |
| 354 |
|
| 355 |
data = r.json() |
| 356 |
|
| 357 |
assert "x-api-secret" not in data |
| 358 |
assert "X-API-Secret" not in data |
| 359 |
assert data["Authorization"] == "bar" |
| 360 |
assert data["Proxy-Authorization"] == "baz" |
| 361 |
assert data["Cookie"] == "foo=bar" |
| 362 |
|
| 363 |
# Ensure the header argument itself is not modified in-place. |
| 364 |
assert headers == { |
| 365 |
"x-api-secret": "foo", |
| 366 |
"authorization": "bar", |
| 367 |
"proxy-authorization": "baz", |
| 368 |
"cookie": "foo=bar", |
| 369 |
} |
| 370 |
|
| 371 |
def test_redirect_without_preload_releases_connection(self) -> None: |
| 372 |
with PoolManager(block=True, maxsize=2) as http: |
| 373 |
r = http.request("GET", f"{self.base_url}/redirect", preload_content=False) |
| 374 |
assert isinstance(r, HTTPResponse) |
| 375 |
assert r._pool is not None |
| 376 |
assert r._pool.num_requests == 2 |
| 377 |
assert r._pool.num_connections == 1 |
| 378 |
assert len(http.pools) == 1 |
| 379 |
|
| 380 |
def test_303_redirect_makes_request_lose_body(self) -> None: |
| 381 |
with PoolManager() as http: |
| 382 |
response = http.request( |
| 383 |
"POST", |
| 384 |
f"{self.base_url}/redirect", |
| 385 |
fields={ |
| 386 |
"target": f"{self.base_url}/headers_and_params", |
| 387 |
"status": "303 See Other", |
| 388 |
}, |
| 389 |
) |
| 390 |
data = response.json() |
| 391 |
assert data["params"] == {} |
| 392 |
assert "Content-Type" not in HTTPHeaderDict(data["headers"]) |
| 393 |
|
| 394 |
def test_unknown_scheme(self) -> None: |
| 395 |
with PoolManager() as http: |
| 396 |
unknown_scheme = "unknown" |
| 397 |
unknown_scheme_url = f"{unknown_scheme}://host" |
| 398 |
with pytest.raises(URLSchemeUnknown) as e: |
| 399 |
r = http.request("GET", unknown_scheme_url) |
| 400 |
assert e.value.scheme == unknown_scheme |
| 401 |
r = http.request( |
| 402 |
"GET", |
| 403 |
f"{self.base_url}/redirect", |
| 404 |
fields={"target": unknown_scheme_url}, |
| 405 |
redirect=False, |
| 406 |
) |
| 407 |
assert r.status == 303 |
| 408 |
assert r.headers.get("Location") == unknown_scheme_url |
| 409 |
with pytest.raises(URLSchemeUnknown) as e: |
| 410 |
r = http.request( |
| 411 |
"GET", |
| 412 |
f"{self.base_url}/redirect", |
| 413 |
fields={"target": unknown_scheme_url}, |
| 414 |
) |
| 415 |
assert e.value.scheme == unknown_scheme |
| 416 |
|
| 417 |
def test_raise_on_redirect(self) -> None: |
| 418 |
with PoolManager() as http: |
| 419 |
r = http.request( |
| 420 |
"GET", |
| 421 |
f"{self.base_url}/redirect", |
| 422 |
fields={"target": f"{self.base_url}/redirect?target={self.base_url}/"}, |
| 423 |
retries=Retry(total=None, redirect=1, raise_on_redirect=False), |
| 424 |
) |
| 425 |
|
| 426 |
assert r.status == 303 |
| 427 |
|
| 428 |
def test_raise_on_status(self) -> None: |
| 429 |
with PoolManager() as http: |
| 430 |
with pytest.raises(MaxRetryError): |
| 431 |
# the default is to raise |
| 432 |
r = http.request( |
| 433 |
"GET", |
| 434 |
f"{self.base_url}/status", |
| 435 |
fields={"status": "500 Internal Server Error"}, |
| 436 |
retries=Retry(total=1, status_forcelist=range(500, 600)), |
| 437 |
) |
| 438 |
|
| 439 |
with pytest.raises(MaxRetryError): |
| 440 |
# raise explicitly |
| 441 |
r = http.request( |
| 442 |
"GET", |
| 443 |
f"{self.base_url}/status", |
| 444 |
fields={"status": "500 Internal Server Error"}, |
| 445 |
retries=Retry( |
| 446 |
total=1, status_forcelist=range(500, 600), raise_on_status=True |
| 447 |
), |
| 448 |
) |
| 449 |
|
| 450 |
# don't raise |
| 451 |
r = http.request( |
| 452 |
"GET", |
| 453 |
f"{self.base_url}/status", |
| 454 |
fields={"status": "500 Internal Server Error"}, |
| 455 |
retries=Retry( |
| 456 |
total=1, status_forcelist=range(500, 600), raise_on_status=False |
| 457 |
), |
| 458 |
) |
| 459 |
|
| 460 |
assert r.status == 500 |
| 461 |
|
| 462 |
def test_missing_port(self) -> None: |
| 463 |
# Can a URL that lacks an explicit port like ':80' succeed, or |
| 464 |
# will all such URLs fail with an error? |
| 465 |
|
| 466 |
with PoolManager() as http: |
| 467 |
# By globally adjusting `port_by_scheme` we pretend for a moment |
| 468 |
# that HTTP's default port is not 80, but is the port at which |
| 469 |
# our test server happens to be listening. |
| 470 |
port_by_scheme["http"] = self.port |
| 471 |
try: |
| 472 |
r = http.request("GET", f"http://{self.host}/", retries=0) |
| 473 |
finally: |
| 474 |
port_by_scheme["http"] = 80 |
| 475 |
|
| 476 |
assert r.status == 200 |
| 477 |
assert r.data == b"Dummy server!" |
| 478 |
|
| 479 |
def test_headers(self) -> None: |
| 480 |
with PoolManager(headers={"Foo": "bar"}) as http: |
| 481 |
r = http.request("GET", f"{self.base_url}/headers") |
| 482 |
returned_headers = r.json() |
| 483 |
assert returned_headers.get("Foo") == "bar" |
| 484 |
|
| 485 |
r = http.request("POST", f"{self.base_url}/headers") |
| 486 |
returned_headers = r.json() |
| 487 |
assert returned_headers.get("Foo") == "bar" |
| 488 |
|
| 489 |
r = http.request_encode_url("GET", f"{self.base_url}/headers") |
| 490 |
returned_headers = r.json() |
| 491 |
assert returned_headers.get("Foo") == "bar" |
| 492 |
|
| 493 |
r = http.request_encode_body("POST", f"{self.base_url}/headers") |
| 494 |
returned_headers = r.json() |
| 495 |
assert returned_headers.get("Foo") == "bar" |
| 496 |
|
| 497 |
r = http.request_encode_url( |
| 498 |
"GET", f"{self.base_url}/headers", headers={"Baz": "quux"} |
| 499 |
) |
| 500 |
returned_headers = r.json() |
| 501 |
assert returned_headers.get("Foo") is None |
| 502 |
assert returned_headers.get("Baz") == "quux" |
| 503 |
|
| 504 |
r = http.request_encode_body( |
| 505 |
"GET", f"{self.base_url}/headers", headers={"Baz": "quux"} |
| 506 |
) |
| 507 |
returned_headers = r.json() |
| 508 |
assert returned_headers.get("Foo") is None |
| 509 |
assert returned_headers.get("Baz") == "quux" |
| 510 |
|
| 511 |
def test_headers_http_header_dict(self) -> None: |
| 512 |
# Test uses a list of headers to assert the order |
| 513 |
# that headers are sent in the request too. |
| 514 |
|
| 515 |
headers = HTTPHeaderDict() |
| 516 |
headers.add("Foo", "bar") |
| 517 |
headers.add("Multi", "1") |
| 518 |
headers.add("Baz", "quux") |
| 519 |
headers.add("Multi", "2") |
| 520 |
|
| 521 |
with PoolManager(headers=headers) as http: |
| 522 |
r = http.request("GET", f"{self.base_url}/multi_headers") |
| 523 |
returned_headers = r.json()["headers"] |
| 524 |
assert returned_headers[-4:] == [ |
| 525 |
["Foo", "bar"], |
| 526 |
["Multi", "1"], |
| 527 |
["Multi", "2"], |
| 528 |
["Baz", "quux"], |
| 529 |
] |
| 530 |
|
| 531 |
r = http.request( |
| 532 |
"GET", |
| 533 |
f"{self.base_url}/multi_headers", |
| 534 |
headers={ |
| 535 |
**headers, |
| 536 |
"Extra": "extra", |
| 537 |
"Foo": "new", |
| 538 |
}, |
| 539 |
) |
| 540 |
returned_headers = r.json()["headers"] |
| 541 |
assert returned_headers[-4:] == [ |
| 542 |
["Foo", "new"], |
| 543 |
["Multi", "1, 2"], |
| 544 |
["Baz", "quux"], |
| 545 |
["Extra", "extra"], |
| 546 |
] |
| 547 |
|
| 548 |
def test_merge_headers_with_pool_manager_headers(self) -> None: |
| 549 |
headers = HTTPHeaderDict() |
| 550 |
headers.add("Cookie", "choc-chip") |
| 551 |
headers.add("Cookie", "oatmeal-raisin") |
| 552 |
orig = headers.copy() |
| 553 |
added_headers = {"Cookie": "tim-tam"} |
| 554 |
|
| 555 |
with PoolManager(headers=headers) as http: |
| 556 |
r = http.request( |
| 557 |
"GET", |
| 558 |
f"{self.base_url}/multi_headers", |
| 559 |
headers=typing.cast(HTTPHeaderDict, http.headers) | added_headers, |
| 560 |
) |
| 561 |
returned_headers = r.json()["headers"] |
| 562 |
assert returned_headers[-3:] == [ |
| 563 |
["Cookie", "choc-chip"], |
| 564 |
["Cookie", "oatmeal-raisin"], |
| 565 |
["Cookie", "tim-tam"], |
| 566 |
] |
| 567 |
# make sure the pool headers weren't modified |
| 568 |
assert http.headers == orig |
| 569 |
|
| 570 |
def test_headers_http_multi_header_multipart(self) -> None: |
| 571 |
headers = HTTPHeaderDict() |
| 572 |
headers.add("Multi", "1") |
| 573 |
headers.add("Multi", "2") |
| 574 |
old_headers = headers.copy() |
| 575 |
|
| 576 |
with PoolManager(headers=headers) as http: |
| 577 |
r = http.request( |
| 578 |
"POST", |
| 579 |
f"{self.base_url}/multi_headers", |
| 580 |
fields={"k": "v"}, |
| 581 |
multipart_boundary="b", |
| 582 |
encode_multipart=True, |
| 583 |
) |
| 584 |
returned_headers = r.json()["headers"] |
| 585 |
assert returned_headers[5:] == [ |
| 586 |
["Multi", "1"], |
| 587 |
["Multi", "2"], |
| 588 |
["Content-Type", "multipart/form-data; boundary=b"], |
| 589 |
] |
| 590 |
# Assert that the previous headers weren't modified. |
| 591 |
assert headers == old_headers |
| 592 |
|
| 593 |
# Set a default value for the Content-Type |
| 594 |
headers["Content-Type"] = "multipart/form-data; boundary=b; field=value" |
| 595 |
r = http.request( |
| 596 |
"POST", |
| 597 |
f"{self.base_url}/multi_headers", |
| 598 |
fields={"k": "v"}, |
| 599 |
multipart_boundary="b", |
| 600 |
encode_multipart=True, |
| 601 |
) |
| 602 |
returned_headers = r.json()["headers"] |
| 603 |
assert returned_headers[5:] == [ |
| 604 |
["Multi", "1"], |
| 605 |
["Multi", "2"], |
| 606 |
# Uses the set value, not the one that would be generated. |
| 607 |
["Content-Type", "multipart/form-data; boundary=b; field=value"], |
| 608 |
] |
| 609 |
|
| 610 |
def test_body(self) -> None: |
| 611 |
with PoolManager() as http: |
| 612 |
r = http.request("POST", f"{self.base_url}/echo", body=b"test") |
| 613 |
assert r.data == b"test" |
| 614 |
|
| 615 |
def test_http_with_ssl_keywords(self) -> None: |
| 616 |
with PoolManager(ca_certs="REQUIRED") as http: |
| 617 |
r = http.request("GET", f"http://{self.host}:{self.port}/") |
| 618 |
assert r.status == 200 |
| 619 |
|
| 620 |
def test_http_with_server_hostname(self) -> None: |
| 621 |
with PoolManager(server_hostname="example.com") as http: |
| 622 |
r = http.request("GET", f"http://{self.host}:{self.port}/") |
| 623 |
assert r.status == 200 |
| 624 |
|
| 625 |
def test_http_with_ca_cert_dir(self) -> None: |
| 626 |
with PoolManager(ca_certs="REQUIRED", ca_cert_dir="/nosuchdir") as http: |
| 627 |
r = http.request("GET", f"http://{self.host}:{self.port}/") |
| 628 |
assert r.status == 200 |
| 629 |
|
| 630 |
@pytest.mark.parametrize( |
| 631 |
["target", "expected_target"], |
| 632 |
[ |
| 633 |
# annoyingly quart.request.full_path adds a stray `?` |
| 634 |
("/echo_uri", b"/echo_uri?"), |
| 635 |
("/echo_uri?q=1#fragment", b"/echo_uri?q=1"), |
| 636 |
("/echo_uri?#", b"/echo_uri?"), |
| 637 |
("/echo_uri#!", b"/echo_uri?"), |
| 638 |
("/echo_uri#!#", b"/echo_uri?"), |
| 639 |
("/echo_uri??#", b"/echo_uri??"), |
| 640 |
("/echo_uri?%3f#", b"/echo_uri?%3F"), |
| 641 |
("/echo_uri?%3F#", b"/echo_uri?%3F"), |
| 642 |
("/echo_uri?[]", b"/echo_uri?%5B%5D"), |
| 643 |
], |
| 644 |
) |
| 645 |
def test_encode_http_target(self, target: str, expected_target: bytes) -> None: |
| 646 |
with PoolManager() as http: |
| 647 |
url = f"http://{self.host}:{self.port}{target}" |
| 648 |
r = http.request("GET", url) |
| 649 |
assert r.data == expected_target |
| 650 |
|
| 651 |
def test_top_level_request(self) -> None: |
| 652 |
r = request("GET", f"{self.base_url}/") |
| 653 |
assert r.status == 200 |
| 654 |
assert r.data == b"Dummy server!" |
| 655 |
|
| 656 |
def test_top_level_request_without_keyword_args(self) -> None: |
| 657 |
body = "" |
| 658 |
with pytest.raises(TypeError): |
| 659 |
request("GET", f"{self.base_url}/", body) # type: ignore[misc] |
| 660 |
|
| 661 |
def test_top_level_request_with_body(self) -> None: |
| 662 |
r = request("POST", f"{self.base_url}/echo", body=b"test") |
| 663 |
assert r.status == 200 |
| 664 |
assert r.data == b"test" |
| 665 |
|
| 666 |
def test_top_level_request_with_preload_content(self) -> None: |
| 667 |
r = request("GET", f"{self.base_url}/echo", preload_content=False) |
| 668 |
assert r.status == 200 |
| 669 |
assert r.connection is not None |
| 670 |
r.data |
| 671 |
assert r.connection is None |
| 672 |
|
| 673 |
def test_top_level_request_with_decode_content(self) -> None: |
| 674 |
r = request( |
| 675 |
"GET", |
| 676 |
f"{self.base_url}/encodingrequest", |
| 677 |
headers={"accept-encoding": "gzip"}, |
| 678 |
decode_content=False, |
| 679 |
) |
| 680 |
assert r.status == 200 |
| 681 |
assert gzip.decompress(r.data) == b"hello, world!" |
| 682 |
|
| 683 |
r = request( |
| 684 |
"GET", |
| 685 |
f"{self.base_url}/encodingrequest", |
| 686 |
headers={"accept-encoding": "gzip"}, |
| 687 |
decode_content=True, |
| 688 |
) |
| 689 |
assert r.status == 200 |
| 690 |
assert r.data == b"hello, world!" |
| 691 |
|
| 692 |
def test_top_level_request_with_redirect(self) -> None: |
| 693 |
r = request( |
| 694 |
"GET", |
| 695 |
f"{self.base_url}/redirect", |
| 696 |
fields={"target": f"{self.base_url}/"}, |
| 697 |
redirect=False, |
| 698 |
) |
| 699 |
|
| 700 |
assert r.status == 303 |
| 701 |
|
| 702 |
r = request( |
| 703 |
"GET", |
| 704 |
f"{self.base_url}/redirect", |
| 705 |
fields={"target": f"{self.base_url}/"}, |
| 706 |
redirect=True, |
| 707 |
) |
| 708 |
|
| 709 |
assert r.status == 200 |
| 710 |
assert r.data == b"Dummy server!" |
| 711 |
|
| 712 |
def test_top_level_request_with_retries(self) -> None: |
| 713 |
r = request("GET", f"{self.base_url}/redirect", retries=False) |
| 714 |
assert r.status == 303 |
| 715 |
|
| 716 |
r = request("GET", f"{self.base_url}/redirect", retries=3) |
| 717 |
assert r.status == 200 |
| 718 |
|
| 719 |
def test_top_level_request_with_timeout(self) -> None: |
| 720 |
with mock.patch("urllib3.poolmanager.RequestMethods.request") as mockRequest: |
| 721 |
mockRequest.return_value = HTTPResponse(status=200) |
| 722 |
|
| 723 |
r = request("GET", f"{self.base_url}/redirect", timeout=2.5) |
| 724 |
|
| 725 |
assert r.status == 200 |
| 726 |
|
| 727 |
mockRequest.assert_called_with( |
| 728 |
"GET", |
| 729 |
f"{self.base_url}/redirect", |
| 730 |
body=None, |
| 731 |
fields=None, |
| 732 |
headers=None, |
| 733 |
preload_content=True, |
| 734 |
decode_content=True, |
| 735 |
redirect=True, |
| 736 |
retries=None, |
| 737 |
timeout=2.5, |
| 738 |
json=None, |
| 739 |
) |
| 740 |
|
| 741 |
@pytest.mark.parametrize( |
| 742 |
"headers", |
| 743 |
[ |
| 744 |
None, |
| 745 |
{"content-Type": "application/json"}, |
| 746 |
{"content-Type": "text/plain"}, |
| 747 |
{"attribute": "value", "CONTENT-TYPE": "application/json"}, |
| 748 |
HTTPHeaderDict(cookie="foo, bar"), |
| 749 |
], |
| 750 |
) |
| 751 |
def test_request_with_json(self, headers: HTTPHeaderDict) -> None: |
| 752 |
old_headers = None if headers is None else headers.copy() |
| 753 |
body = {"attribute": "value"} |
| 754 |
r = request( |
| 755 |
method="POST", url=f"{self.base_url}/echo_json", headers=headers, json=body |
| 756 |
) |
| 757 |
assert r.status == 200 |
| 758 |
assert r.json() == body |
| 759 |
content_type = HTTPHeaderDict(old_headers).get( |
| 760 |
"Content-Type", "application/json" |
| 761 |
) |
| 762 |
assert content_type in r.headers["Content-Type"].replace(" ", "").split(",") |
| 763 |
|
| 764 |
# Ensure the header argument itself is not modified in-place. |
| 765 |
assert headers == old_headers |
| 766 |
|
| 767 |
def test_top_level_request_with_json_with_httpheaderdict(self) -> None: |
| 768 |
body = {"attribute": "value"} |
| 769 |
header = HTTPHeaderDict(cookie="foo, bar") |
| 770 |
with PoolManager(headers=header) as http: |
| 771 |
r = http.request(method="POST", url=f"{self.base_url}/echo_json", json=body) |
| 772 |
assert r.status == 200 |
| 773 |
assert r.json() == body |
| 774 |
assert "application/json" in r.headers["Content-Type"].replace( |
| 775 |
" ", "" |
| 776 |
).split(",") |
| 777 |
|
| 778 |
def test_top_level_request_with_body_and_json(self) -> None: |
| 779 |
match = "request got values for both 'body' and 'json' parameters which are mutually exclusive" |
| 780 |
with pytest.raises(TypeError, match=match): |
| 781 |
body = {"attribute": "value"} |
| 782 |
request(method="POST", url=f"{self.base_url}/echo", body="", json=body) |
| 783 |
|
| 784 |
def test_top_level_request_with_invalid_body(self) -> None: |
| 785 |
class BadBody: |
| 786 |
def __repr__(self) -> str: |
| 787 |
return "<BadBody>" |
| 788 |
|
| 789 |
with pytest.raises(TypeError) as e: |
| 790 |
request( |
| 791 |
method="POST", |
| 792 |
url=f"{self.base_url}/echo", |
| 793 |
body=BadBody(), # type: ignore[arg-type] |
| 794 |
) |
| 795 |
assert str(e.value) == ( |
| 796 |
"'body' must be a bytes-like object, file-like " |
| 797 |
"object, or iterable. Instead was <BadBody>" |
| 798 |
) |
| 799 |
|
| 800 |
|
| 801 |
@pytest.mark.skipif(not HAS_IPV6, reason="IPv6 is not supported on this system") |
| 802 |
class TestIPv6PoolManager(IPv6HypercornDummyServerTestCase): |
| 803 |
@classmethod |
| 804 |
def setup_class(cls) -> None: |
| 805 |
super().setup_class() |
| 806 |
cls.base_url = f"http://[{cls.host}]:{cls.port}" |
| 807 |
|
| 808 |
def test_ipv6(self) -> None: |
| 809 |
with PoolManager() as http: |
| 810 |
http.request("GET", self.base_url) |