Compare commits

...

2 Commits

6 changed files with 93 additions and 9 deletions

View File

@ -1,5 +1,9 @@
# HalfAPI
## 0.6.26
- Adds the "base_url", "cookies" and "url" to the "halfapi" argument of route definitions
## 0.6.25
- Deletes the "Authorization" cookie on authentication error

View File

@ -1,5 +1,5 @@
#!/usr/bin/env python3
__version__ = '0.6.25'
__version__ = '0.6.26'
def version():
return f'HalfAPI version:{__version__}'

View File

@ -73,9 +73,11 @@ def route_decorator(fct: FunctionType) -> Coroutine:
'user' in request else None,
'config': request.scope.get('config', {}),
'domain': request.scope.get('domain', 'unknown'),
'cookies': request.cookies,
'base_url': request.base_url,
'url': request.url
}
if 'data' in fct_args_spec:
if 'data' in fct_args_defaults_dict:
fct_args['data'] = fct_args_defaults_dict['data']
@ -97,6 +99,7 @@ def route_decorator(fct: FunctionType) -> Coroutine:
logger.debug('Return type {} (defaults: {})'.format(ret_type,
fct_args_defaults_dict))
try:
logger.debug('FCT_ARGS***** %s', fct_args)
if ret_type == 'json':
return ORJSONResponse(fct(**fct_args))

View File

@ -47,7 +47,8 @@ ACLS = {
'foo', 'baz'
},
'optional': {
'truebidoo'
'truebidoo',
'z'
}
}
},

View File

@ -32,7 +32,7 @@ class TestDummyDomain(TestDomain):
assert isinstance(res.content.decode(), str)
assert res.headers['content-type'].split(';')[0] == 'text/html'
res = self.client.request('post', '/ret_type/h24/config', data={
res = self.client.request('post', '/ret_type/h24/config', json={
'trou': 'glet'
})
assert res.status_code == 200
@ -53,24 +53,27 @@ class TestDummyDomain(TestDomain):
def test_arguments_post_routes(self):
arg_dict = {}
res = self.client.request('post', '/arguments', data=arg_dict)
res = self.client.request('post', '/arguments', json=arg_dict)
assert res.status_code == 400
arg_dict = {'foo': '1', 'bar': '3'}
res = self.client.request('post', '/arguments', data=arg_dict)
res = self.client.request('post', '/arguments', json=arg_dict)
assert res.status_code == 400
arg_dict = {'foo': '1', 'baz': '3'}
res = self.client.request('post', '/arguments', data=arg_dict)
res = self.client.request('post', '/arguments', json=arg_dict)
assert res.json() == arg_dict
arg_dict = {'foo': '1', 'baz': '3', 'truebidoo': '4'}
res = self.client.request('post', '/arguments', data=arg_dict)
res = self.client.request('post', '/arguments', json=arg_dict)
assert res.json() == arg_dict
res = self.client.request('post', '/arguments', data={ **arg_dict, 'y': '4'})
res = self.client.request('post', '/arguments', json={ **arg_dict, 'y': '4'})
assert res.json() == arg_dict
res = self.client.request('post', '/arguments', json={ **arg_dict, 'z': True})
assert res.json() == {**arg_dict, 'z': True}

View File

@ -49,3 +49,76 @@
# 
#  assert isinstance(res, list)
#  assert len(res) > 0
from starlette.testclient import TestClient
from starlette.responses import Response
from starlette.routing import Router, Route
from halfapi.lib.domain import route_decorator
from halfapi.lib.user import Nobody
def test_route_decorator():
""" It should decorate an async function that fullfills its arguments
"""
def route(halfapi, data, out, ret_type='txt'):
for key in ['user', 'config', 'domain', 'cookies', 'base_url', 'url']:
assert key in halfapi
assert halfapi['user'] is None
assert isinstance(halfapi['config'], dict)
assert len(halfapi['config']) == 0
assert isinstance(halfapi['domain'], str)
assert halfapi['domain'] == 'unknown'
assert isinstance(halfapi['cookies'], dict)
assert len(halfapi['cookies']) == 0
assert len(str(halfapi['base_url'])) > 0
assert str(halfapi['base_url']) == 'http://testserver/'
assert len(str(halfapi['url'])) > 0
assert str(halfapi['url']) == 'http://testserver/'
assert isinstance(data, dict)
assert len(data) == 0
assert out is None
assert ret_type is 'txt'
return ''
async_route = route_decorator(route)
app = Router([Route('/', endpoint=async_route, methods=['GET'])])
client = TestClient(app)
response = client.get('/')
assert response.is_success
assert response.content.decode() == ''
def route(data, out, ret_type='txt'):
assert isinstance(data, dict)
assert len(data) == 0
assert out is None
assert ret_type is 'txt'
return ''
async_route = route_decorator(route)
app = Router([Route('/', endpoint=async_route, methods=['GET'])])
client = TestClient(app)
response = client.get('/')
assert response.is_success
assert response.content.decode() == ''
def route(data):
assert isinstance(data, dict)
assert len(data) == 2
assert data['toto'] == 'tata'
assert data['bouboul'] == True
return ''
async_route = route_decorator(route)
app = Router([Route('/', endpoint=async_route, methods=['POST'])])
client = TestClient(app)
response = client.post('/', json={'toto': 'tata', 'bouboul': True})
assert response.is_success
assert response.json() == ''