示例#1
0
 def test_route_list(self):
     r = route("api",
         route("news", "myapp.api.news"),
         route("comments", "myapp.api.comments"))
     self.assertEqual(r.guards, [])
     self.assertNotEqual(r.pattern, None)
     self.assertIsInstance(r, RouteGroup)
示例#2
0
    def test_method_inner(self):
        r = route(
            route("news", "news"),
            route(GET, "comments", "comments_get"),
            route(POST, "comments", "comments_post"))

        req = Request.blank("/news", {"REQUEST_METHOD": "GET"})
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((), {}, "news"))

        req = Request.blank("/news", {"REQUEST_METHOD": "POST"})
        self.assertRaises(
            RouteGuarded,
            r, req)

        req = Request.blank("/comments", {"REQUEST_METHOD": "POST"})
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((), {}, "comments_post"))

        req = Request.blank("/comments", {"REQUEST_METHOD": "DELETE"})
        self.assertRaises(
            RouteGuarded,
            r, req)

        self.assertNoMatch(r, "/newsweeek")
        self.assertNoMatch(r, "/ne")
示例#3
0
文件: tests.py 项目: andreypopp/routr
    def test_method_inner(self):
        r = route(
            route('news', 'news'),
            route(GET, 'comments', 'comments_get'),
            route(POST, 'comments', 'comments_post'))

        req = Request.blank('/news', {'REQUEST_METHOD': 'GET'})
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((), {}, 'news'))

        req = Request.blank('/news', {'REQUEST_METHOD': 'POST'})
        self.assertRaises(
            RouteGuarded,
            r, req)

        req = Request.blank('/comments', {'REQUEST_METHOD': 'POST'})
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((), {}, 'comments_post'))

        req = Request.blank('/comments', {'REQUEST_METHOD': 'DELETE'})
        self.assertRaises(
            RouteGuarded,
            r, req)

        self.assertNoMatch(r, '/newsweeek')
        self.assertNoMatch(r, '/ne')
示例#4
0
文件: tests.py 项目: andreypopp/routr
 def test_route_list_no_pattern(self):
     r = route(
         route('news', 'myapp.api.news'),
         route('comments', 'myapp.api.comments'))
     self.assertEqual(r.guards, [])
     self.assertEqual(r.pattern, None)
     self.assertIsInstance(r, RouteGroup)
示例#5
0
 def test_no_match(self):
     self.assertNoMatch(
         route("news", "target"),
         "/new")
     self.assertNoMatch(
         route("news", "target"),
         "/newsweek")
示例#6
0
文件: tests.py 项目: andreypopp/routr
 def test_no_match(self):
     self.assertNoMatch(
         route('news', 'target'),
         '/new')
     self.assertNoMatch(
         route('news', 'target'),
         '/newsweek')
示例#7
0
    def test_reverse(self):
        r = route("news", "target", name="news")
        self.assertEqual(r.reverse("news"), "/news")
        self.assertRaises(RouteReversalError, r.reverse, "news2")

        r = route("news/{id:int}/", "target", name="news")
        self.assertEqual(r.reverse("news", 42), "/news/42/")
        self.assertRaises(RouteReversalError, r.reverse, "news2")
示例#8
0
 def test_route_list_no_pattern_guards(self):
     r = route(
         route("news", "myapp.api.news"),
         route("comments", "myapp.api.comments"),
         guards=["guard"])
     self.assertEqual(r.guards, ["guard"])
     self.assertEqual(r.pattern, None)
     self.assertIsInstance(r, RouteGroup)
示例#9
0
    def test_by_method(self):
        r = route('api', route(GET, 'news_get'), route(POST, 'news_post'))

        req = Request.blank('/api', {'REQUEST_METHOD': 'POST'})
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target),
                         ((), {}, 'news_post'))

        req = Request.blank('/api')
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target), ((), {}, 'news_get'))
示例#10
0
文件: resource.py 项目: riffm/routr
def resource(res, name=None):
    name = name or (res.__name__ if hasattr(res, "__name__") else str(res))
    collection_methods = [
        route(m, getattr(res, mn), name="%s-%s" % (mn, name))
        for mn, m in _collection_methods.items()
        if hasattr(res, mn)
    ]
    resource_methods = [
        route(m, getattr(res, mn), name="%s-%s" % (mn, name)) for mn, m in _resource_methods.items() if hasattr(res, mn)
    ]
    return route(*collection_methods, route("{id}", *resource_methods))
