- #32 missing tests for multipart/form-data, x-www-form-urlencoded and others (typically text/plain

This commit is contained in:
Maxime Alves LIRMM 2022-07-06 08:01:28 +02:00
parent 3ec6d7514e
commit 998350cd89
1 changed files with 20 additions and 7 deletions

View File

@ -56,11 +56,24 @@ def args_check(fct):
data_ = dict(req.query_params) data_ = dict(req.query_params)
elif req.method in ['POST', 'PATCH', 'PUT', 'DELETE']: elif req.method in ['POST', 'PATCH', 'PUT', 'DELETE']:
if req.scope.get('headers'):
if b'content-type' not in dict(req.scope.get('headers')):
data_ = {}
else:
content_type = dict(req.scope.get('headers')).get(b'content-type').decode().split(';')[0]
if content_type == 'application/json':
try: try:
data_ = await req.json() data_ = await req.json()
except JSONDecodeError as exc: except JSONDecodeError as exc:
logger.debug('Posted data was not JSON') logger.debug('Posted data was not JSON')
pass pass
elif content_type in [
'multipart/form-data', 'application/x-www-form-urlencoded']:
data_ = await req.form()
else:
data_ = await req.body()
def plural(array: list) -> str: def plural(array: list) -> str:
return 's' if len(array) > 1 else '' return 's' if len(array) > 1 else ''
@ -69,7 +82,7 @@ def args_check(fct):
args_d = req.scope.get('args') args_d = req.scope.get('args')
if args_d is not None: if args_d is not None and isinstance(data_, dict):
required = args_d.get('required', set()) required = args_d.get('required', set())
missing = [] missing = []
@ -90,7 +103,7 @@ def args_check(fct):
if key in data_: if key in data_:
data[key] = data_[key] data[key] = data_[key]
else: else:
""" Unsafe mode, without specified arguments """ Unsafe mode, without specified arguments, or plain text mode
""" """
data = data_ data = data_