Wrap urllib3's SSLError as requests' SSLError (#6057)
Before adoption: this commit came in with the history the project adopted, so no landing records it.
authored by Nate Prewitt · committed by GitHub · parent 7ae3887 · Browse files · 3 files·+55 added−5 removed
This commit
State
Before adoption: this commit came in with the history the project adopted, so no landing records it.
Diff stats
3 files+55 added−5 removed
- tests:+41 added−4 removed
- other:+14 added−1 removed
Provenance
- Commit
95f4567- Tree
3e1a0cd- Author
Nate Prewitt <nate.prewitt@gmail.com>- Committer
GitHub <noreply@github.com>- Parent
7ae3887
3 files changed+55 added−5 removed
modified:requests/models.py+9 added-1 removed
17 unmodified lines18 from urllib3.fields import RequestField19 from urllib3.filepost import encode_multipart_formdata20 from urllib3.util import parse_url21 from urllib3.exceptions import (22- DecodeError, ReadTimeoutError, ProtocolError, LocationParseError)22+ DecodeError,23+ LocationParseError,24+ ProtocolError,25+ ReadTimeoutError,26+ SSLError,27+)28 29 from io import UnsupportedOperation30 from .hooks import default_hooks31 from .structures import CaseInsensitiveDict4 unmodified lines36 HTTPError, MissingSchema, InvalidURL, ChunkedEncodingError,37 ContentDecodingError, ConnectionError, StreamConsumedError,38 InvalidJSONError)39 from .exceptions import JSONDecodeError as RequestsJSONDecodeError40+from .exceptions import SSLError as RequestsSSLError41 from ._internal_utils import to_native_string, unicode_is_ascii42 from .utils import (43 guess_filename, get_auth_from_url, requote_uri,44 stream_decode_response_unicode, to_key_val_list, parse_header_links,725 unmodified lines770 except DecodeError as e:771 raise ContentDecodingError(e)772 except ReadTimeoutError as e:773 raise ConnectionError(e)774+ except SSLError as e:775+ raise RequestsSSLError(e)776 else:777 # Standard file-like object.778 while True:779 chunk = self.raw.read(chunk_size)202 unmodified linesmodified:tests/test_requests.py+41 added-4 removed
12 unmodified lines13 14 import io15 import requests16 import pytest17+import urllib318 from requests.adapters import HTTPAdapter19 from requests.auth import HTTPDigestAuth, _basic_auth_str20 from requests.compat import (21 Morsel, cookielib, getproxies, str, urlparse,22 builtin_str)23 from requests.cookies import (24 cookiejar_from_dict, morsel_to_cookie)25 from requests.exceptions import (25- ConnectionError, ConnectTimeout, InvalidSchema, InvalidURL,26- MissingSchema, ReadTimeout, Timeout, RetryError, RequestException, TooManyRedirects,27- ProxyError, InvalidHeader, UnrewindableBodyError, SSLError, InvalidProxyURL, InvalidJSONError)26+ ChunkedEncodingError,27+ ConnectionError,28+ ConnectTimeout,29+ ContentDecodingError,30+ InvalidHeader,31+ InvalidJSONError,32+ InvalidProxyURL,33+ InvalidSchema,34+ InvalidURL,35+ MissingSchema,36+ ProxyError,37+ ReadTimeout,38+ RequestException,39+ RetryError,40+ Timeout,41+ TooManyRedirects,42+ UnrewindableBodyError,43+)44+from requests.exceptions import SSLError as RequestsSSLError45 from requests.models import PreparedRequest46 from requests.structures import CaseInsensitiveDict47 from requests.sessions import SessionRedirectMixin48 from requests.models import urlencode877 unmodified lines926 def test_certificate_failure(self, httpbin_secure):927 """928 When underlying SSL problems occur, an SSLError is raised.929 """913- with pytest.raises(SSLError):930+ with pytest.raises(RequestsSSLError):931 # Our local httpbin does not have a trusted CA, so this call will932 # fail if we use our default trust bundle.933 requests.get(httpbin_secure('status', '200'))934 401 unmodified lines1336 r.raw = io.BytesIO(b'the content')1337 with pytest.raises(TypeError):1338 chunks = r.iter_content("1024")1339 1340+ @pytest.mark.parametrize(1341+ 'exception, args, expected', (1342+ (urllib3.exceptions.ProtocolError, tuple(), ChunkedEncodingError),1343+ (urllib3.exceptions.DecodeError, tuple(), ContentDecodingError),1344+ (urllib3.exceptions.ReadTimeoutError, (None, '', ''), ConnectionError),1345+ (urllib3.exceptions.SSLError, tuple(), RequestsSSLError),1346+ )1347+ )1348+ def test_iter_content_wraps_exceptions(1349+ self, httpbin, mocker, exception, args, expected1350+ ):1351+ r = requests.Response()1352+ r.raw = mocker.Mock()1353+ # ReadTimeoutError can't be initialized by mock1354+ # so we'll manually create the instance with args1355+ r.raw.stream.side_effect = exception(*args)1356+1357+ with pytest.raises(expected):1358+ next(r.iter_content(1024))1359+1360 def test_request_and_response_are_pickleable(self, httpbin):1361 r = requests.get(httpbin('get'))1362 1363 # verify we can pickle the original request1273 unmodified linesmodified:HISTORY.md+5 added
4 unmodified lines5 ---6 7 - \[Short description of non-trivial change.\]8 9+**Bugfixes**10+11+- Fixed urllib3 exception leak, wrapping `urllib3.exceptions.SSLError` with12+ `requests.exceptions.SSLError` for `content` and `iter_content`.13+14 2.27.1 (2022-01-05)15 -------------------16 17 **Bugfixes**1795 unmodified lines