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__),
|
|
|
|
'../',
|
|
|
|
)),
|
|
|
|
)
|
2020-12-06 05:36:30 +03:00
|
|
|
_SAMPLES_DIR = os.path.abspath(
|
|
|
|
os.path.sep.join((
|
|
|
|
os.path.dirname(__file__),
|
|
|
|
'../samples/',
|
|
|
|
)),
|
|
|
|
)
|
|
|
|
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()
|
|
|
|
self.client = AsyncClient(app=web.app, base_url='http://test')
|
|
|
|
|
|
|
|
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):
|
|
|
|
return 'Foo'
|
2020-12-06 05:36:30 +03:00
|
|
|
|
2020-12-06 05:58:42 +03:00
|
|
|
with web.container.service.override(ServiceMock()):
|
2020-12-06 05:36:30 +03:00
|
|
|
response = self._run(self.client.get('/'))
|
|
|
|
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.json(), {'result': 'Foo'})
|
|
|
|
|
|
|
|
def test_depends_injection(self):
|
|
|
|
response = self._run(self.client.get('/auth', auth=('john_smith', 'secret')))
|
|
|
|
self.assertEqual(response.status_code, 200)
|
|
|
|
self.assertEqual(response.json(), {'username': 'john_smith', 'password': 'secret'})
|