Exemplo n.º 1
0
def test_route_good_matches_with_parameter_patterns(pattern, url,
                                                    expected_values):
    route = Route(pattern, mock_handler)
    match = route.match(url)

    assert match is not None
    assert match.values == expected_values
Exemplo n.º 2
0
def test_route_good_matches(pattern, url, expected_values):
    route = Route(pattern, mock_handler)
    print(route.full_pattern)
    match = route.match(url)

    assert match is not None
    assert match.values == expected_values
Exemplo n.º 3
0
def test_implicit_from_services_only_when_annotation_is_none():
    def handler(dog):
        ...

    binders = get_binders(Route(b"/", handler), {"dog": Dog("Snoopy")})

    assert isinstance(binders[0], ServiceBinder)

    def handler(dog: str):
        ...

    binders = get_binders(Route(b"/", handler), {"dog": Dog("Snoopy")})

    assert isinstance(binders[0], QueryBinder)
Exemplo n.º 4
0
def test_sort_routes_path_pattern(pattern):
    router = Router()
    catch_all_route = Route(pattern, mock_handler)

    router.add_route("GET", catch_all_route)
    router.add_route("GET", Route("/cat/:cat_id", mock_handler))
    router.add_route("GET", Route("/index", mock_handler))
    router.add_route("GET", Route("/about", mock_handler))

    assert router.routes[b"GET"][0] is catch_all_route

    router.sort_routes()

    assert router.routes[b"GET"][-1] is catch_all_route
Exemplo n.º 5
0
    def test_route_handler_can_be_anything(self):
        def request_handler():
            pass

        def auth_handler():
            pass

        handler = MockHandler(request_handler, auth_handler)

        route = Route(b'/', handler)
        match = route.match(b'/')

        assert match is not None
        assert match.handler.request_handler is request_handler
        assert match.handler.auth_handler is auth_handler
Exemplo n.º 6
0
def test_request_binding():
    def handler(request):
        assert request

    binders = get_binders(Route(b'/', handler), {})

    assert isinstance(binders[0], RequestBinder)
Exemplo n.º 7
0
def test_identity_binder_by_param_name_user():
    async def handler(user):
        ...

    binders = get_binders(Route(b'/', handler), {})

    assert isinstance(binders[0], IdentityBinder)
Exemplo n.º 8
0
def test_request_binding():
    def handler(request):
        ...

    binders = get_binders(Route(b"/", handler), Services())

    assert isinstance(binders[0], RequestBinder)
Exemplo n.º 9
0
def serve_files_dynamic(
    router: Router, files_handler: FilesHandler, options: ServeFilesOptions
) -> None:
    """
    Configures a route to serve files dynamically, using the given files handler and
    options.
    """
    options.validate()

    handler = get_files_route_handler(
        files_handler,
        str(options.source_folder),
        options.discovery,
        options.cache_time,
        options.extensions,
        options.root_path,
        options.index_document,
        options.fallback_document,
    )

    if options.allow_anonymous:
        handler = allow_anonymous()(handler)

    route = Route(
        get_static_files_route(options.root_path),
        handler,
    )
    router.add_route("GET", route)
    router.add_route("HEAD", route)
Exemplo n.º 10
0
def test_parameters_get_binders_list_types_default_from_query_required():
    def handler(a: List[str]):
        pass

    binders = get_binders(Route(b'/', handler), {})

    assert all(isinstance(binder, FromQuery) for binder in binders)
    assert all(binder.required for binder in binders)
Exemplo n.º 11
0
def test_parameters_get_binders_list_types_default_from_query_required():
    def handler(a: List[str]):
        ...

    binders = get_binders(Route(b"/", handler), Services())

    assert all(isinstance(binder, QueryBinder) for binder in binders)
    assert all(binder.required for binder in binders)
Exemplo n.º 12
0
def test_identity_binder_by_param_type(annotation_type):
    async def handler(param):
        ...

    handler.__annotations__['param'] = annotation_type

    binders = get_binders(Route(b'/', handler), {})

    assert isinstance(binders[0], IdentityBinder)
Exemplo n.º 13
0
async def test_services_from_normalization():
    app_services = Services()

    def handler(services):
        assert services is app_services
        return None

    method = normalize_handler(Route(b"/", handler), app_services)
    await method(None)
Exemplo n.º 14
0
def test_from_header_specific_name():
    def handler(a: FromHeader(str, 'example')):
        pass

    binders = get_binders(Route(b'/', handler), {})

    assert isinstance(binders[0], FromHeader)
    assert binders[0].expected_type is str
    assert binders[0].name == 'example'
Exemplo n.º 15
0
def test_identity_binder_by_param_type(annotation_type):
    async def handler(param):
        ...

    handler.__annotations__["param"] = annotation_type

    binders = get_binders(Route(b"/", handler), Services())

    assert isinstance(binders[0], IdentityBinder)
Exemplo n.º 16
0
def test_services_binding():
    app_services = {}

    def handler(services):
        assert services is app_services

    binders = get_binders(Route(b'/', handler), app_services)

    assert isinstance(binders[0], ExactBinder)
