|
14 | 14 | import io |
15 | 15 | import requests |
16 | 16 | import pytest |
| 17 | +import urllib3 |
17 | 18 | from requests.adapters import HTTPAdapter |
18 | 19 | from requests.auth import HTTPDigestAuth, _basic_auth_str |
19 | 20 | from requests.compat import ( |
|
22 | 23 | from requests.cookies import ( |
23 | 24 | cookiejar_from_dict, morsel_to_cookie) |
24 | 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 RequestsSSLError |
28 | 45 | from requests.models import PreparedRequest |
29 | 46 | from requests.structures import CaseInsensitiveDict |
30 | 47 | from requests.sessions import SessionRedirectMixin |
@@ -910,7 +927,7 @@ def test_certificate_failure(self, httpbin_secure): |
910 | 927 | """ |
911 | 928 | When underlying SSL problems occur, an SSLError is raised. |
912 | 929 | """ |
913 | | - with pytest.raises(SSLError): |
| 930 | + with pytest.raises(RequestsSSLError): |
914 | 931 | # Our local httpbin does not have a trusted CA, so this call will |
915 | 932 | # fail if we use our default trust bundle. |
916 | 933 | requests.get(httpbin_secure('status', '200')) |
@@ -1320,6 +1337,26 @@ def test_response_chunk_size_type(self): |
1320 | 1337 | with pytest.raises(TypeError): |
1321 | 1338 | chunks = r.iter_content("1024") |
1322 | 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, expected |
| 1350 | + ): |
| 1351 | + r = requests.Response() |
| 1352 | + r.raw = mocker.Mock() |
| 1353 | + # ReadTimeoutError can't be initialized by mock |
| 1354 | + # so we'll manually create the instance with args |
| 1355 | + r.raw.stream.side_effect = exception(*args) |
| 1356 | + |
| 1357 | + with pytest.raises(expected): |
| 1358 | + next(r.iter_content(1024)) |
| 1359 | + |
1323 | 1360 | def test_request_and_response_are_pickleable(self, httpbin): |
1324 | 1361 | r = requests.get(httpbin('get')) |
1325 | 1362 |
|
|
0 commit comments