| 1 |
from __future__ import annotations |
| 2 |
|
| 3 |
import pickle |
| 4 |
import socket |
| 5 |
from email.errors import MessageDefect |
| 6 |
from test import DUMMY_POOL |
| 7 |
|
| 8 |
import pytest |
| 9 |
|
| 10 |
from urllib3.connection import HTTPConnection |
| 11 |
from urllib3.connectionpool import HTTPConnectionPool |
| 12 |
from urllib3.exceptions import ( |
| 13 |
ClosedPoolError, |
| 14 |
ConnectTimeoutError, |
| 15 |
EmptyPoolError, |
| 16 |
HeaderParsingError, |
| 17 |
HostChangedError, |
| 18 |
HTTPError, |
| 19 |
LocationParseError, |
| 20 |
MaxRetryError, |
| 21 |
NameResolutionError, |
| 22 |
NewConnectionError, |
| 23 |
ReadTimeoutError, |
| 24 |
) |
| 25 |
|
| 26 |
|
| 27 |
class TestPickle: |
| 28 |
@pytest.mark.parametrize( |
| 29 |
"exception", |
| 30 |
[ |
| 31 |
HTTPError(None), |
| 32 |
MaxRetryError(DUMMY_POOL, "", None), |
| 33 |
MaxRetryError(DUMMY_POOL, "", Exception("Error occured")), |
| 34 |
LocationParseError(""), |
| 35 |
ConnectTimeoutError(None), |
| 36 |
HTTPError("foo"), |
| 37 |
HTTPError("foo", IOError("foo")), |
| 38 |
MaxRetryError(HTTPConnectionPool("localhost"), "/", None), |
| 39 |
LocationParseError("fake location"), |
| 40 |
ClosedPoolError(HTTPConnectionPool("localhost"), ""), |
| 41 |
EmptyPoolError(HTTPConnectionPool("localhost"), ""), |
| 42 |
HostChangedError(HTTPConnectionPool("localhost"), "/", 0), |
| 43 |
ReadTimeoutError(HTTPConnectionPool("localhost"), "/", ""), |
| 44 |
ReadTimeoutError(HTTPConnectionPool("localhost"), "/", "message"), |
| 45 |
NewConnectionError(HTTPConnection("localhost"), ""), |
| 46 |
NameResolutionError("", HTTPConnection("localhost"), socket.gaierror()), |
| 47 |
NameResolutionError( |
| 48 |
"host", HTTPConnection("localhost"), socket.gaierror("error") |
| 49 |
), |
| 50 |
], |
| 51 |
) |
| 52 |
def test_exceptions(self, exception: Exception) -> None: |
| 53 |
result = pickle.loads(pickle.dumps(exception)) |
| 54 |
assert isinstance(result, type(exception)) |
| 55 |
|
| 56 |
if hasattr(exception, "_message"): |
| 57 |
assert exception._message == result._message # type: ignore[attr-defined] |
| 58 |
assert exception._message in str(result) |
| 59 |
|
| 60 |
if hasattr(exception, "_host"): |
| 61 |
# host is likely a string so directly comparable |
| 62 |
assert exception._host == result._host # type: ignore[attr-defined] |
| 63 |
|
| 64 |
if hasattr(exception, "_reason"): |
| 65 |
# reason is likely an exception so do string comparison instead |
| 66 |
assert str(exception._reason) == str(result._reason) # type: ignore[attr-defined] |
| 67 |
|
| 68 |
|
| 69 |
class TestFormat: |
| 70 |
def test_header_parsing_errors(self) -> None: |
| 71 |
hpe = HeaderParsingError([MessageDefect("defects")], "unparsed_data") |
| 72 |
|
| 73 |
assert "defects" in str(hpe) |
| 74 |
assert "unparsed_data" in str(hpe) |
| 75 |
|
| 76 |
|
| 77 |
class TestNewConnectionError: |
| 78 |
def test_pool_property_deprecation_warning(self) -> None: |
| 79 |
err = NewConnectionError(HTTPConnection("localhost"), "test") |
| 80 |
with pytest.warns(FutureWarning) as records: |
| 81 |
err_pool = err.pool |
| 82 |
|
| 83 |
assert err_pool is err.conn |
| 84 |
msg = ( |
| 85 |
"The 'pool' property is deprecated and will be removed " |
| 86 |
"in urllib3 v3.0. Use 'conn' instead." |
| 87 |
) |
| 88 |
record = records[0] |
| 89 |
assert isinstance(record.message, Warning) |
| 90 |
assert record.message.args[0] == msg |