示例#11
0
文件: tests.py 项目: riffm/routr
    def test_reverse(self):
        r = route("news", "target", name="news")
        self.assertEqual(r.reverse("news"), "/news")
        self.assertRaises(RouteReversalError, r.reverse, "news2")

        r = route("news/{id:int}/", "target", name="news")
        self.assertEqual(r.reverse("news", 42), "/news/42/")
        self.assertRaises(RouteReversalError, r.reverse, "news2")

        r = route("news/{any(login,attach,app_login,access_token)}",
            "target", name="news")
        self.assertEqual(r.reverse("news", "aa"), "/news/aa")
        self.assertRaises(RouteReversalError, r.reverse, "news2")
示例#12
0
    def test_reverse(self):
        r = route('news', 'target', name='news')
        self.assertEqual(r.reverse('news'), '/news')
        self.assertRaises(RouteReversalError, r.reverse, 'news2')

        r = route('news/{id:int}/', 'target', name='news')
        self.assertEqual(r.reverse('news', 42), '/news/42/')
        self.assertRaises(RouteReversalError, r.reverse, 'news2')

        r = route('news/{any(login,attach,app_login,access_token)}',
                  'target', name='news')
        self.assertEqual(r.reverse('news', 'aa'), '/news/aa')
        self.assertRaises(RouteReversalError, r.reverse, 'news2')
示例#13
0
    def test_simple(self):
        r = route(route('news', 'news'), route('comments', 'comments'))

        req = Request.blank('/news')
        tr = r(req)
        self.assertEqual(len(tr.routes), 2)
        self.assertEqual((tr.args, tr.kwargs, tr.target), ((), {}, 'news'))

        req = Request.blank('/comments')
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target), ((), {}, 'comments'))

        self.assertNoMatch(r, '/newsweeek')
        self.assertNoMatch(r, '/ne')
示例#14
0
    def test_custom_url_pattern_cls(self):
        from routr.urlpattern import URLPattern

        class MyURLPattern(URLPattern):
            pass

        r = route('api',
                  route(GET, 'news', 'news', name='get-news'),
                  route(POST, 'news', 'news', name='create-news'),
                  url_pattern_cls=MyURLPattern)

        self.assertTrue(isinstance(r.pattern, MyURLPattern))
        for r in r.routes:
            self.assertTrue(isinstance(r.pattern, MyURLPattern))
示例#15
0
    def test_param_pattern_path(self):
        r = route('/news/{p:path}', 'target')

        req = Request.blank('/news/42/news')
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target),
                         ((six.u('42/news'), ), {}, 'target'))

        r = route('/news/{p:path}/comments', 'target')

        req = Request.blank('/news/42/news/comments')
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target),
                         ((six.u('42/news'), ), {}, 'target'))
示例#16
0
    def test_reverse(self):
        r = route('news', 'target', name='news')
        self.assertEqual(r.reverse('news'), '/news')
        self.assertRaises(RouteReversalError, r.reverse, 'news2')

        r = route('news/{id:int}/', 'target', name='news')
        self.assertEqual(r.reverse('news', 42), '/news/42/')
        self.assertRaises(RouteReversalError, r.reverse, 'news2')

        r = route('news/{any(login,attach,app_login,access_token)}',
                  'target',
                  name='news')
        self.assertEqual(r.reverse('news', 'aa'), '/news/aa')
        self.assertRaises(RouteReversalError, r.reverse, 'news2')
示例#17
0
文件: clients.py 项目: modulix/gwss
 def main(self):
     self.clients = {}
     routes = []
     for s in self.services.values():
         s_routes = s.public_routes
         if s_routes:
             routes.append(route(s.name, *s_routes))
     html_dir = self.config.get("html_dir", "")
     if html_dir:
         routes.append(static("", html_dir))
     self.collected_routes = route("", *routes)
     self.server = WSGIThread(self.name, self.config, self.gwss_dispatch)
     self.server.start()
     while True:
         self.listen()
示例#18
0
文件: tests.py 项目: andreypopp/routr
    def test_custom_url_pattern_cls(self):
        from routr.urlpattern import URLPattern

        class MyURLPattern(URLPattern):
            pass

        r = route(
            'api',
            route(GET, 'news', 'news', name='get-news'),
            route(POST, 'news', 'news', name='create-news'),
            url_pattern_cls=MyURLPattern)

        self.assertTrue(isinstance(r.pattern, MyURLPattern))
        for r in r.routes:
            self.assertTrue(isinstance(r.pattern, MyURLPattern))
