mirror of
https://github.com/aiogram/aiogram.git
synced 2026-04-08 16:37:47 +00:00
feat(proxy): proxy for aiohttp,base sessions
Add support for proxies in aiohttp session with aiohttp_socks library, edit BaseSession class to support proxies for other sessions in future.
This commit is contained in:
parent
0bd7fc2c7e
commit
aa289cdd93
6 changed files with 99 additions and 10 deletions
|
|
@ -1,6 +1,7 @@
|
|||
from typing import AsyncContextManager, AsyncGenerator
|
||||
|
||||
import aiohttp
|
||||
import aiohttp_socks
|
||||
import pytest
|
||||
from aresponses import ResponsesMockServer
|
||||
|
||||
|
|
@ -29,6 +30,20 @@ class TestAiohttpSession:
|
|||
assert session._session is not None
|
||||
assert isinstance(aiohttp_session, aiohttp.ClientSession)
|
||||
|
||||
@pytest.mark.asyncio
|
||||
async def test_create_proxy_session(self):
|
||||
session = AiohttpSession(
|
||||
proxy=("socks5://proxy.url/", aiohttp.BasicAuth("login", "password", "encoding"))
|
||||
)
|
||||
|
||||
assert session.cfg.connector_type == aiohttp_socks.ProxyConnector
|
||||
|
||||
assert isinstance(session.cfg.connector_init, dict)
|
||||
assert session.cfg.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_close_session(self):
|
||||
session = AiohttpSession()
|
||||
|
|
|
|||
|
|
@ -1,5 +1,6 @@
|
|||
import types
|
||||
import datetime
|
||||
from typing import AsyncContextManager, AsyncGenerator
|
||||
from typing import AsyncContextManager, AsyncGenerator, Any
|
||||
|
||||
import pytest
|
||||
|
||||
|
|
@ -14,7 +15,7 @@ except ImportError:
|
|||
from unittest.mock import AsyncMock as CoroutineMock, patch # type: ignore
|
||||
|
||||
|
||||
class CustomSession(BaseSession):
|
||||
class CustomSession(BaseSession[Any]):
|
||||
async def close(self):
|
||||
pass
|
||||
|
||||
|
|
@ -44,6 +45,10 @@ 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