1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338
|
# Tests for client_exceptions.py
import errno
import pickle
from unittest import mock
import pytest
from multidict import CIMultiDict
from yarl import URL
from aiohttp import client, client_reqrep
class TestClientResponseError:
request_info = client.RequestInfo(
url="http://example.com",
method="GET",
headers={},
real_url="http://example.com",
)
def test_default_status(self) -> None:
err = client.ClientResponseError(history=(), request_info=self.request_info)
assert err.status == 0
def test_status(self) -> None:
err = client.ClientResponseError(
status=400, history=(), request_info=self.request_info
)
assert err.status == 400
def test_pickle(self) -> None:
err = client.ClientResponseError(request_info=self.request_info, history=())
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(err, proto)
err2 = pickle.loads(pickled)
assert err2.request_info == self.request_info
assert err2.history == ()
assert err2.status == 0
assert err2.message == ""
assert err2.headers is None
err = client.ClientResponseError(
request_info=self.request_info,
history=(),
status=400,
message="Something wrong",
headers=CIMultiDict(foo="bar"),
)
err.foo = "bar"
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(err, proto)
err2 = pickle.loads(pickled)
assert err2.request_info == self.request_info
assert err2.history == ()
assert err2.status == 400
assert err2.message == "Something wrong"
# Use headers.get() to verify static type is correct.
assert err2.headers.get("foo") == "bar"
assert err2.foo == "bar"
def test_repr(self) -> None:
err = client.ClientResponseError(request_info=self.request_info, history=())
assert repr(err) == (f"ClientResponseError({self.request_info!r}, ())")
err = client.ClientResponseError(
request_info=self.request_info,
history=(),
status=400,
message="Something wrong",
headers=CIMultiDict(),
)
assert repr(err) == (
"ClientResponseError(%r, (), status=400, "
"message='Something wrong', headers=<CIMultiDict()>)" % (self.request_info,)
)
def test_str(self) -> None:
err = client.ClientResponseError(
request_info=self.request_info,
history=(),
status=400,
message="Something wrong",
headers=CIMultiDict(),
)
assert str(err) == ("400, message='Something wrong', url='http://example.com'")
def test_response_status() -> None:
request_info = mock.Mock(real_url="http://example.com")
err = client.ClientResponseError(
status=400, history=None, request_info=request_info
)
assert err.status == 400
def test_response_deprecated_code_property() -> None:
request_info = mock.Mock(real_url="http://example.com")
with pytest.warns(DeprecationWarning):
err = client.ClientResponseError(
code=400, history=None, request_info=request_info
)
with pytest.warns(DeprecationWarning):
assert err.code == err.status
with pytest.warns(DeprecationWarning):
err.code = "404"
with pytest.warns(DeprecationWarning):
assert err.code == err.status
def test_response_both_code_and_status() -> None:
with pytest.raises(ValueError):
client.ClientResponseError(
code=400, status=400, history=None, request_info=None
)
class TestClientConnectorError:
connection_key = client_reqrep.ConnectionKey(
host="example.com",
port=8080,
is_ssl=False,
ssl=True,
proxy=None,
proxy_auth=None,
proxy_headers_hash=None,
)
def test_ctor(self) -> None:
err = client.ClientConnectorError(
connection_key=self.connection_key,
os_error=OSError(errno.ENOENT, "No such file"),
)
assert err.errno == errno.ENOENT
assert err.strerror == "No such file"
assert err.os_error.errno == errno.ENOENT
assert err.os_error.strerror == "No such file"
assert err.host == "example.com"
assert err.port == 8080
assert err.ssl is True
def test_pickle(self) -> None:
err = client.ClientConnectorError(
connection_key=self.connection_key,
os_error=OSError(errno.ENOENT, "No such file"),
)
err.foo = "bar"
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(err, proto)
err2 = pickle.loads(pickled)
assert err2.errno == errno.ENOENT
assert err2.strerror == "No such file"
assert err2.os_error.errno == errno.ENOENT
assert err2.os_error.strerror == "No such file"
assert err2.host == "example.com"
assert err2.port == 8080
assert err2.ssl is True
assert err2.foo == "bar"
def test_repr(self) -> None:
os_error = OSError(errno.ENOENT, "No such file")
err = client.ClientConnectorError(
connection_key=self.connection_key, os_error=os_error
)
assert repr(err) == (
f"ClientConnectorError({self.connection_key!r}, {os_error!r})"
)
def test_str(self) -> None:
err = client.ClientConnectorError(
connection_key=self.connection_key,
os_error=OSError(errno.ENOENT, "No such file"),
)
assert str(err) == (
"Cannot connect to host example.com:8080 ssl:default [No such file]"
)
class TestClientConnectorCertificateError:
connection_key = client_reqrep.ConnectionKey(
host="example.com",
port=8080,
is_ssl=False,
ssl=True,
proxy=None,
proxy_auth=None,
proxy_headers_hash=None,
)
def test_ctor(self) -> None:
certificate_error = Exception("Bad certificate")
err = client.ClientConnectorCertificateError(
connection_key=self.connection_key, certificate_error=certificate_error
)
assert err.certificate_error == certificate_error
assert err.host == "example.com"
assert err.port == 8080
assert err.ssl is False
def test_pickle(self) -> None:
certificate_error = Exception("Bad certificate")
err = client.ClientConnectorCertificateError(
connection_key=self.connection_key, certificate_error=certificate_error
)
err.foo = "bar"
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(err, proto)
err2 = pickle.loads(pickled)
assert err2.certificate_error.args == ("Bad certificate",)
assert err2.host == "example.com"
assert err2.port == 8080
assert err2.ssl is False
assert err2.foo == "bar"
def test_repr(self) -> None:
certificate_error = Exception("Bad certificate")
err = client.ClientConnectorCertificateError(
connection_key=self.connection_key, certificate_error=certificate_error
)
assert repr(err) == (
"ClientConnectorCertificateError(%r, %r)"
% (self.connection_key, certificate_error)
)
def test_str(self) -> None:
certificate_error = Exception("Bad certificate")
err = client.ClientConnectorCertificateError(
connection_key=self.connection_key, certificate_error=certificate_error
)
assert str(err) == (
"Cannot connect to host example.com:8080 ssl:False"
" [Exception: ('Bad certificate',)]"
)
class TestServerDisconnectedError:
def test_ctor(self) -> None:
err = client.ServerDisconnectedError()
assert err.message == "Server disconnected"
err = client.ServerDisconnectedError(message="No connection")
assert err.message == "No connection"
def test_pickle(self) -> None:
err = client.ServerDisconnectedError(message="No connection")
err.foo = "bar"
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(err, proto)
err2 = pickle.loads(pickled)
assert err2.message == "No connection"
assert err2.foo == "bar"
def test_repr(self) -> None:
err = client.ServerDisconnectedError()
assert repr(err) == ("ServerDisconnectedError('Server disconnected')")
err = client.ServerDisconnectedError(message="No connection")
assert repr(err) == "ServerDisconnectedError('No connection')"
def test_str(self) -> None:
err = client.ServerDisconnectedError()
assert str(err) == "Server disconnected"
err = client.ServerDisconnectedError(message="No connection")
assert str(err) == "No connection"
class TestServerFingerprintMismatch:
def test_ctor(self) -> None:
err = client.ServerFingerprintMismatch(
expected=b"exp", got=b"got", host="example.com", port=8080
)
assert err.expected == b"exp"
assert err.got == b"got"
assert err.host == "example.com"
assert err.port == 8080
def test_pickle(self) -> None:
err = client.ServerFingerprintMismatch(
expected=b"exp", got=b"got", host="example.com", port=8080
)
err.foo = "bar"
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(err, proto)
err2 = pickle.loads(pickled)
assert err2.expected == b"exp"
assert err2.got == b"got"
assert err2.host == "example.com"
assert err2.port == 8080
assert err2.foo == "bar"
def test_repr(self) -> None:
err = client.ServerFingerprintMismatch(b"exp", b"got", "example.com", 8080)
assert repr(err) == (
"<ServerFingerprintMismatch expected=b'exp' "
"got=b'got' host='example.com' port=8080>"
)
class TestInvalidURL:
def test_ctor(self) -> None:
err = client.InvalidURL(url=":wrong:url:", description=":description:")
assert err.url == ":wrong:url:"
assert err.description == ":description:"
def test_pickle(self) -> None:
err = client.InvalidURL(url=":wrong:url:")
err.foo = "bar"
for proto in range(pickle.HIGHEST_PROTOCOL + 1):
pickled = pickle.dumps(err, proto)
err2 = pickle.loads(pickled)
assert err2.url == ":wrong:url:"
assert err2.foo == "bar"
def test_repr_no_description(self) -> None:
err = client.InvalidURL(url=":wrong:url:")
assert err.args == (":wrong:url:",)
assert repr(err) == "<InvalidURL :wrong:url:>"
def test_repr_yarl_URL(self) -> None:
err = client.InvalidURL(url=URL(":wrong:url:"))
assert repr(err) == "<InvalidURL :wrong:url:>"
def test_repr_with_description(self) -> None:
err = client.InvalidURL(url=":wrong:url:", description=":description:")
assert repr(err) == "<InvalidURL :wrong:url: - :description:>"
def test_str_no_description(self) -> None:
err = client.InvalidURL(url=":wrong:url:")
assert str(err) == ":wrong:url:"
def test_none_description(self) -> None:
err = client.InvalidURL(":wrong:url:")
assert err.description is None
def test_str_with_description(self) -> None:
err = client.InvalidURL(url=":wrong:url:", description=":description:")
assert str(err) == ":wrong:url: - :description:"
|