示例#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)])