示例#19
0
    def test_no_match(self):
        def target():
            return 'hello'

        r = route(target)
        req = Request.blank('/news')
        self.assertRaises(NoURLPatternMatched, r, req)
示例#20
0
    def test_param_pattern_path(self):
        r = route("/news/{p:path}", "target")

        req = Request.blank("/news/42/news")
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((u"42/news",), {}, "target"))

        r = route("/news/{p:path}/comments", "target")

        req = Request.blank("/news/42/news/comments")
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((u"42/news",), {}, "target"))
示例#21
0
    def test_param_guard(self):
        r = route(
            "/news/{id:int}/", "target",
            guards=qs(q=opt(String), page=opt(Int)))

        req = Request.blank("/news/42/")
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((42,), {}, "target"))

        req = Request.blank("/news/42/?q=search")
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs.items(), tr.target),
            ((42,), [("q", "search")], "target"))

        req = Request.blank("/news/42/?q=search&page=100")
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs.items(), tr.target),
            ((42,), [("q", "search"), ("page", 100)], "target"))

        self.assertRaises(
            exc.HTTPBadRequest,
            r, Request.blank("/news/42/?q=search&page=aa"))
示例#22
0
    def test_by_method(self):
        r = route("api",
            route(GET, "news_get"),
            route(POST, "news_post"))

        req = Request.blank("/api", {"REQUEST_METHOD": "POST"})
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((), {}, "news_post"))

        req = Request.blank("/api")
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((), {}, "news_get"))
示例#23
0
文件: tests.py 项目: andreypopp/routr
    def test_param_pattern_path(self):
        r = route('/news/{p:path}', 'target')

        req = Request.blank('/news/42/news')
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((six.u('42/news'),), {}, 'target'))

        r = route('/news/{p:path}/comments', 'target')

        req = Request.blank('/news/42/news/comments')
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((six.u('42/news'),), {}, 'target'))
示例#24
0
    def test_param_pattern_int(self):
        r = route('/news/{id:int}/', 'target')
        req = Request.blank('/news/42/')
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target),
                         ((42, ), {}, 'target'))

        self.assertNoMatch(r, Request.blank('/news/'))
        self.assertNoMatch(r, Request.blank('/news/a/'))
        self.assertNoMatch(r, Request.blank('/news//'))
        self.assertNoMatch(r, Request.blank('/news/122'))

        r = route('/news/{a:int}/{b:int}/{c:int}/', 'target')
        req = Request.blank('/news/42/41/40/')
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target),
                         ((42, 41, 40), {}, 'target'))
示例#25
0
    def test_method(self):
        r = route(POST, 'news', 'target')
        req = Request.blank('/news', {'REQUEST_METHOD': 'POST'})
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target), ((), {}, 'target'))

        self.assertRaises(MethodNotAllowed, r,
                          Request.blank('/news', {'REQUEST_METHOD': 'DELETE'}))
示例#26
0
文件: tests.py 项目: andreypopp/routr
 def test_match(self):
     r = route('news', 'target')
     req = Request.blank('/news')
     tr = r(req)
     self.assertEqual(len(tr.routes), 1)
     self.assertEqual(
         (tr.args, tr.kwargs, tr.target),
         ((), {}, 'target'))
示例#27
0
文件: tests.py 项目: andreypopp/routr
    def test_by_method(self):
        r = route(
            'api',
            route(GET, 'news_get'),
            route(POST, 'news_post'))

        req = Request.blank('/api', {'REQUEST_METHOD': 'POST'})
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((), {}, 'news_post'))

        req = Request.blank('/api')
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((), {}, 'news_get'))
示例#28
0
 def test_match(self):
     r = route("news", "target")
     req = Request.blank("/news")
     tr = r(req)
     self.assertEqual(len(tr.routes), 1)
     self.assertEqual(
         (tr.args, tr.kwargs, tr.target),
         ((), {}, "target"))
示例#29
0
文件: tests.py 项目: andreypopp/routr
 def test_endpoint_func(self):
     def target():
         pass
     r = route('news', target)
     self.assertNotEqual(r.pattern, None)
     self.assertEqual(r.guards, [])
     self.assertIsInstance(r, Endpoint)
     self.assertEqual(r.target, target)