Exemplo n.º 17
0
def test_from_query_specific_name():
    def handler(a: FromQuery(str, 'example')):
        ...

    binders = get_binders(Route(b'/', handler), {})

    assert isinstance(binders[0], FromQuery)
    assert binders[0].expected_type is str
    assert binders[0].name == 'example'
Exemplo n.º 18
0
def test_parameters_get_binders_list_types_default_from_query_optional():
    def handler(a: Optional[List[str]]):
        ...

    binders = get_binders(Route(b"/", handler), Services())

    assert all(isinstance(binder, QueryBinder) for binder in binders)
    assert all(binder.required is False for binder in binders)
    assert binders[0].parameter_name == "a"
    assert binders[0].expected_type == List[str]
Exemplo n.º 19
0
def test_parameters_get_binders_list_types_default_from_query_optional():
    def handler(a: Optional[List[str]]):
        pass

    binders = get_binders(Route(b'/', handler), {})

    assert all(isinstance(binder, FromQuery) for binder in binders)
    assert all(binder.required is False for binder in binders)
    assert binders[0].name == 'a'
    assert binders[0].expected_type == List[str]
Exemplo n.º 20
0
def test_parameters_get_binders_from_route():
    def handler(a, b, c):
        ...

    binders = get_binders(Route(b"/:a/:b/:c", handler), Services())

    assert all(isinstance(binder, RouteBinder) for binder in binders)
    assert binders[0].parameter_name == "a"
    assert binders[1].parameter_name == "b"
    assert binders[2].parameter_name == "c"
Exemplo n.º 21
0
def test_parameters_get_binders_default_query():
    def handler(a, b, c):
        ...

    binders = get_binders(Route(b"/", handler), Services())

    assert all(isinstance(binder, QueryBinder) for binder in binders)
    assert binders[0].parameter_name == "a"
    assert binders[1].parameter_name == "b"
    assert binders[2].parameter_name == "c"
Exemplo n.º 22
0
def test_parameters_get_binders_default_query():
    def handler(a, b, c):
        pass

    binders = get_binders(Route(b'/', handler), {})

    assert all(isinstance(binder, FromQuery) for binder in binders)
    assert binders[0].name == 'a'
    assert binders[1].name == 'b'
    assert binders[2].name == 'c'
Exemplo n.º 23
0
def test_parameters_get_binders_from_route():
    def handler(a, b, c):
        pass

    binders = get_binders(Route(b'/:a/:b/:c', handler), {})

    assert all(isinstance(binder, FromRoute) for binder in binders)
    assert binders[0].name == 'a'
    assert binders[1].name == 'b'
    assert binders[2].name == 'c'
Exemplo n.º 24
0
def test_does_not_throw_for_forward_ref():
    def handler(a: "Cat"):
        ...

    get_binders(Route(b"/", handler), Services())

    def handler(a: List["str"]):
        ...

    get_binders(Route(b"/", handler), Services())

    def handler(a: Optional[List["Cat"]]):
        ...

    get_binders(Route(b"/", handler), Services())

    def handler(a: FromQuery["str"]):
        ...

    get_binders(Route(b"/", handler), Services())
Exemplo n.º 25
0
def test_parameters_get_binders_from_body_optional():
    def handler(a: Optional[Cat]):
        pass

    binders = get_binders(Route(b'/', handler), {})
    assert len(binders) == 1
    binder = binders[0]

    assert isinstance(binder, FromJson)
    assert binder.expected_type is Cat
    assert binder.required is False
Exemplo n.º 26
0
def test_from_query_unspecified_type():
    def handler(a: FromQuery):
        ...

    binders = get_binders(Route(b"/", handler), Services())
    binder = binders[0]

    assert isinstance(binder, QueryBinder)
    assert binder.expected_type is List[str]
    assert binder.required is True
    assert binder.parameter_name == "a"
Exemplo n.º 27
0
def test_from_query_optional_type_with_union():
    def handler(a: FromQuery[Union[None, str]]):
        ...

    binders = get_binders(Route(b"/", handler), Services())
    binder = binders[0]

    assert isinstance(binder, QueryBinder)
    assert binder.expected_type is str
    assert binder.required is False
    assert binder.parameter_name == "a"
Exemplo n.º 28
0
async def test_services_from_normalization():

    app_services = {}

    def handler(services):
        assert services is app_services
        return services

    method = normalize_handler(Route(b'/', handler), app_services)
    services = await method(None)
    assert services is app_services
Exemplo n.º 29
0
def get_routes_for_static_files(source_folder, extensions, discovery,
                                cache_max_age, frozen):
    for file_paths in get_files_to_serve(source_folder, extensions, discovery):
        full_path = file_paths.get('full_path')

        if file_paths.get('is_dir'):
            handler = get_folder_getter(full_path, cache_max_age)
        else:
            handler = get_frozen_file_getter(full_path, cache_max_age) \
                if frozen else get_file_getter(full_path, cache_max_age)
        yield Route(quote(file_paths.get('rel_path')).encode(), handler)
Exemplo n.º 30
0
def test_from_query_optional_list_type():
    def handler(a: FromQuery[Optional[List[str]]]):
        ...

    binders = get_binders(Route(b"/", handler), Services())
    binder = binders[0]

    assert isinstance(binder, QueryBinder)
    assert binder.expected_type is List[str]
    assert binder.required is False
    assert binder.parameter_name == "a"