예제 #1
0
    def test_should_route_three_paths_with_common_parts(
            self, method, controller, controller2, controller3):
        path1_pattern = r'/apis/dataclasses/{id:\d{3}}'
        path2_pattern = r'/apis/dataclasses/{id:\d{3}}/{type}'
        # path3_pattern = r'/apis/dataclasses/{id:\d{3}}/{type}/{value:.+}'

        router = http_router([
            Route(path1_pattern, method, controller),
            Route(path2_pattern, method, controller2),
            # Route(path3_pattern, method, controller3),
        ])

        path1 = '/apis/dataclasses/012'
        path2 = '/apis/dataclasses/013/apis'
        # path3 = '/apis/dataclasses/014/api/dataclasses'

        resolved1 = route(router, path1, method.value)
        resolved2 = route(router, path2, method.value)
        # resolved3 = route(router, path3, method.value)

        assert resolved1.route.caller is controller
        assert resolved2.route.caller is controller2
        # assert resolved3.route.caller is controller3

        assert resolved1.path_args == {'id': '012'}
        assert resolved2.path_args == {'id': '013', 'type': 'apis'}
예제 #2
0
    def test_should_route_path_with_slash(self, method, controller):
        path = '/dataclasses/api'
        router = http_router([Route(path, method, controller)])

        resolved = route(router, path, method.value)

        assert resolved.route.caller is controller
예제 #3
0
    def test_should_route_path_with_regex_and_slash(self, method, controller):
        path_pattern = r'/{id:\d{3}}/apidaora'
        router = http_router([Route(path_pattern, method, controller)])
        path = '/012/apidaora'

        resolved = route(router, path, method.value)

        assert resolved.route.caller is controller
예제 #4
0
    def test_should_not_route_method(self, method, controller):
        path = '/api'
        router = http_router([Route(path, method, controller)])

        with pytest.raises(MethodNotFoundError) as exc_info:
            route(router, path, MethodType.POST.value)

        assert exc_info.value.args == (MethodType.POST.value, path)
예제 #5
0
    def test_should_not_route_path_with_slash(self, method, controller):
        path_pattern = '/api/apidaora'
        router = http_router([Route(path_pattern, method, controller)])
        path = '/api/invalid'

        with pytest.raises(PathNotFoundError) as exc_info:
            route(router, path, method.value)

        assert exc_info.value.args == (path, )
예제 #6
0
    def test_should_route_path_with_slash_and_regex(self, method, controller):
        path_pattern = r'/api/{id:\d{3}}'
        router = http_router([Route(path_pattern, method, controller)])
        path = '/api/012'

        resolved = route(router, path, method.value)

        assert resolved.route.caller is controller
        assert resolved.path_args == {'id': '012'}
예제 #7
0
    def test_should_route_path_with_path_arg_name(self, method, controller):
        path_pattern = r'/{id}'
        router = http_router([Route(path_pattern, method, controller)])
        path = '/012'

        resolved = route(router, path, method.value)

        assert resolved.route.caller is controller
        assert resolved.path_args == {'id': '012'}
예제 #8
0
    def test_should_not_route_path_with_regex(self, method, controller):
        path_pattern = r'/{id:\d{3}}'
        router = http_router([Route(path_pattern, method, controller)])
        path = '/0'

        with pytest.raises(PathNotFoundError) as exc_info:
            route(router, path, method.value)

        assert exc_info.value.args == (path, )
예제 #9
0
    class MyResponseBody(TypedDict):
        hello_message: str
        about_you: 'MyResponse.You'

    body: MyResponseBody
    headers: MyHeaders


def hello_controller(req: MyRequest) -> MyResponse:
    body = MyResponse.MyResponseBody(
        hello_message=hello_message(
            req.path_args['name'], req.query['location']
        ),
        about_you=MyResponse.You(
            name=req.path_args['name'],
            last_name=req.body['last_name'],
            location=req.query['location'],
            age=req.body['age'],
        ),
    )
    headers = MyHeaders(x_req_id=req.headers['x_req_id'])
    return MyResponse(body=body, headers=headers)


def hello_message(name: str, location: str) -> str:
    return f'Hello {name}! Welcome to {location}!'


app = asgi_app([Route('/hello/{name}', MethodType.PUT, hello_controller)])
예제 #10
0
from apidaora import JSONResponse, MethodType
from apidaora.core.app import asgi_app
from apidaora.core.request import Request
from apidaora.core.router import Route


@jsondaora
class MyRequest(Request):
    class MyQuery(TypedDict):
        name: str

    query: MyQuery


@jsondaora
class MyResponse(JSONResponse):
    class MyResponseBody(TypedDict):
        message: str

    body: MyResponseBody


def hello_controller(req: MyRequest) -> MyResponse:
    name = req.query['name']
    body = MyResponse.MyResponseBody(message=f'Hello {name}!')
    return MyResponse(body=body)


app = asgi_app([Route('/hello', MethodType.GET, hello_controller)])
예제 #11
0
def fake_app():
    return asgi_app([Route('/api/{id}', MethodType.GET, fake_controller)])