Beispiel #1
0
def test_reverse_matching():
    from lona.routing import Router, Route

    router = Router()

    routes = [
        Route('/foo/<arg>/', None, name='foo'),
        Route('/bar/', None, name='bar'),
    ]

    router.add_routes(*routes)

    # /foo/bar/
    url = router.reverse('foo', arg='bar')

    assert url == '/foo/bar/'

    # /bar/
    url = router.reverse('bar')

    assert url == '/bar/'

    # /foo/bar/ but wrongly named arg
    with pytest.raises(ValueError):
        url = router.reverse('foo', foo='bar')

    # unknown name
    with pytest.raises(ValueError):
        url = router.reverse('baz')
Beispiel #2
0
def test_basic_routing():
    from lona.routing import Router, Route

    router = Router()

    routes = [
        Route('/foo/<arg>/', None),
        Route('/foo/', None),
        Route('/bar/<arg>/', None),
        Route('/', None),
    ]

    router.add_routes(*routes)

    # /foo/
    match, route, match_info = router.resolve('/foo/')

    assert match
    assert route == routes[1]
    assert match_info == {}

    # /foo/bar/
    match, route, match_info = router.resolve('/foo/bar/')

    assert match
    assert route == routes[0]
    assert match_info == {'arg': 'bar'}

    # /bar/foo/
    match, route, match_info = router.resolve('/bar/foo/')

    assert match
    assert route == routes[2]
    assert match_info == {'arg': 'foo'}

    # /
    match, route, match_info = router.resolve('/')

    assert match
    assert route == routes[3]
    assert match_info == {}

    # /foobar/
    match, route, match_info = router.resolve('/foobar/')

    assert not match
Beispiel #3
0
def test_optional_trailing_slash():
    from lona.routing import Router, Route

    router = Router()

    routes = [
        Route('/foo(/)', None),
        Route('/bar/', None),
    ]

    router.add_routes(*routes)

    # /foo
    match, route, match_info = router.resolve('/foo')

    assert match
    assert route == routes[0]
    assert match_info == {}

    # /foo/
    match, route, match_info = router.resolve('/foo/')

    assert match
    assert route == routes[0]
    assert match_info == {}

    # /bar
    match, route, match_info = router.resolve('/bar')

    assert not match

    # /bar/
    match, route, match_info = router.resolve('/bar/')

    assert match
    assert route == routes[1]
    assert match_info == {}
Beispiel #4
0
def test_routes_with_regexes():
    from lona.routing import Router, Route

    router = Router()

    routes = [
        Route('/number/<number:[0-9]+>/', None),
    ]

    router.add_routes(*routes)

    # /number/14/
    match, route, match_info = router.resolve('/number/14/')

    assert match
    assert route == routes[0]
    assert match_info == {'number': '14'}

    # /number/14a/
    match, route, match_info = router.resolve('/number/14a/')

    assert not match
Beispiel #5
0
from lona.routing import Route

routes = [
    Route('/', 'views/home.py::HomeView'),
]
Beispiel #6
0
 def decorator(view_class):
     self.routes.append(Route(
         *args,
         view=view_class,
         **kwargs,
     ))
Beispiel #7
0
from lona.routing import Route

routes = [
    # view types
    Route('/view-types/interactive-view/',
          'views/view_types/interactive_view.py::InteractiveView'),
    Route('/view-types/non-interactive-view/',
          'views/view_types/non_interactive_view.py::NonInteractiveView',
          interactive=False),
    Route('/view-types/http-pass-through/',
          'views/view_types/http_pass_through_view.py::HTTPPassThroughView',
          http_pass_through=True),
    Route('/view-types/daemonized-view/<name>/',
          'views/view_types/daemonized_view.py::DaemonizedView'),
    Route('/view-types/daemonized-view/',
          'views/view_types/daemonized_view.py::DaemonizedView'),
    Route('/view-types/form-view/', 'views/view_types/form_view.py::FormView'),
    Route('/view-types/async-view/',
          'views/view_types/async_view.py::AsyncView'),

    # response types
    Route('/response-types/template-response/',
          'views/response_types/template_response.py::TemplateResponseView'),
    Route('/response-types/file-response/',
          'views/response_types/file_response.py::FileResponseView',
          interactive=False),
    Route('/response-types/json-response/',
          'views/response_types/json_response.py::JSONResponseView',
          interactive=False),
    Route('/response-types/redirect/',
          'views/response_types/redirect.py::RedirectView'),