How to use the blacksheep.server.routing.Router function in blacksheep

To help you get started, we’ve selected a few blacksheep examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github RobertoPrevato / BlackSheep / tests / test_router.py View on Github external
def test_router_match_any_by_extension():
    router = Router()

    def a(): ...

    def b(): ...

    router.add_get(b'/a/*.js', a)
    router.add_get(b'/b/*.css', b)

    m = router.get_match(HttpMethod.GET, b'/a/anything/really')
    assert m is None

    m = router.get_match(HttpMethod.GET, b'/a/anything/really.js')
    assert m is not None
    assert m.handler is a
    assert m.values.get('tail') == 'anything/really'
github RobertoPrevato / BlackSheep / tests / test_router.py View on Github external
def test_router_add_method(method, pattern, url):
    router = Router()
    router.add(method, pattern, mock_handler)

    route = router.get_match(method, url)

    assert route is not None
    assert route.handler is mock_handler

    route = router.get_match(FAKE, url)
    assert route is None
github RobertoPrevato / BlackSheep / tests / test_router.py View on Github external
def test_automatic_pattern_with_ellipsis():
    router = Router()

    @router.get(...)
    def home(): ...

    @router.get(...)
    def another(): ...

    match = router.get_match('GET', '/')

    assert match is None

    match = router.get_match('GET', '/home')

    assert match is not None
    assert match.handler is home
github RobertoPrevato / BlackSheep / tests / test_router.py View on Github external
def test_automatic_pattern_with_ellipsis_name_normalization():
    router = Router()

    @router.get(...)
    def hello_world(): ...

    match = router.get_match('GET', '/hello_world')

    assert match is None

    match = router.get_match('GET', '/hello-world')

    assert match is not None
    assert match.handler is hello_world
github RobertoPrevato / BlackSheep / tests / test_router.py View on Github external
def test_router_match_any_below():
    router = Router()

    def a(): ...

    def b(): ...

    def c(): ...

    def d(): ...

    router.add_get(b'/a/*', a)
    router.add_get(b'/b/*', b)
    router.add_get(b'/c/*', c)
    router.add_get(b'/d/*', d)

    m = router.get_match(HttpMethod.GET, b'/a')
    assert m is None
github RobertoPrevato / BlackSheep / tests / test_router.py View on Github external
def test_fallback_route():
    router = Router()

    def not_found_handler():
        pass

    router.fallback = not_found_handler

    m = router.get_match(HttpMethod.POST, b'/')

    assert m is not None
    assert m.handler is not_found_handler
github RobertoPrevato / BlackSheep / tests / test_router.py View on Github external
def test_router_match_among_many():
    router = Router()

    def home(): ...

    def get_foo(): ...

    def create_foo(): ...

    def delete_foo(): ...

    router.add_get(b'/', home)
    router.add_get(b'/foo', get_foo)
    router.add_post(b'/foo', create_foo)
    router.add_delete(b'/foo', delete_foo)

    m = router.get_match(HttpMethod.GET, b'/')
github RobertoPrevato / BlackSheep / tests / test_router.py View on Github external
def test_router_decorator(decorator, pattern, url):
    router = Router()

    method = getattr(router, decorator)

    @method(pattern)
    def home():
        return 'Hello, World'

    route = router.get_match(decorator.upper(), url)

    assert route is not None
    assert route.handler is home

    value = route.handler()
    assert value == 'Hello, World'

    route = router.get_match(FAKE, url)
github RobertoPrevato / BlackSheep / tests / test_router.py View on Github external
def test_automatic_pattern_with_ellipsis_index_name():
    router = Router()

    @router.get(...)
    def index(): ...

    match = router.get_match('GET', '/')

    assert match is not None
    assert match.handler is index
github RobertoPrevato / BlackSheep / blacksheep / server / application.py View on Github external
def __init__(self,
                 router: Optional[Router] = None,
                 middlewares: Optional[List[Callable]] = None,
                 resources: Optional[Resources] = None,
                 services: Optional[ServicesType] = None,
                 debug: bool = False,
                 show_error_details: bool = False):
        if router is None:
            router = Router()
        if services is None:
            services = Container()
        super().__init__(get_show_error_details(debug or show_error_details), router)

        if middlewares is None:
            middlewares = []
        if resources is None:
            resources = Resources(get_resource_file_content('error.html'))
        self.services = services  # type: ServicesType
        self.debug = debug
        self.middlewares = middlewares
        self.access_logger = None
        self.logger = None
        self._default_headers = None
        self._use_sync_logging = False
        self._middlewares_configured = False