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)
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")
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')
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)
def test_no_match(self): self.assertNoMatch( route("news", "target"), "/new") self.assertNoMatch( route("news", "target"), "/newsweek")
def test_no_match(self): self.assertNoMatch( route('news', 'target'), '/new') self.assertNoMatch( route('news', 'target'), '/newsweek')
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")
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)
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'))
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))
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")
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')
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')
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))
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'))
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()
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))
def test_no_match(self): def target(): return 'hello' r = route(target) req = Request.blank('/news') self.assertRaises(NoURLPatternMatched, r, req)
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"))
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"))
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"))
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'))
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'))
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'}))
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'))
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'))
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"))
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)
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"))
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
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')
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"))
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'))
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'}))
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"}))
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')
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")
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'))
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'))
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")
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')
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)
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'))
def test_no_match(self): self.assertNoMatch(route('news', 'target'), '/new') self.assertNoMatch(route('news', 'target'), '/newsweek')
def test_reverse(self): r = route('target', name='news') self.assertEqual(r.reverse('news'), '/') self.assertRaises(RouteReversalError, r.reverse, 'news2')
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'))
# 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)))
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)
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')
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')
def test_reverse_empty_pattern(self): r = route(route('news', name='news')) self.assertEqual(r.reverse('news'), '/')
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
""" """ 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)