示例#30
0
    def test_group_inexact_pattern(self):
        r = route("news",
                route("{id:int}",
                    route("comments", "view")))
        req = Request.blank("/news/42/comments")
        tr = r(req)
        self.assertEqual(
                (tr.args, tr.kwargs, tr.endpoint.target),
                ((42,), {}, "view"))

        r = route("news/{id:int}",
                route("comments", "view"))
        req = Request.blank("/news/42/comments")
        tr = r(req)
        self.assertEqual(
                (tr.args, tr.kwargs, tr.endpoint.target),
                ((42,), {}, "view"))

        r = route("news",
                route("{id:int}/comments", "view"))
        req = Request.blank("/news/42/comments")
        tr = r(req)
        self.assertEqual(
                (tr.args, tr.kwargs, tr.endpoint.target),
                ((42,), {}, "view"))
示例#31
0
def parse(input, namespace):
    routes = []

    for line in input.split('\n'):
        line = line.strip()
        if not is_route(line):
            continue
        method, path = method_split_re.split(line, 1)
        method = routr.HTTPMethod(method)
        guards = []
        if '?' in path:
            path, qs = path.split('?', 1)
            guards.append(compile_qs(qs))

        args = [method, path] + guards + [ref_target(method, path, namespace)]
        routes.append(routr.route(*args))

    return routr.route(*routes) if routes else None
示例#32
0
    def test_endpoint_func(self):
        def target():
            pass

        r = route('news', target)
        self.assertNotEqual(r.pattern, None)
        self.assertEqual(r.guards, [])
        self.assertIsInstance(r, Endpoint)
        self.assertEqual(r.target, target)
示例#33
0
    def test_reverse(self):
        r = route('api', route('news', 'news', name='news'),
                  route('comments', 'comments', name='comments'))
        self.assertEqual(r.reverse('news'), '/api/news')
        self.assertEqual(r.reverse('comments'), '/api/comments')
        self.assertRaises(RouteReversalError, r.reverse, 'a')

        r = route('api', route('news/{id}/', 'news', name='news'),
                  route('comments', 'comments', name='comments'))
        self.assertEqual(r.reverse('news', 'hello'), '/api/news/hello/')

        r = route('api', route(GET, 'news', name='get-news'),
                  route(POST, 'news', name='create-news'))
        self.assertEqual(r.reverse('get-news'), '/api')
        self.assertEqual(r.reverse('create-news'), '/api')
示例#34
0
    def test_param_pattern_int(self):
        r = route("/news/{id:int}/", "target")
        req = Request.blank("/news/42/")
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((42,), {}, "target"))

        self.assertNoMatch(r, Request.blank("/news/"))
        self.assertNoMatch(r, Request.blank("/news/a/"))
        self.assertNoMatch(r, Request.blank("/news//"))
        self.assertNoMatch(r, Request.blank("/news/122"))

        r = route("/news/{a:int}/{b:int}/{c:int}/", "target")
        req = Request.blank("/news/42/41/40/")
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((42, 41, 40), {}, "target"))
示例#35
0
文件: tests.py 项目: andreypopp/routr
    def test_param_pattern_int(self):
        r = route('/news/{id:int}/', 'target')
        req = Request.blank('/news/42/')
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((42,), {}, 'target'))

        self.assertNoMatch(r, Request.blank('/news/'))
        self.assertNoMatch(r, Request.blank('/news/a/'))
        self.assertNoMatch(r, Request.blank('/news//'))
        self.assertNoMatch(r, Request.blank('/news/122'))

        r = route('/news/{a:int}/{b:int}/{c:int}/', 'target')
        req = Request.blank('/news/42/41/40/')
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((42, 41, 40), {}, 'target'))
示例#36
0
文件: tests.py 项目: andreypopp/routr
    def test_method(self):
        r = route(POST, 'news', 'target')
        req = Request.blank('/news', {'REQUEST_METHOD': 'POST'})
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((), {}, 'target'))

        self.assertRaises(
            MethodNotAllowed,
            r, Request.blank('/news', {'REQUEST_METHOD': 'DELETE'}))
示例#37
0
    def test_method(self):
        r = route(POST, "news", "target")
        req = Request.blank("/news", {"REQUEST_METHOD": "POST"})
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((), {}, "target"))

        self.assertRaises(
            MethodNotAllowed,
            r, Request.blank("/news", {"REQUEST_METHOD": "DELETE"}))
