diff options
author | Quentin Pradet <quentin.pradet@gmail.com> | 2022-11-17 17:43:26 +0400 |
---|---|---|
committer | Seth Michael Larson <sethmichaellarson@gmail.com> | 2022-11-20 15:45:35 +0000 |
commit | b8c5d457fc42821b951ea58bec4ad685a0183c02 (patch) | |
tree | 142727e185989c28c0a8f22122abda546acbdacb | |
parent | 8b8e4b5a148d0eb706daf5ac48b4423b434495f5 (diff) | |
download | urllib3-b8c5d457fc42821b951ea58bec4ad685a0183c02.tar.gz |
[1.26] Deprecate HTTPResponse.getheaders() and .getheader() methods
-rw-r--r-- | src/urllib3/connectionpool.py | 2 | ||||
-rw-r--r-- | src/urllib3/response.py | 13 | ||||
-rw-r--r-- | src/urllib3/util/retry.py | 2 | ||||
-rw-r--r-- | test/test_response.py | 13 |
4 files changed, 26 insertions, 4 deletions
diff --git a/src/urllib3/connectionpool.py b/src/urllib3/connectionpool.py index 96339e90..70873927 100644 --- a/src/urllib3/connectionpool.py +++ b/src/urllib3/connectionpool.py @@ -862,7 +862,7 @@ class HTTPConnectionPool(ConnectionPool, RequestMethods): ) # Check if we should retry the HTTP response. - has_retry_after = bool(response.getheader("Retry-After")) + has_retry_after = bool(response.headers.get("Retry-After")) if retries.is_retry(method, response.status, has_retry_after): try: retries = retries.increment(method, url, response=response, _pool=self) diff --git a/src/urllib3/response.py b/src/urllib3/response.py index 01f08eee..8f1b4fa8 100644 --- a/src/urllib3/response.py +++ b/src/urllib3/response.py @@ -3,6 +3,7 @@ from __future__ import absolute_import import io import logging import sys +import warnings import zlib from contextlib import contextmanager from socket import error as SocketError @@ -663,9 +664,21 @@ class HTTPResponse(io.IOBase): # Backwards-compatibility methods for http.client.HTTPResponse def getheaders(self): + warnings.warn( + "HTTPResponse.getheaders() is deprecated and will be removed " + "in urllib3 v2.1.0. Instead access HTTResponse.headers directly.", + category=DeprecationWarning, + stacklevel=2, + ) return self.headers def getheader(self, name, default=None): + warnings.warn( + "HTTPResponse.getheader() is deprecated and will be removed " + "in urllib3 v2.1.0. Instead use HTTResponse.headers.get(name, default).", + category=DeprecationWarning, + stacklevel=2, + ) return self.headers.get(name, default) # Backwards compatibility for http.cookiejar diff --git a/src/urllib3/util/retry.py b/src/urllib3/util/retry.py index 3398323f..2490d5e5 100644 --- a/src/urllib3/util/retry.py +++ b/src/urllib3/util/retry.py @@ -394,7 +394,7 @@ class Retry(object): def get_retry_after(self, response): """Get the value of Retry-After in seconds.""" - retry_after = response.getheader("Retry-After") + retry_after = response.headers.get("Retry-After") if retry_after is None: return None diff --git a/test/test_response.py b/test/test_response.py index 03f2780c..e09e3858 100644 --- a/test/test_response.py +++ b/test/test_response.py @@ -13,6 +13,7 @@ import mock import pytest import six +from urllib3._collections import HTTPHeaderDict from urllib3.exceptions import ( DecodeError, IncompleteRead, @@ -57,12 +58,20 @@ class TestLegacyResponse(object): def test_getheaders(self): headers = {"host": "example.com"} r = HTTPResponse(headers=headers) - assert r.getheaders() == headers + with pytest.warns( + DeprecationWarning, + match=r"HTTPResponse.getheaders\(\) is deprecated", + ): + assert r.getheaders() == HTTPHeaderDict(headers) def test_getheader(self): headers = {"host": "example.com"} r = HTTPResponse(headers=headers) - assert r.getheader("host") == "example.com" + with pytest.warns( + DeprecationWarning, + match=r"HTTPResponse.getheader\(\) is deprecated", + ): + assert r.getheader("host") == "example.com" class TestResponse(object): |