def test_nested_cases(self): 'Locations of nested web.cases' chain = web.cases( web.match('/', 'index'), web.cases( web.match('/docs', 'docs'))) self.assert_(web.locations(chain).keys(), ['index', 'docs'])
def test_prefix_and_cases(self): 'Locations of web.cases with prefix' chain = web.prefix('/news') | web.cases(web.match('/', 'index'), web.match('/docs', 'docs')) for k in ('index', 'docs'): self.assert_(web.locations(chain).keys(), [k]) self.assert_('builders' in web.locations(chain)[k]) self.assertEqual(len(web.locations(chain)[k]['builders']), 2)
def test_subdomains_and_cases(self): 'Locations of web.cases with subdomains' chain = web.subdomain('news') | web.cases(web.match('/', 'index'), web.match('/docs', 'docs')) for k in ('index', 'docs'): self.assert_(web.locations(chain).keys(), [k]) self.assert_('subdomains' in web.locations(chain)[k]) self.assertEqual(web.locations(chain)[k]['subdomains'], ['news'])
def test_prefix_and_cases(self): 'Locations of web.cases with prefix' chain = web.prefix('/news') | web.cases( web.match('/', 'index'), web.match('/docs', 'docs')) for k in ('index', 'docs'): self.assert_(web.locations(chain).keys(), [k]) self.assert_('builders' in web.locations(chain)[k]) self.assertEqual(len(web.locations(chain)[k]['builders']), 2)
def test_nested_cases(self): 'Reverse with nested web.cases' chain = web.cases( web.cases(web.match('/', 'index'), web.match('/docs', 'docs'), web.cases(web.match('/news', 'news')))) r = web.Reverse.from_handler(chain) self.assertEqual(r('index'), '/') self.assertEqual(r('docs'), '/docs') self.assertEqual(r('news'), '/news')
def test_subdomains_and_cases(self): 'Locations of web.cases with subdomains' chain = web.subdomain('news') | web.cases( web.match('/', 'index'), web.match('/docs', 'docs')) for k in ('index', 'docs'): self.assert_(web.locations(chain).keys(), [k]) self.assert_('subdomains' in web.locations(chain)[k]) self.assertEqual(web.locations(chain)[k]['subdomains'], ['news'])
def test_few_handlers(self): 'Reverse a few handlers' chain = web.cases( web.match('/', 'index'), web.match('/docs', 'docs'), web.match('/news', 'news'), ) r = web.Reverse.from_handler(chain) self.assertEqual(r('index'), '/') self.assertEqual(r('docs'), '/docs') self.assertEqual(r('news'), '/news')
def test_host10(self): app = web.cases ( web.subdomain(u'bla') | web.match('/', 'bla') | (lambda e,d,n: Response(body='bla') ), web.subdomain(u'localhost') | web.match('/', 'localhost') | (lambda e,d,n: Response(body='localhost')) ) self.assertEqual(web.ask(app, 'http://bla/').body, 'bla') self.assertEqual(web.ask(app, '/').body, 'localhost')
def test_int_converter(self): '''Check int converter''' def handler(env, data, nx): self.assertEqual(data.id, 42) return Response() app = web.cases( web.match('/first', 'first') | handler, web.match('/second/<int:id>', 'second') | handler) web.ask(app, '/second/42')
def test_not_found(self): '''Check int converter with handler which accepts params''' def handler(env, data, nx): return Response() app = web.cases( web.match('/first', 'first') | handler, web.match('/second/<int:id>', 'second') | handler) self.assert_(web.ask(app, '/second/42/') is None) self.assert_(web.ask(app, '/second/42s') is None)
def test_nested_cases(self): 'Reverse with nested web.cases' chain = web.cases( web.cases( web.match('/', 'index'), web.match('/docs', 'docs'), web.cases( web.match('/news', 'news')))) r = web.Reverse.from_handler(chain) self.assertEqual(r('index'), '/') self.assertEqual(r('docs'), '/docs') self.assertEqual(r('news'), '/news')
def test_multiple_int_convs(self): '''Check multiple int converters''' def handler(env, data, nx): self.assertEqual(data.id, 42) self.assertEqual(data.param, 23) return Response() app = web.cases( web.match('/first', 'first') | handler, web.match('/second/<int:id>/<int:param>', 'second') | handler) web.ask(app, '/second/42/23')
def test_unicode(self): 'Reverse with unicode' # various combinations of url parts containing unicode chain = web.subdomain(u'п') | web.cases( web.prefix(u'/з') | web.match('/', 'unicode1'), web.prefix(u'/з') | web.match('/<string:slug>', 'unicode2'), web.match(u'/д/<string:slug>', 'unicode3'), #regression web.match(u'/<string:slug1>/<string:slug2>', 'unicode4'), #regression ) r = web.Reverse.from_handler(chain) self.assertEqual(r('unicode1'), 'http://xn--o1a/%D0%B7/') self.assertEqual(r('unicode2', slug=u'ю'), 'http://xn--o1a/%D0%B7/%D1%8E') self.assertEqual(r('unicode3', slug=u'ю'), 'http://xn--o1a/%D0%B4/%D1%8E') self.assertEqual(r('unicode4', slug1=u'д', slug2=u'ю'), 'http://xn--o1a/%D0%B4/%D1%8E')
def test_nested_cases_with_prefixes(self): 'Reverse with nested web.cases with web.prefixes' chain = web.cases( web.match('/', 'index'), web.prefix('/docs') | web.cases(web.match('/<int:id>', 'doc'), web.match('/list', 'docs')), web.prefix('/news') | web.cases(web.match('/<int:id>', 'news'), web.match('/list', 'newslist'))) r = web.Reverse.from_handler(chain) self.assertEqual(r('index'), '/') self.assertEqual(r('docs'), '/docs/list') self.assertEqual(r('newslist'), '/news/list') self.assertEqual(r('doc', id=1), '/docs/1') self.assertEqual(r('news', id=1), '/news/1')
def test_simple_match(self): '''Check simple case of match''' app = web.match('/first', 'first') | (lambda env, data, nx: Response()) self.assertEqual(web.ask(app, '/first').status_int, 200) self.assertEqual(web.ask(app, '/second'), None)
def test_unicode(self): '''IRI tests''' app = web.subdomain(u'рф') | web.subdomain(u'сайт') | web.match('/', 'site') | (lambda e,d,n: Response() ) encoded = 'http://xn--80aswg.xn--p1ai/' self.assertEqual(web.Reverse.from_handler(app)('site').get_readable(), u'http://сайт.рф/') self.assertEqual(web.Reverse.from_handler(app)('site'), encoded) self.assertNotEqual(web.ask(app, encoded), None)
def test_subdomain(self): 'Locations and subdomains' chain = web.subdomain('news') | web.match('/', 'index') self.assert_(web.locations(chain).keys(), ['index']) self.assert_('subdomains' in web.locations(chain)['index']) self.assertEqual(web.locations(chain)['index']['subdomains'], ['news'])
def login_handler(self): """ This property will return component which will handle login requests. It is good idea to append some template rendering handler after this component to see `login_form`. auth.logout_handler | render_to('login.html') """ def login(env, data, next_handler): form = self._login_form(env) next = env.request.GET.get("next", "/") msg = "" if env.request.method == "POST": if form.accept(env.request.POST): user_id, msg = self._user_by_credential(env, **form.python_data) if user_id is not None: response = self.login(user_id) response.status = 303 response.headers["Location"] = next.encode("utf-8") return response data.form = form data.message = msg data.login_url = env.url_for(self._login).set(next=next) return next_handler(env, data) return web.match("/%s" % self._login, self._login) | login
def login_handler(self): ''' This property will return component which will handle login requests. It is good idea to append some template rendering handler after this component to see `login_form`. auth.logout_handler | render_to('login.html') ''' def login(env, data, next_handler): form = self._login_form(env) next = env.request.GET.get('next', '/') msg = '' if env.request.method == 'POST': if form.accept(env.request.POST): user_id, msg = self._user_by_credential(env, **form.python_data) if user_id is not None: response = self.login(user_id) response.status = 303 response.headers['Location'] = next.encode('utf-8') return response data.form = form data.message = msg data.login_url = env.url_for(self._login).set(next=next) return next_handler(env, data) return web.match('/%s' % self._login, self._login) | login
def test_mix(self): 'Loactions mix' chain = web.prefix('/items') | web.cases( web.match('/', 'index'), web.prefix('/news') | web.namespace('news') | web.cases( web.match('/', 'index'), web.match('/<int:id>', 'item')), web.prefix('/docs') | web.namespace('docs') | web.cases( web.match('/', 'index'), web.match('/<int:id>', 'item'))) locations = web.locations(chain) self.assertEqual( locations.keys(), ['index', 'docs.index', 'docs.item', 'news.item', 'news.index']) self.assertEqual(len(locations['index']['builders']), 2) self.assertEqual(len(locations['news.index']['builders']), 3) self.assertEqual(len(locations['news.item']['builders']), 3) self.assertEqual(len(locations['docs.index']['builders']), 3) self.assertEqual(len(locations['docs.item']['builders']), 3)
def test_prefix_leaf(self): '''Simple prefix''' def handler(env, data, nx): self.assertEqual(env.request.prefixed_path, '/item') return Response() app = web.cases( web.match('/', 'index'), web.prefix('/docs') | web.cases( web.match('/', 'docs') | handler, web.match('/item', 'doc') | handler, web.prefix('/tags') | web.cases( web.match('/', 'tags') | handler, web.match('/tag', 'tag') | handler))) self.assertEqual(web.ask(app, '/docs/item').status_int, 200)
def test_nested_cases_with_prefixes(self): 'Reverse with nested web.cases with web.prefixes' chain = web.cases( web.match('/', 'index'), web.prefix('/docs') | web.cases( web.match('/<int:id>', 'doc'), web.match('/list', 'docs')), web.prefix('/news') | web.cases( web.match('/<int:id>', 'news'), web.match('/list', 'newslist'))) r = web.Reverse.from_handler(chain) self.assertEqual(r('index'), '/') self.assertEqual(r('docs'), '/docs/list') self.assertEqual(r('newslist'), '/news/list') self.assertEqual(r('doc', id=1), '/docs/1') self.assertEqual(r('news', id=1), '/news/1')
def test_mix(self): 'Loactions mix' chain = web.prefix('/items') | web.cases( web.match('/', 'index'), web.prefix('/news') | web.namespace('news') | web.cases( web.match('/', 'index'), web.match('/<int:id>', 'item')), web.prefix('/docs') | web.namespace('docs') | web.cases( web.match('/', 'index'), web.match('/<int:id>', 'item'))) locations = web.locations(chain) self.assertEqual(locations.keys(), ['index', 'docs.index', 'docs.item', 'news.item', 'news.index']) self.assertEqual(len(locations['index']['builders']), 2) self.assertEqual(len(locations['news.index']['builders']), 3) self.assertEqual(len(locations['news.item']['builders']), 3) self.assertEqual(len(locations['docs.index']['builders']), 3) self.assertEqual(len(locations['docs.item']['builders']), 3)
def test_prefix_root(self): '''Prefix root''' def handler(env, data, nx): self.assertEqual(env.request.prefixed_path, '/') return Response() app = web.cases( web.match('/', 'index') | handler, web.prefix('/docs') | web.cases( web.match('/', 'docs') | handler, web.match('/item', 'doc') | handler, web.prefix('/tags') | web.cases( web.match('/', 'tags') | handler, web.match('/tag', 'tag') | handler))) self.assertEqual(web.ask(app, '/docs'), None) self.assertEqual(web.ask(app, '/docs/').status_int, 200) self.assertEqual(web.ask(app, '/docs/tags'), None) self.assertEqual(web.ask(app, '/docs/tags/').status_int, 200) self.assertEqual(web.ask(app, '/docs/tags/asdasd'), None)
def test_subdomain(self): '''Subdomain filter''' def handler(env, data, nx): self.assertEqual(env.request.path, '/') return Response() app = web.subdomain('host') | web.cases( web.subdomain('') | web.match('/', 'index') | handler, web.subdomain('k') | web.cases( web.subdomain('l') | web.cases( web.match('/', 'l') | handler, ), web.subdomain('') | web.match('/', 'k') | handler)) self.assertEqual(web.ask(app, 'http://host/').status_int, 200) self.assertEqual(web.ask(app, 'http://k.host/').status_int, 200) self.assertEqual(web.ask(app, 'http://l.k.host/').status_int, 200) self.assertEqual(web.ask(app, 'http://x.l.k.host/').status_int, 200) self.assert_(web.ask(app, 'http://x.k.host/') is None) self.assert_(web.ask(app, 'http://lk.host/') is None) self.assert_(web.ask(app, 'http://mhost/') is None)
def logout_handler(self): ''' This property will return component which will handle logout requests. It only handles POST requests and do not display any rendered content. This handler deletes session id from `session_storage`. If there is no session id provided or id is incorrect handler silently redirects to login url and does not throw any exception. ''' def logout(env, data, next_handler): if self._cookie_name in env.request.cookies: response = self.logout(env.request) response.status = 303 response.headers['Location'] = '/' return response return next_handler(env, data) return web.match('/%s' % self._logout, self._logout) | logout
def logout_handler(self): """ This property will return component which will handle logout requests. It only handles POST requests and do not display any rendered content. This handler deletes session id from `session_storage`. If there is no session id provided or id is incorrect handler silently redirects to login url and does not throw any exception. """ def logout(env, data, next_handler): if self._cookie_name in env.request.cookies: response = self.logout(env.request) response.status = 303 response.headers["Location"] = "/" return response return next_handler(env, data) return web.match("/%s" % self._logout, self._logout) | logout
def test_unicode(self): '''Routing rules with unicode''' # XXX move to urltemplate and reverse tests? app = web.cases( web.prefix(u'/հայերեն') | web.cases( web.match(u'/%', 'percent') | (lambda e,d,n: Response()) ) ) encoded = '/%D5%B0%D5%A1%D5%B5%D5%A5%D6%80%D5%A5%D5%B6/%25' self.assertEqual(web.Reverse.from_handler(app)('percent'), encoded) self.assertEqual(web.Reverse.from_handler(app)('percent').get_readable(), u'/հայերեն/%') self.assertNotEqual(web.ask(app, encoded), None) # ??? # rctx have prefixes, so we need new one self.assertEqual(web.ask(app, encoded).status_int, 200)
def test_match(self): 'Locations of web.match' self.assert_(web.locations(web.match('/', 'name')).keys(), ['name'])
def test_namespace_with_empty_name(self): 'Namespaces with empty url name' chain = web.namespace('news') | web.match('/', '') self.assert_(web.locations(chain).keys(), ['news'])
def test_namespace(self): 'Locations namespace' chain = web.namespace('news') | web.match('/', 'index') self.assert_(web.locations(chain).keys(), ['news.index'])
def test_namespace_and_cases(self): 'Locations namespace with web.cases' chain = web.namespace('news') | web.cases( web.match('/', 'index'), web.match('/docs', 'docs')) self.assertEqual(web.locations(chain).keys(), ['news.index', 'news.docs'])
def test_prefix(self): 'Locations of web.match with prefix' chain = web.prefix('/news') | web.match('/', 'index') self.assert_(web.locations(chain).keys(), ['index']) self.assert_('builders' in web.locations(chain)['index']) self.assertEqual(len(web.locations(chain)['index']['builders']), 2)
def test_one_handler(self): 'Reverse one match' r = web.Reverse.from_handler(web.match('/', 'index')) self.assertEqual(r('index'), '/')
def test_nested_cases(self): 'Locations of nested web.cases' chain = web.cases(web.match('/', 'index'), web.cases(web.match('/docs', 'docs'))) self.assert_(web.locations(chain).keys(), ['index', 'docs'])
def test_match_dublication(self): 'Raise error on same url names' self.assertRaises(ValueError, lambda: web.locations( web.cases( web.match('/', 'index'), web.match('/index', 'index'))))
def test_namespace_and_cases(self): 'Locations namespace with web.cases' chain = web.namespace('news') | web.cases(web.match('/', 'index'), web.match('/docs', 'docs')) self.assertEqual( web.locations(chain).keys(), ['news.index', 'news.docs'])
# redirect shortcut env.redirect_to = redirect_to try: return next_handler(env, data) finally: env.db.close() app = web.handler(config) | web.cases( static, auth.login_handler | template.render_to('login'), auth.logout_handler, auth | web.cases( web.match('/', 'dashboard') | views.dashboard, web.prefix('/issue') | web.cases( web.match('/<int:issue>', 'issue') | issue.get | web.cases( web.method('get'), web.method('post') | auth.login_required | issue.update, ) | template.render_to('issue'), ), web.prefix('/proj') | web.cases( web.match('', 'create-project') | auth.login_required | project.create, web.prefix('/<int:proj>') | project.get | web.cases( web.match('', 'project') | template.render_to('proj'), web.match('/update', 'update-project') | project.update, web.match('/issue', 'create-issue') | issue.create, )
def test_match_dublication(self): 'Raise error on same url names' self.assertRaises( ValueError, lambda: web.locations( web.cases(web.match('/', 'index'), web.match( '/index', 'index'))))