From dd83a337e9d78bc6c71aaf8b769cd37213c2f017 Mon Sep 17 00:00:00 2001 From: Maxime Alves LIRMM Date: Tue, 21 Feb 2023 19:22:57 +0100 Subject: [PATCH] [lib.domain] route_decorator : Adds the "base_url", "cookies" and "url" to the "halfapi" argument of route definitions --- CHANGELOG.md | 4 +++ halfapi/__init__.py | 2 +- halfapi/lib/domain.py | 4 ++- tests/test_lib_domain.py | 73 ++++++++++++++++++++++++++++++++++++++++ 4 files changed, 81 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index a2716b8..b7567a0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/halfapi/__init__.py b/halfapi/__init__.py index f77b486..4327858 100644 --- a/halfapi/__init__.py +++ b/halfapi/__init__.py @@ -1,5 +1,5 @@ #!/usr/bin/env python3 -__version__ = '0.6.25' +__version__ = '0.6.26' def version(): return f'HalfAPI version:{__version__}' diff --git a/halfapi/lib/domain.py b/halfapi/lib/domain.py index 5d55738..6f8a7f5 100644 --- a/halfapi/lib/domain.py +++ b/halfapi/lib/domain.py @@ -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'] diff --git a/tests/test_lib_domain.py b/tests/test_lib_domain.py index 4b5100c..9552edb 100644 --- a/tests/test_lib_domain.py +++ b/tests/test_lib_domain.py @@ -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() == ''