2020-12-06 05:36:30 +03:00
|
|
|
from httpx import AsyncClient
|
|
|
|
|
2021-01-11 03:26:15 +03:00
|
|
|
# Runtime import to avoid syntax errors in samples on Python < 3.5 and reach top-dir
|
2020-12-06 05:36:30 +03:00
|
|
|
import os
|
2021-01-11 03:26:15 +03:00
|
|
|
_TOP_DIR = os.path.abspath(
|
|
|
|
os.path.sep.join((
|
|
|
|
os.path.dirname(__file__),
|
2021-10-01 03:09:42 +03:00
|
|
|
"../",
|
2021-01-11 03:26:15 +03:00
|
|
|
)),
|
|
|
|
)
|
2020-12-06 05:36:30 +03:00
|
|
|
_SAMPLES_DIR = os.path.abspath(
|
|
|
|
os.path.sep.join((
|
|
|
|
os.path.dirname(__file__),
|
2021-10-01 03:09:42 +03:00
|
|
|
"../samples/",
|
2020-12-06 05:36:30 +03:00
|
|
|
)),
|
|
|
|
)
|
|
|
|
import sys
|
2021-01-11 03:26:15 +03:00
|
|
|
sys.path.append(_TOP_DIR)
|
2020-12-06 05:36:30 +03:00
|
|
|
sys.path.append(_SAMPLES_DIR)
|
|
|
|
|
2021-01-11 03:26:15 +03:00
|
|
|
from asyncutils import AsyncTestCase
|
2020-12-06 05:36:30 +03:00
|
|
|
|
2021-01-11 03:26:15 +03:00
|
|
|
from wiringfastapi import web
|
2020-12-06 05:36:30 +03:00
|
|
|
|
|
|
|
|
|
|
|
class WiringFastAPITest(AsyncTestCase):
|
|
|
|
|
|
|
|
client: AsyncClient
|
|
|
|
|
|
|
|
def setUp(self) -> None:
|
|
|
|
super().setUp()
|
2021-10-01 03:09:42 +03:00
|
|
|
self.client = AsyncClient(app=web.app, base_url="http://test")
|
2020-12-06 05:36:30 +03:00
|
|
|
|
|
|
|
def tearDown(self) -> None:
|
|
|
|
self._run(self.client.aclose())
|
|
|
|
super().tearDown()
|
|
|
|
|
|
|
|
def test_depends_marker_injection(self):
|
2020-12-06 05:58:42 +03:00
|
|
|
class ServiceMock:
|
|
|
|
async def process(self):
|
2021-10-01 03:09:42 +03:00
|
|
|
return "Foo"
|
2020-12-06 05:36:30 +03:00
|
|
|
|
2020-12-06 05:58:42 +03:00
|
|
|
with web.container.service.override(ServiceMock()):
|
2021-10-01 03:09:42 +03:00
|
|
|
response = self._run(self.client.get("/"))
|
2020-12-06 05:36:30 +03:00
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(response.json(), {"result": "Foo"})
|
2020-12-06 05:36:30 +03:00
|
|
|
|
|
|
|
def test_depends_injection(self):
|
2021-10-01 03:09:42 +03:00
|
|
|
response = self._run(self.client.get("/auth", auth=("john_smith", "secret")))
|
2020-12-06 05:36:30 +03:00
|
|
|
self.assertEqual(response.status_code, 200)
|
2021-10-01 03:09:42 +03:00
|
|
|
self.assertEqual(response.json(), {"username": "john_smith", "password": "secret"})
|