mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
test: add missing test
Add missing test, remove BaseSession.cfg and switch to implementing class' "private" traits, add aiohttp_socks in dependency list as optional and extra.
This commit is contained in:
parent
1f8fa0c4e8
commit
edce7c4f75
6 changed files with 26 additions and 19 deletions
|
|
@ -1,6 +1,6 @@
|
|||
from __future__ import annotations
|
||||
|
||||
from typing import AsyncGenerator, Callable, Optional, TypeVar, Tuple, Dict, Any, Union, cast
|
||||
from typing import AsyncGenerator, Callable, Optional, TypeVar, Type, Tuple, Dict, Any, Union, cast
|
||||
|
||||
from aiohttp import ClientSession, ClientTimeout, FormData, BasicAuth, TCPConnector
|
||||
|
||||
|
|
@ -27,14 +27,14 @@ class AiohttpSession(BaseSession[_ProxyType]):
|
|||
proxy=proxy
|
||||
)
|
||||
self._session: Optional[ClientSession] = None
|
||||
self.cfg.connector_type = TCPConnector
|
||||
self.cfg.connector_init = cast(Dict[str, Any], {})
|
||||
self._connector_type: Type[TCPConnector] = TCPConnector
|
||||
self._connector_init: Dict[str, Any] = {}
|
||||
|
||||
if self.proxy:
|
||||
try:
|
||||
from aiohttp_socks import ProxyConnector
|
||||
from aiohttp_socks.utils import parse_proxy_url
|
||||
except ImportError as exc:
|
||||
except ImportError as exc: # pragma: no cover
|
||||
raise UserWarning(
|
||||
"In order to use aiohttp client for proxy requests, install "
|
||||
"https://pypi.org/project/aiohttp-socks/"
|
||||
|
|
@ -45,7 +45,7 @@ class AiohttpSession(BaseSession[_ProxyType]):
|
|||
else:
|
||||
proxy_url, proxy_auth = self.proxy
|
||||
|
||||
self.cfg.connector_type = ProxyConnector
|
||||
self._connector_type = ProxyConnector
|
||||
|
||||
proxy_type, host, port, username, password = parse_proxy_url(proxy_url)
|
||||
if proxy_auth:
|
||||
|
|
@ -54,7 +54,7 @@ class AiohttpSession(BaseSession[_ProxyType]):
|
|||
if not password:
|
||||
password = proxy_auth.password
|
||||
|
||||
self.cfg.connector_init.update(
|
||||
self._connector_init.update(
|
||||
dict(
|
||||
proxy_type=proxy_type, host=host, port=port,
|
||||
username=username, password=password,
|
||||
|
|
@ -65,7 +65,7 @@ class AiohttpSession(BaseSession[_ProxyType]):
|
|||
async def create_session(self) -> ClientSession:
|
||||
if self._session is None or self._session.closed:
|
||||
self._session = ClientSession(
|
||||
connector=self.cfg.connector_type(**self.cfg.connector_init)
|
||||
connector=self._connector_type(**self._connector_init)
|
||||
)
|
||||
|
||||
return self._session
|
||||
|
|
|
|||
|
|
@ -3,7 +3,6 @@ from __future__ import annotations
|
|||
import abc
|
||||
import datetime
|
||||
import json
|
||||
import types
|
||||
from typing import Any, AsyncGenerator, Callable, Optional, TypeVar, Union, Generic
|
||||
|
||||
from aiogram.utils.exceptions import TelegramAPIError
|
||||
|
|
@ -35,8 +34,6 @@ class BaseSession(abc.ABC, Generic[_ProxyType]):
|
|||
self.json_dumps = json_dumps
|
||||
self.proxy = proxy
|
||||
|
||||
self.cfg: types.SimpleNamespace = types.SimpleNamespace()
|
||||
|
||||
def raise_for_status(self, response: Response[T]) -> None:
|
||||
if response.ok:
|
||||
return
|
||||
|
|
|
|||
4
poetry.lock
generated
4
poetry.lock
generated
|
|
@ -25,7 +25,7 @@ yarl = ">=1.0,<2.0"
|
|||
speedups = ["aiodns", "brotlipy", "cchardet"]
|
||||
|
||||
[[package]]
|
||||
category = "dev"
|
||||
category = "main"
|
||||
description = "Proxy connector for aiohttp"
|
||||
name = "aiohttp-socks"
|
||||
optional = false
|
||||
|
|
@ -873,7 +873,7 @@ testing = ["jaraco.itertools", "func-timeout"]
|
|||
fast = ["uvloop"]
|
||||
|
||||
[metadata]
|
||||
content-hash = "2a95c67ca1bea20c29bf63542bf5ace592b9ee5dcc70e5bf8ed304bd9b37ded2"
|
||||
content-hash = "20c32d3ce09f448ff7009437f8fe2ca6bed5b7d678a4b5ba7af81d2c6746ceb5"
|
||||
python-versions = "^3.7"
|
||||
|
||||
[metadata.files]
|
||||
|
|
|
|||
|
|
@ -38,6 +38,7 @@ Babel = "^2.7"
|
|||
aiofiles = "^0.4.0"
|
||||
uvloop = {version = "^0.14.0", markers = "sys_platform == 'darwin' or sys_platform == 'linux'", optional = true}
|
||||
async_lru = "^1.0"
|
||||
aiohttp-socks = {version = "^0.3.4", optional = true}
|
||||
|
||||
[tool.poetry.dev-dependencies]
|
||||
uvloop = {version = "^0.14.0", markers = "sys_platform == 'darwin' or sys_platform == 'linux'"}
|
||||
|
|
@ -66,6 +67,7 @@ aiohttp-socks = "^0.3.4"
|
|||
|
||||
[tool.poetry.extras]
|
||||
fast = ["uvloop"]
|
||||
proxy = ["aiohttp-socks"]
|
||||
|
||||
[tool.black]
|
||||
line-length = 99
|
||||
|
|
|
|||
|
|
@ -36,10 +36,22 @@ class TestAiohttpSession:
|
|||
proxy=("socks5://proxy.url/", aiohttp.BasicAuth("login", "password", "encoding"))
|
||||
)
|
||||
|
||||
assert session.cfg.connector_type == aiohttp_socks.ProxyConnector
|
||||
assert session._connector_type == aiohttp_socks.ProxyConnector
|
||||
|
||||
assert isinstance(session.cfg.connector_init, dict)
|
||||
assert session.cfg.connector_init["proxy_type"] is aiohttp_socks.ProxyType.SOCKS5
|
||||
assert isinstance(session._connector_init, dict)
|
||||
assert session._connector_init["proxy_type"] is aiohttp_socks.ProxyType.SOCKS5
|
||||
|
||||
aiohttp_session = await session.create_session()
|
||||
assert isinstance(aiohttp_session.connector, aiohttp_socks.ProxyConnector)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_proxy_session_proxy_url(self):
|
||||
session = AiohttpSession(proxy="socks4://proxy.url/")
|
||||
|
||||
assert isinstance(session.proxy, str)
|
||||
|
||||
assert isinstance(session._connector_init, dict)
|
||||
assert session._connector_init["proxy_type"] is aiohttp_socks.ProxyType.SOCKS4
|
||||
|
||||
aiohttp_session = await session.create_session()
|
||||
assert isinstance(aiohttp_session.connector, aiohttp_socks.ProxyConnector)
|
||||
|
|
|
|||
|
|
@ -45,10 +45,6 @@ class TestBaseSession(DataMixin):
|
|||
session = CustomSession(api=api)
|
||||
assert session.api == api
|
||||
|
||||
def test_init_cfg_namespace(self):
|
||||
session = CustomSession()
|
||||
assert isinstance(session.cfg, types.SimpleNamespace)
|
||||
|
||||
def test_prepare_value(self):
|
||||
session = CustomSession()
|
||||
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue