parse path and header
This commit is contained in:
49
tests/test_parse_func_signature.py
Normal file
49
tests/test_parse_func_signature.py
Normal file
@@ -0,0 +1,49 @@
|
||||
from aiohttp_pydantic.injectors import _parse_func_signature
|
||||
from pydantic import BaseModel
|
||||
from uuid import UUID
|
||||
|
||||
|
||||
class User(BaseModel):
|
||||
firstname: str
|
||||
lastname: str
|
||||
|
||||
|
||||
def test_parse_func_signature():
|
||||
|
||||
def body_only(self, user: User):
|
||||
pass
|
||||
|
||||
def path_only(self, id: str, /):
|
||||
pass
|
||||
|
||||
def qs_only(self, page: int):
|
||||
pass
|
||||
|
||||
def header_only(self, *, auth: UUID):
|
||||
pass
|
||||
|
||||
def path_and_qs(self, id: str, /, page: int):
|
||||
pass
|
||||
|
||||
def path_and_header(self, id: str, /, *, auth: UUID):
|
||||
pass
|
||||
|
||||
def qs_and_header(self, page: int, *, auth: UUID):
|
||||
pass
|
||||
|
||||
def path_qs_and_header(self, id: str, /, page: int, *, auth: UUID):
|
||||
pass
|
||||
|
||||
def path_body_qs_and_header(self, id: str, /, user: User, page: int, *, auth: UUID):
|
||||
pass
|
||||
|
||||
assert _parse_func_signature(body_only) == ({}, {'user': User}, {}, {})
|
||||
assert _parse_func_signature(path_only) == ({'id': str}, {}, {}, {})
|
||||
assert _parse_func_signature(qs_only) == ({}, {}, {'page': int}, {})
|
||||
assert _parse_func_signature(header_only) == ({}, {}, {}, {'auth': UUID})
|
||||
assert _parse_func_signature(path_and_qs) == ({'id': str}, {}, {'page': int}, {})
|
||||
assert _parse_func_signature(path_and_header) == ({'id': str}, {}, {}, {'auth': UUID})
|
||||
assert _parse_func_signature(qs_and_header) == ({}, {}, {'page': int}, {'auth': UUID})
|
||||
assert _parse_func_signature(path_qs_and_header) == ({'id': str}, {}, {'page': int}, {'auth': UUID})
|
||||
assert _parse_func_signature(path_body_qs_and_header) == ({'id': str}, {'user': User}, {'page': int}, {'auth': UUID})
|
||||
|
||||
@@ -54,3 +54,14 @@ async def test_get_article_with_valid_header_should_return_the_parsed_type(aioht
|
||||
assert resp.status == 200
|
||||
assert resp.content_type == 'application/json'
|
||||
assert await resp.json() == {'signature': '2020-10-04T18:01:00'}
|
||||
|
||||
|
||||
async def test_get_article_with_valid_header_containing_hyphen_should_be_returned(aiohttp_client, loop):
|
||||
app = web.Application()
|
||||
app.router.add_view('/article', ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
resp = await client.get('/article', headers={'Signature-Expired': '2020-10-04T18:01:00'})
|
||||
assert resp.status == 200
|
||||
assert resp.content_type == 'application/json'
|
||||
assert await resp.json() == {'signature': '2020-10-04T18:01:00'}
|
||||
|
||||
20
tests/test_validation_path.py
Normal file
20
tests/test_validation_path.py
Normal file
@@ -0,0 +1,20 @@
|
||||
from aiohttp import web
|
||||
from aiohttp_pydantic import PydanticView
|
||||
|
||||
|
||||
class ArticleView(PydanticView):
|
||||
|
||||
async def get(self, author_id: str, tag: str, date: int, /):
|
||||
return web.json_response({'path': [author_id, tag, date]})
|
||||
|
||||
|
||||
async def test_get_article_without_required_qs_should_return_an_error_message(aiohttp_client, loop):
|
||||
app = web.Application()
|
||||
app.router.add_view('/article/{author_id}/tag/{tag}/before/{date}', ArticleView)
|
||||
|
||||
client = await aiohttp_client(app)
|
||||
resp = await client.get('/article/1234/tag/music/before/1980')
|
||||
assert resp.status == 200
|
||||
assert resp.content_type == 'application/json'
|
||||
assert await resp.json() == {'path': ['1234', 'music', 1980]}
|
||||
|
||||
Reference in New Issue
Block a user