示例#38
0
文件: tests.py 项目: andreypopp/routr
    def test_simple(self):
        r = route(
            route('news', 'news'),
            route('comments', 'comments'))

        req = Request.blank('/news')
        tr = r(req)
        self.assertEqual(len(tr.routes), 2)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((), {}, 'news'))

        req = Request.blank('/comments')
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((), {}, 'comments'))

        self.assertNoMatch(r, '/newsweeek')
        self.assertNoMatch(r, '/ne')
示例#39
0
    def test_simple(self):
        r = route(
            route("news", "news"),
            route("comments", "comments"))

        req = Request.blank("/news")
        tr = r(req)
        self.assertEqual(len(tr.routes), 2)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((), {}, "news"))

        req = Request.blank("/comments")
        tr = r(req)
        self.assertEqual(
            (tr.args, tr.kwargs, tr.target),
            ((), {}, "comments"))

        self.assertNoMatch(r, "/newsweeek")
        self.assertNoMatch(r, "/ne")
示例#40
0
    def test_param_pattern_string(self):
        r = route('/news/{id:string}/', 'target')

        req = Request.blank('/news/42/')
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target),
                         ((six.u('42'), ), {}, 'target'))

        req = Request.blank('/news/abcdef-12/')
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target),
                         ((six.u('abcdef-12'), ), {}, 'target'))
示例#41
0
    def test_complex_match(self):
        def news():
            return 'news'

        def comments():
            return 'comments'

        def api_news():
            return 'api_news'

        def api_comments():
            return 'api_comments'

        r = route(
            route('api', route('news', 'api_news'),
                  route('comments', 'api_comments')), route('news', 'news'),
            route('comments', 'comments'))

        req = Request.blank('/news')
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target), ((), {}, 'news'))

        req = Request.blank('/comments')
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target), ((), {}, 'comments'))

        req = Request.blank('/api/news')
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target), ((), {}, 'api_news'))

        req = Request.blank('/api/comments')
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target),
                         ((), {}, 'api_comments'))
示例#42
0
    def test_reverse(self):
        r = route("api",
            route("news", "news", name="news"),
            route("comments", "comments", name="comments"))
        self.assertEqual(r.reverse("news"), "/api/news")
        self.assertEqual(r.reverse("comments"), "/api/comments")
        self.assertRaises(RouteReversalError, r.reverse, "a")

        r = route("api",
            route("news/{id}/", "news", name="news"),
            route("comments", "comments", name="comments"))
        self.assertEqual(r.reverse("news", "hello"), "/api/news/hello/")

        r = route("api",
            route(GET, "news", name="get-news"),
            route(POST, "news", name="create-news"))
        self.assertEqual(r.reverse("get-news"), "/api")
        self.assertEqual(r.reverse("create-news"), "/api")
示例#43
0
    def test_method_inner(self):
        r = route(route('news', 'news'), route(GET, 'comments',
                                               'comments_get'),
                  route(POST, 'comments', 'comments_post'))

        req = Request.blank('/news', {'REQUEST_METHOD': 'GET'})
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target), ((), {}, 'news'))

        req = Request.blank('/news', {'REQUEST_METHOD': 'POST'})
        self.assertRaises(RouteGuarded, r, req)

        req = Request.blank('/comments', {'REQUEST_METHOD': 'POST'})
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.target),
                         ((), {}, 'comments_post'))

        req = Request.blank('/comments', {'REQUEST_METHOD': 'DELETE'})
        self.assertRaises(RouteGuarded, r, req)

        self.assertNoMatch(r, '/newsweeek')
        self.assertNoMatch(r, '/ne')
示例#44
0
def static(prefix, directory, **kw):
    """ Define a route which serves static assets

    :param prefix:
        URL prefix on which to serve static assets
    :param directory:
        directory from which to serve static assets
    """
    if prefix.endswith('/'):
        prefix = prefix[:-1]
    kw['static_view'] = True
    return route(
        GET, '%s/{path:path}' % prefix,
        make_static_view(directory), **kw)
