2021-10-18 23:19:03 +03:00
|
|
|
"""Aiohttp extension tests."""
|
2020-07-29 02:19:05 +03:00
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
from aiohttp import web, test_utils
|
2020-07-29 02:19:05 +03:00
|
|
|
from dependency_injector import containers, providers
|
|
|
|
from dependency_injector.ext import aiohttp
|
2021-10-18 23:19:03 +03:00
|
|
|
from pytest import fixture, mark
|
2020-07-29 02:19:05 +03:00
|
|
|
|
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
async def index_view(_):
|
2021-10-01 03:09:42 +03:00
|
|
|
return web.Response(text="Hello World!")
|
2020-07-29 02:19:05 +03:00
|
|
|
|
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
async def second_view(_):
|
2021-10-01 03:09:42 +03:00
|
|
|
return web.Response(text="Test!")
|
2020-07-29 02:19:05 +03:00
|
|
|
|
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
class OtherClassBasedView(web.View):
|
2020-07-29 02:19:05 +03:00
|
|
|
async def get(self):
|
2021-10-01 03:09:42 +03:00
|
|
|
return web.Response(text="Test class-based!")
|
2020-07-29 02:19:05 +03:00
|
|
|
|
|
|
|
|
|
|
|
@web.middleware
|
|
|
|
async def middleware(request, handler):
|
|
|
|
resp = await handler(request)
|
2021-10-01 03:09:42 +03:00
|
|
|
resp.text = resp.text + " wink1"
|
2020-07-29 02:19:05 +03:00
|
|
|
return resp
|
|
|
|
|
|
|
|
|
|
|
|
def middleware_factory(text):
|
|
|
|
@web.middleware
|
|
|
|
async def sample_middleware(request, handler):
|
|
|
|
resp = await handler(request)
|
|
|
|
resp.text = resp.text + text
|
|
|
|
return resp
|
|
|
|
return sample_middleware
|
|
|
|
|
|
|
|
|
|
|
|
class ApplicationContainer(containers.DeclarativeContainer):
|
|
|
|
|
|
|
|
app = aiohttp.Application(
|
|
|
|
web.Application,
|
|
|
|
middlewares=providers.List(
|
|
|
|
aiohttp.Middleware(middleware),
|
2021-10-01 03:09:42 +03:00
|
|
|
aiohttp.MiddlewareFactory(middleware_factory, text=" wink2"),
|
2020-07-29 02:19:05 +03:00
|
|
|
),
|
|
|
|
)
|
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
index_view = aiohttp.View(index_view)
|
|
|
|
second_view = aiohttp.View(second_view)
|
|
|
|
other_class_based_view = aiohttp.ClassBasedView(OtherClassBasedView)
|
|
|
|
|
|
|
|
|
|
|
|
@fixture
|
|
|
|
def app():
|
|
|
|
container = ApplicationContainer()
|
|
|
|
app = container.app()
|
|
|
|
app.container = container
|
|
|
|
app.add_routes([
|
|
|
|
web.get("/", container.index_view.as_view()),
|
|
|
|
web.get("/second", container.second_view.as_view(), name="second"),
|
|
|
|
web.get("/class-based", container.other_class_based_view.as_view()),
|
|
|
|
])
|
|
|
|
return app
|
|
|
|
|
|
|
|
|
|
|
|
@fixture
|
|
|
|
async def client(app):
|
|
|
|
async with test_utils.TestClient(test_utils.TestServer(app)) as client:
|
|
|
|
yield client
|
|
|
|
|
2020-07-29 02:19:05 +03:00
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
@mark.asyncio
|
|
|
|
@mark.filterwarnings("ignore:The loop argument is deprecated:DeprecationWarning")
|
|
|
|
async def test_index(client):
|
|
|
|
response = await client.get("/")
|
2020-07-29 02:19:05 +03:00
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
assert response.status == 200
|
|
|
|
assert await response.text() == "Hello World! wink2 wink1"
|
2020-07-29 02:19:05 +03:00
|
|
|
|
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
@mark.asyncio
|
|
|
|
@mark.filterwarnings("ignore:The loop argument is deprecated:DeprecationWarning")
|
|
|
|
async def test_second(client):
|
|
|
|
response = await client.get("/second")
|
2020-07-29 02:19:05 +03:00
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
assert response.status == 200
|
|
|
|
assert await response.text() == "Test! wink2 wink1"
|
2020-07-29 02:19:05 +03:00
|
|
|
|
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
@mark.asyncio
|
|
|
|
@mark.filterwarnings("ignore:The loop argument is deprecated:DeprecationWarning")
|
|
|
|
async def test_class_based(client):
|
|
|
|
response = await client.get("/class-based")
|
2020-07-29 02:19:05 +03:00
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
assert response.status == 200
|
|
|
|
assert await response.text() == "Test class-based! wink2 wink1"
|
2020-07-29 02:19:05 +03:00
|
|
|
|
|
|
|
|
2021-10-18 23:19:03 +03:00
|
|
|
def test_endpoints(app):
|
|
|
|
assert str(app.router["second"].url_for()) == "/second"
|