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