示例#45
0
    def test_group_inexact_pattern(self):
        r = route('news', route('{id:int}', route('comments', 'view')))
        req = Request.blank('/news/42/comments')
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.endpoint.target),
                         ((42, ), {}, 'view'))

        r = route('news/{id:int}', route('comments', 'view'))
        req = Request.blank('/news/42/comments')
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.endpoint.target),
                         ((42, ), {}, 'view'))

        r = route('news', route('{id:int}/comments', 'view'))
        req = Request.blank('/news/42/comments')
        tr = r(req)
        self.assertEqual((tr.args, tr.kwargs, tr.endpoint.target),
                         ((42, ), {}, 'view'))
示例#46
0
 def test_no_match(self):
     self.assertNoMatch(route('news', 'target'), '/new')
     self.assertNoMatch(route('news', 'target'), '/newsweek')
示例#47
0
 def test_reverse(self):
     r = route('target', name='news')
     self.assertEqual(r.reverse('news'), '/')
     self.assertRaises(RouteReversalError, r.reverse, 'news2')
示例#48
0
 def test_match(self):
     r = route('news', 'target')
     req = Request.blank('/news')
     tr = r(req)
     self.assertEqual(len(tr.routes), 1)
     self.assertEqual((tr.args, tr.kwargs, tr.target), ((), {}, 'target'))
示例#49
0
# examples.py

from routr import route, POST, GET


def api_list_news():
    """ API list news items"""


def api_create_news():
    """ API create news item"""


def api_get_news(id):
    """ API get news item by ``id``"""


routes = route(
    route("api", route(GET, "news", api_list_news),
          route(POST, "news", api_create_news),
          route(GET, "news/{int}", api_get_news)))
示例#50
0
 def test_route_list_guards(self):
     r = route('api', id, route('news', 'myapp.api.news'),
               route('comments', 'myapp.api.comments'))
     self.assertNotEqual(r.pattern, None)
     self.assertEqual(r.guards, [id])
     self.assertIsInstance(r, RouteGroup)
示例#51
0
 def test_endpoint_guards(self):
     r = route('news', id, 'myapp.mytarget')
     self.assertNotEqual(r.pattern, None)
     self.assertEqual(r.guards, [id])
     self.assertIsInstance(r, Endpoint)
     self.assertEqual(r.target, 'myapp.mytarget')
示例#52
0
 def test_root_endpoint(self):
     r = route('myapp.mytarget')
     self.assertEqual(r.pattern, None)
     self.assertEqual(r.guards, [])
     self.assertIsInstance(r, Endpoint)
     self.assertEqual(r.target, 'myapp.mytarget')
示例#53
0
 def test_reverse_empty_pattern(self):
     r = route(route('news', name='news'))
     self.assertEqual(r.reverse('news'), '/')
示例#54
0
        result['title'] = doc.title
    return result


def analyse_html(url=None):
    doc = Document.from_url(url)
    return Response(doc.html)


routes = route(
    GET(
        '/analyse',
        qs(
            url=str,
            image=opt(bool),
            text=opt(bool),
            html=opt(bool),
            title=opt(bool),
            author=opt(bool),
        ), analyse),
    GET('/analyse.html', qs(url=str), analyse_html),
)


def app(environ, start_response):
    request = Request(environ)
    try:
        tr = routes(request)
        response = tr.target(*tr.args, **tr.kwargs)
    except NoMatchFound as e:
        response = e.response
示例#55
0
    """ """
    with storage_type.get_session() as storage:
        results = storage.list_products()
    return '\n'.join(['{} : {}'.format(k, v) for k, v in results.iteritems()])


# Routing
# ----------------------------------------------------------------------------
routes = route(
    route(GET, '{}availability.service.php'.format(url_prefix),
          get_availability),
    route(POST, '{}availability.service.php'.format(url_prefix),
          get_availability),
    route(GET, '{}data-noRegion.service.php'.format(url_prefix), get_datasets),
    route(GET, '{}findnearestdata.service.php'.format(url_prefix),
          find_nearest_dataset),
    route(GET, '{}histogram.service.php'.format(url_prefix), histogram),
    route(GET, '{}downloadRaster.service.php'.format(url_prefix),
          download_png),
    route(GET, '{}downloadNumpy.service.php'.format(url_prefix),
          download_numpy),
    route(GET, '{}search/'.format(url_prefix), search_datasets),
    route(GET, '{}listProducts/'.format(url_prefix), list_products),
)


# WSGI application
# ----------------------------------------------------------------------------
def application(environ, start_response):
    request = Request(environ)
    try:
        trace = routes(request)