Exemplo n.º 1
0
def test_validator():
    from http_router import Router

    # The router only accepts async functions
    router = Router(validator=inspect.iscoroutinefunction)

    with pytest.raises(router.RouterError):
        router.route('/', '/simple')(lambda: 'simple')
Exemplo n.º 2
0
def test_converter():
    from http_router import Router

    # The router only accepts async functions
    router = Router(converter=lambda v: lambda r: (r, v))

    router.route('/')('simple')
    match = router('/')
    assert match.target('test') == ('test', 'simple')
Exemplo n.º 3
0
    def __route__(cls,
                  router: Router,
                  *paths: str,
                  methods: TYPE_METHODS = None,
                  **params):
        """Check for registered methods."""
        router.bind(cls, *paths, methods=methods or cls.methods, **params)
        for _, method in inspect.getmembers(cls,
                                            lambda m: hasattr(m, '__route__')):
            cpaths, cparams = method.__route__
            router.bind(cls, *cpaths, __meth__=method.__name__, **cparams)

        return cls
Exemplo n.º 4
0
def test_readme():
    from http_router import Router

    router = Router(trim_last_slash=True)

    @router.route('/simple')
    def simple():
        return 'simple'

    match = router('/simple')
    assert match.target() == 'simple'
    assert match.params is None
Exemplo n.º 5
0
def test_custom_route():
    from http_router import Router

    class View:

        methods = 'get', 'post'

        def __new__(cls, *args, **kwargs):
            """Init the class and call it."""
            self = super().__new__(cls)
            return self(*args, **kwargs)

        @classmethod
        def __route__(cls, router, *paths, **params):
            return router.bind(cls, *paths, methods=cls.methods)

    # The router only accepts async functions
    router = Router()
    router.route('/')(View)
    assert router.plain['/'][0].methods == {'GET', 'POST'}
    match = router('/')
    assert match.target is View
Exemplo n.º 6
0
def test_nested_routers():
    from http_router import Router

    child = Router()
    child.route('/url', methods='PATCH')('child_url')
    match = child('/url', 'PATCH')
    assert match.target == 'child_url'

    root = Router()
    root.route('/child')(child)

    with pytest.raises(root.NotFound):
        root('/child')

    with pytest.raises(root.NotFound):
        root('/child/unknown')

    with pytest.raises(root.MethodNotAllowed):
        root('/child/url')

    match = root('/child/url', 'PATCH')
    assert match.target == 'child_url'
Exemplo n.º 7
0
def test_trim_last_slash():
    from http_router import Router

    router = Router()

    router.route('/route1')('route1')
    router.route('/route2/')('route2')

    assert router('/route1').target == 'route1'
    assert router('/route2/').target == 'route2'

    with pytest.raises(router.NotFound):
        assert not router('/route1/')

    with pytest.raises(router.NotFound):
        assert not router('/route2')

    router = Router(trim_last_slash=True)

    router.route('/route1')('route1')
    router.route('/route2/')('route2')

    assert router('/route1').target == 'route1'
    assert router('/route2/').target == 'route2'
    assert router('/route1/').target == 'route1'
    assert router('/route2').target == 'route2'
Exemplo n.º 8
0
def test_mounts():
    from http_router import Router
    from http_router.routes import Mount

    router = Router()
    route = Mount('/api/', set(), router)
    assert route.path == '/api'
    match = route.match('/api/e1', '')
    assert not match

    router.route('/e1')('e1')
    match = route.match('/api/e1', 'UNKNOWN')
    assert match
    assert match.target == 'e1'

    root = Router()
    subrouter = Router()

    root.route('/api')(1)
    root.route(re('/api/test'))(2)
    root.route('/api')(subrouter)
    subrouter.route('/test')(3)

    assert root('/api').target == 1
    assert root('/api/test').target == 3
Exemplo n.º 9
0
def test_router():
    """Base tests."""
    from http_router import Router

    router = Router(trim_last_slash=True)

    with pytest.raises(router.RouterError):
        router.route(lambda: 12)

    with pytest.raises(router.NotFound):
        assert router('/unknown')

    router.route('/', '/simple')('simple')

    match = router('/', 'POST')
    assert match.target == 'simple'
    assert not match.params

    match = router('/simple', 'DELETE')
    assert match.target == 'simple'
    assert not match.params

    router.route('/only-post', methods='post')('only-post')
    assert router.plain['/only-post'][0].methods == {'POST'}

    with pytest.raises(router.MethodNotAllowed):
        assert router('/only-post')

    match = router('/only-post', 'POST')
    assert match.target == 'only-post'
    assert not match.params

    router.route('/dynamic1/{id}')('dyn1')
    router.route('/dynamic2/{ id }')('dyn2')

    match = router('/dynamic1/11/')
    assert match.target == 'dyn1'
    assert match.params == {'id': '11'}

    match = router('/dynamic2/22/')
    assert match.target == 'dyn2'
    assert match.params == {'id': '22'}

    @router.route(r'/hello/{name:str}', methods='post')
    def hello():
        return 'hello'

    match = router('/hello/john/', 'POST')
    assert match.target() == 'hello'
    assert match.params == {'name': 'john'}

    @router.route('/params', var='value')
    def params(**opts):
        return opts

    match = router('/params', 'POST')
    assert match.target() == {'var': 'value'}

    assert router.routes()
    assert router.routes()[0].path == ''
Exemplo n.º 10
0
def router():
    from http_router import Router, NotFound, MethodNotAllowed, RouterError  # noqa

    return Router()