def test_aliases(self): def handler(env, data): return Response(env.current_location + ' ' + env.root.domain1.as_url + ' ' + env.root.domain1.as_url.with_host() + ' ' + env.root.domain2.as_url + ' ' + env.root.domain2.as_url.with_host()) www = web.subdomain('', 'www') app = web.subdomain('example.com', 'example.ru', 'example.com.ua') | web.cases( web.subdomain('ru', None, primary=None) | web.cases( web.subdomain('moscow') | www | web.match('/', 'domain3'), www | web.match('/', 'domain1') ), web.subdomain('en', 'eng') | www | web.match('/', 'domain2'), ) app = app | handler # XXX As for now .with_host() return primary domain, not the current one # It is easy to change the behaviour, but which behaviour is # correct? self.assertEqual(web.ask(app, 'http://ru.example.com/').body, 'domain1 / http://example.com/ ' 'http://en.example.com/ http://en.example.com/') self.assertEqual(web.ask(app, 'http://www.ru.example.ru/').body, 'domain1 / http://example.com/ ' 'http://en.example.com/ http://en.example.com/') self.assertEqual(web.ask(app, 'http://www.example.ru/').body, 'domain1 / http://example.com/ ' 'http://en.example.com/ http://en.example.com/') self.assertEqual(web.ask(app, 'http://moscow.example.ru/').body, 'domain3 http://example.com/ http://example.com/ ' 'http://en.example.com/ http://en.example.com/')
def create_handler(app): h_auth = AdminAuth(models.admin.AdminUser, storage=app.cache) item_lock = ItemLockView() ext_handlers = [] if app.preview_enabled: h_preview = h_app('/preview', name='preview', app=app.preview_app) ext_handlers.append(h_preview) return flash_message_handler | web.cases( static_files(app.cfg.CMS34_STATIC_DIR, app.cfg.CMS34_STATIC_URL), static_files(app.cfg.CMS_STATIC_DIR, app.cfg.CMS_STATIC_URL), static_files(app.cfg.STATIC_DIR, app.cfg.STATIC_URL), static_files(app.cfg.PRIVATE_MEDIA_DIR, app.cfg.PRIVATE_MEDIA_URL), static_files(app.cfg.ADMIN_FORM_TMP_DIR, app.cfg.ADMIN_FORM_TMP_URL), static_files(app.cfg.SHARED_MEDIA_DIR, app.cfg.SHARED_MEDIA_URL), static_files(app.cfg.ADMIN_MEDIA_DIR, app.cfg.ADMIN_MEDIA_URL), Rule('/pack.js', h.packer.js_packer), Rule('/pack.css', h.packer.css_packer), h_auth.login(), h_auth.logout(), h_auth | auth_required | web.cases( Rule('/', h.index, method='GET'), item_lock.app, app.streams.to_app(), web.cases(*ext_handlers), ), HTTPNotFound, # XXX does not work without this )
def test_subdomain(self): '''Subdomain filter''' def handler(env, data): self.assertEqual(env.request.path, '/') return Response() def handler_with_prefix(env, data): self.assertEqual(env.request.path, '/j') 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), web.subdomain('j') | web.match('/', 'j1') | handler, web.subdomain('j') | web.match('/j', 'j2') | handler_with_prefix) 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.assertEqual(web.ask(app, 'http://j.host/').status_int, 200) # Test if subdomains in routing are cleaned properly self.assertEqual(web.ask(app, 'http://j.host/j').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 test_nested_prefixes(self): 'Reverse with nested prefexes' app = web.prefix('/news/<section>', name='news') | web.cases( web.match(), web.prefix('/<int:id>', name='item') | web.cases( web.match(), web.prefix('/docs', name='docs') | web.cases( web.match(), web.match('/<int:id>', 'item')))) r = web.Reverse.from_handler(app) # Normal behavior self.assertEqual(r.news(section='top').as_url, '/news/top') self.assertEqual(r.news(section='top').item(id=1).as_url, '/news/top/1') self.assertEqual(r.news(section='top').item(id=1).docs.as_url, '/news/top/1/docs') self.assertEqual(r.news(section='top').item(id=1).docs.item(id=2).as_url, '/news/top/1/docs/2') # Exceptional behavior self.assertRaises(UrlBuildingError, lambda: r.news.as_url) # XXX this one raises KeyError, not UrlBuildingError self.assertRaises(UrlBuildingError, lambda: r.news(foo='top').as_url) self.assertRaises(UrlBuildingError, lambda: r.news(section='top').item.as_url) self.assertRaises(UrlBuildingError, lambda: r.news(section='top').item(id=1).docs()) self.assertRaises(UrlBuildingError, lambda: r.news(section='top')()) self.assertRaises(UrlBuildingError, lambda: r.news.item)
def test_nested_cases(self): 'Locations of nested web.cases' chain = web.cases( web.match('/', 'index'), web.cases( web.match('/docs', 'docs'))) self.assert_(location_keys(chain), ['index', 'docs'])
def test_nested_prefixes(self): "Reverse with nested prefexes" app = ( web.prefix("/news/<section>") | web.namespace("news") | web.cases( web.match(), web.prefix("/<int:id>") | web.namespace("item") | web.cases( web.match(), web.prefix("/docs") | web.namespace("docs") | web.cases(web.match(), web.match("/<int:id>", "item")), ), ) ) r = web.Reverse.from_handler(app) # Normal behavior self.assertEqual(r.news(section="top").as_url, "/news/top") self.assertEqual(r.news(section="top").item(id=1).as_url, "/news/top/1") self.assertEqual(r.news(section="top").item(id=1).docs.as_url, "/news/top/1/docs") self.assertEqual(r.news(section="top").item(id=1).docs.item(id=2).as_url, "/news/top/1/docs/2") # Exceptional behavior self.assertRaises(UrlBuildingError, lambda: r.news.as_url) # XXX this one raises KeyError, not UrlBuildingError self.assertRaises(UrlBuildingError, lambda: r.news(foo="top").as_url) self.assertRaises(UrlBuildingError, lambda: r.news(section="top").item.as_url) self.assertRaises(UrlBuildingError, lambda: r.news(section="top").item(id=1).docs()) self.assertRaises(UrlBuildingError, lambda: r.news(section="top")()) self.assertRaises(UrlBuildingError, lambda: r.news.item)
def setUp(self): class Storage(object): def set(self, *args, **kwargs): return False def delete(self, *args, **kwargs): return False auth = self.auth = CookieAuth(get_user_identity, identify_user, storage=Storage()) def anonymouse(env, data): self.assertTrue(hasattr(env, 'user')) self.assertEqual(env.user, None) return web.Response('ok') def no_anonymouse(env, data): self.assertTrue(hasattr(env, 'user')) self.assertEqual(env.user, MockUser(name='user name')) return web.Response('ok') self.app = web.cases( auth.login(), auth.logout(), auth | web.cases( web.match('/a', 'a') | anonymouse, web.match('/b', 'b') | auth_required | no_anonymouse, ), )
def test_subdomain(self): '''Subdomain filter''' def handler(env, data): self.assertEqual(env.request.path, '/') return Response() def handler_with_prefix(env, data): self.assertEqual(env.request.path, '/j') 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), web.subdomain('j') | web.match('/', 'j1') | handler, web.subdomain('j') | web.match('/j', 'j2') | handler_with_prefix) 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.assertEqual(web.ask(app, 'http://j.host/').status_int, 200) # Test if subdomains in routing are cleaned properly self.assertEqual(web.ask(app, 'http://j.host/j').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 test_subreverse(self): chain = web.cases( web.prefix('/docs', name='docs') | web.cases( web.match('/<int:id>', 'doc'), web.match('/list', 'docs'))) r = web.Reverse.from_handler(chain) self.assertEqual(r.build_subreverse('docs.doc', id=1).as_url, '/docs/1') self.assertEqual(r.build_subreverse('docs').doc(id=1).as_url, '/docs/1')
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.as_url, "/") self.assertEqual(r.docs.as_url, "/docs") self.assertEqual(r.news.as_url, "/news")
def app(self): return web.prefix('/tray') | web.cases( Rule('/<int:tray>', self.tray), web.prefix('/_') | web.method('POST', strict=True) | web.cases( Rule('/put', self.put_to_tray), Rule('/user/put', self.put_to_user_tray), Rule('/delete', self.delete_from_tray), ) )
def test_nested_prefix_without_ns(self): chain = web.prefix('/docs', name='docs') | web.cases( web.match('/', name='all'), web.prefix('/news') | web.cases( web.match('/', 'news_index'), web.match('/all', 'news_all') ) ) r = web.Reverse.from_handler(chain) self.assertEqual(r.docs.all.as_url, '/docs/') self.assertEqual(r.docs.news_index.as_url, '/docs/news/')
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.as_url, '/') self.assertEqual(r.docs.as_url, '/docs') self.assertEqual(r.news.as_url, '/news')
def setUp(self): from sqlalchemy import Column, Integer, String, create_engine, orm from sqlalchemy.schema import MetaData from sqlalchemy.ext.declarative import declarative_base metadata = MetaData() Model = declarative_base(metadata=metadata) class User(Model): __tablename__ = 'users' id = Column(Integer, primary_key=True) login = Column(String(255), nullable=False, unique=True) password = Column(String(255), nullable=False) auth = SqlaModelAuth(User) class Env(web.AppEnvironment): @cached_property def db(self): return orm.sessionmaker(bind=create_engine('sqlite://'))() @cached_property def template(self): return MockTemplateManager() @web.request_filter def make_env(env, data, nxt): metadata.create_all(env.db.bind) user = User(login='******', password=encrypt_password('123')) env.db.add(user) env.db.commit() try: return nxt(env, data) finally: env.db.close() def anonymouse(env, data): self.assertTrue(hasattr(env, 'user')) self.assertEqual(env.user, None) return web.Response('ok') def no_anonymouse(env, data): self.assertTrue(hasattr(env, 'user')) self.assertEqual(env.user.login, 'user name') return web.Response('ok') app = make_env | web.cases( auth.login(), auth.logout(), auth | web.cases( web.match('/a', 'a') | anonymouse, web.match('/b', 'b') | auth_required | no_anonymouse, ), ) self.app = web.Application(app, Env)
def create_handler(app): h_caching = Caching(app.cache, duration=app.cfg.CACHE_TIME) return cases( static_files(app.cfg.STATIC_DIR, app.cfg.STATIC_URL), static_files(app.cfg.FRONT_MEDIA_DIR, app.cfg.MEDIA_URL), h_caching | cache(enable=app.cfg.CACHE_ENABLED) | cases( prefix('/en') | app.i18n.handler('en') | h_index, app.i18n.handler('ru') | h_index, ), HTTPNotFound, )
def test_current_location(self): def test_ns(env, data): return env.current_location app = web.cases( web.match('/', 'm1'), web.prefix('/ns', name="ns1.ns2") | web.cases( web.match(''), web.match('/m2', 'm2'), )) | test_ns self.assertEqual(web.ask(app, '/'), 'm1') self.assertEqual(web.ask(app, '/ns'), 'ns1.ns2') self.assertEqual(web.ask(app, '/ns/m2'), 'ns1.ns2.m2')
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.as_url, "/") self.assertEqual(r.docs.as_url, "/docs/list") self.assertEqual(r.newslist.as_url, "/news/list") self.assertEqual(r.doc(id=1).as_url, "/docs/1") self.assertEqual(r.news(id=1).as_url, "/news/1")
def test_chain_to_cases_with_functions(self): @F def h(env, data, nx): count = nx(env, data) or 0 return count + 1 def e(env, data): return 10 chain = web.cases(h | e, h) | h self.assertEqual(chain(VS(), VS()), 11) chain = web.cases(e, h) | h self.assertEqual(chain(VS(), VS()), 10)
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: 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.as_url, encoded) self.assertEqual(web.Reverse.from_handler(app).percent.as_url.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_current_location(self): def test_ns(env, data): return env.current_location app = web.cases( web.match('/', 'm1'), web.prefix('/ns', name="ns1.ns2") | web.cases( web.match(''), web.match('/m2', 'm2'), ) ) | test_ns self.assertEqual(web.ask(app, '/'), 'm1') self.assertEqual(web.ask(app, '/ns'), 'ns1.ns2') self.assertEqual(web.ask(app, '/ns/m2'), 'ns1.ns2.m2')
def test_multiple_params_with_endpoints(self): app = web.prefix('/persons/<int:person_id>') | web.namespace('persons') |\ web.cases( web.match('/index', ''), web.prefix('/news') | web.namespace('news') | web.cases( web.match('/index', ''), web.match('/<int:news_id>', 'item') ) ) r = web.Reverse.from_handler(app) r1 = r.persons(person_id=1).news.item(news_id=2) self.assertEqual(r1.as_url, '/persons/1/news/2') self.assertEqual(r.build_url('persons.news.item', person_id=1, news_id=2), '/persons/1/news/2')
def test_match_fragment(self): app = web.prefix('/x') | web.cases( web.match('/', 'index', fragment='index'), web.match('/', 'cyrillic', fragment=u'я'), web.match('/', 'page', fragment='page<int:page>'), web.prefix('/z', name="z") | web.cases( web.match('', name="", fragment="z") ) ) r = web.Reverse.from_handler(app) self.assertEqual(r.index.as_url, '/x/#index') self.assertEqual(r.cyrillic.as_url, '/x/#%D1%8F') self.assertEqual(r.page(page=1).as_url, '/x/#page1') self.assertEqual(r.z.as_url, '/x/z#z')
def test_multiple_params_with_endpoints(self): app = ( web.prefix("/persons/<int:person_id>") | web.namespace("persons") | web.cases( web.match("/index", ""), web.prefix("/news") | web.namespace("news") | web.cases(web.match("/index", ""), web.match("/<int:news_id>", "item")), ) ) r = web.Reverse.from_handler(app) r1 = r.persons(person_id=1).news.item(news_id=2) self.assertEqual(r1.as_url, "/persons/1/news/2") self.assertEqual(r.build_url("persons.news.item", person_id=1, news_id=2), "/persons/1/news/2")
def test_chain_of_lists(self): 'Chain of lists, data check' @F def h(env, data, nx): data.count = 1 return nx(env, data) @F def h1(env, data, nx): self.assert_(hasattr(data, 'count')) self.assertEqual(data.count, 1) return nx(env, data) chain = web.cases(h) | web.cases(h1, h1) chain(VS(), VS())
def test_prefix_with_zeros_in_int(self): '''Simple prefix''' from iktomi.web.url_converters import Converter, ConvertError def handler(env, data): return Response() class ZeroInt(Converter): def to_python(self, value, env=None): try: value = int(value) except ValueError: raise ConvertError(self.name, value) else: return value def to_url(self, value): return str(value) app = web.cases( web.prefix('/section/<int:section_id>', convs={'int': ZeroInt}) | web.match('/item', 'doc') | handler) #self.assertEqual(web.ask(app, '/section/1').status_int, 200) self.assertEqual(web.ask(app, '/section/1/item').status_int, 200) self.assertEqual(web.ask(app, '/section/001/item').status_int, 200)
def app(self): return web.method('POST') | web.cases( Rule('/_update_lock/<string:item_id>/<string:edit_session>', self.update_lock), Rule('/_force_lock/<string:item_id>', self.force_lock), Rule('/_release_lock/<string:item_id>/<string:edit_session>', self.release_lock))
def test_list_of_chains(self): 'cases of chains' @F def h1(env, data, nh): self.assertEqual(env.count, 0) env.count = env.count + 1 return nh(env, data) @F def h2(env, data, nh): self.assertEqual(env.count, 0) env.count = env.count + 1 return nh(env, data) @F def h3(env, data, nh): self.assertEqual(env.count, 1) env.count = env.count + 1 return nh(env, data) chain = web.cases(h1, h2 | h3) env = VS(count=0) self.assert_(chain(env, VS()) is None) self.assertEqual(env.count, 0)
def test_List(self): 'cases handle' @F def h1(env, data, nh): self.assertEqual(env.count, 0) env.count = env.count + 1 return nh(env, data) @F def h2(env, data, nh): self.assertEqual(env.count, 0) env.count = env.count + 1 return nh(env, data) @F def h3(env, data, nh): self.assertEqual(env.count, 0) env.count = env.count + 1 return nh(env, data) chain = web.cases(h1, h2, h3) env = VersionedStorage(count=0) self.assert_(chain(env, VS()) is None) self.assertEqual(env.count, 0)
def app(self): prepare = self.PrepareItemHandler(self) return web.cases( web.match('', '') | prepare | self, web.match('/autosave', 'autosave') | \ web.method('POST', strict=True) | prepare | self.autosave )
def test_external_urls_no_subdomain(self): 'External URL reverse with no subdomains provided in location' def config(env, data, nxt): env.root = root.bind_to_env(env) return nxt(env, data) called_urls = [] def get_handler(num, result): def handler(env, data): self.assertEqual(env.root.url1.as_url.with_host(), result) called_urls.append(num) return Response() return handler url1 = get_handler(1, 'http://example.com/url') url2 = get_handler(2, 'http://example.com:8000/url') url3 = get_handler(3, 'https://example.com:80/url') app = web.request_filter(config) | web.cases( web.match('/url', 'url1') | url1, web.match('/url2', 'url2') | url2, web.match('/url3', 'url3') | url3, ) root = web.Reverse.from_handler(app) assert web.ask(app, 'http://example.com/url') assert web.ask(app, 'http://example.com:8000/url2') assert web.ask(app, 'https://example.com:80/url3') assert called_urls == [1,2,3]
def test_external_urls(self): 'External URL reverse' def host1(env, data): self.assertEqual(env.root.host1.as_url.with_host(), 'http://host1.example.com/url') self.assertEqual(env.root.host2.as_url, 'http://host2.example.com/url') self.assertEqual(env.root.host1.as_url, '/url') self.host1_called = True return Response() def host2(env, data): self.assertEqual(env.root.host2.as_url.with_host(), 'https://host2.example.com/url') self.assertEqual(env.root.host1.as_url, 'https://host1.example.com/url') self.assertEqual(env.root.host2.as_url, '/url') self.host2_called = True return Response() app = web.subdomain('example.com') | web.cases ( web.subdomain('host1') | web.match('/url', 'host1') | host1, web.subdomain('host2') | web.match('/url', 'host2') | host2, ) assert web.ask(app, 'http://host1.example.com/url') assert web.ask(app, 'https://host2.example.com/url') assert self.host1_called and self.host2_called
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_(web.locations(chain)[k][0].subdomains) self.assertEqual(web.locations(chain)[k][0].subdomains, ["news"])
def test_url_building_errors(self): 'UrlBuildingError' app = web.namespace('news') | web.cases( web.match('/', 'index'), web.match('/<int:id>', 'item'), web.match('/', 'anchor', fragment='<int:id>'), ) r = web.Reverse.from_handler(app) self.assertRaises(UrlBuildingError, r.build_url, '') self.assertRaises(UrlBuildingError, lambda: r.as_url) self.assertRaises(UrlBuildingError, r.build_url, 'main') self.assertRaises(UrlBuildingError, lambda: r.main) self.assertRaises(UrlBuildingError, r.build_url, 'news.item') self.assertRaises(UrlBuildingError, lambda: r.news.item.as_url) self.assertRaises(UrlBuildingError, r.build_url, 'news.item', uid=3) self.assertRaises(UrlBuildingError, lambda: r.news.item(uid=3)) self.assertRaises(UrlBuildingError, r.build_url, 'news.item', id=3, uid=3) self.assertRaises(UrlBuildingError, r.build_url, 'news') self.assertRaises(UrlBuildingError, lambda: r.news.as_url) self.assertRaises(UrlBuildingError, r.build_url, 'news.hash') self.assertRaises(UrlBuildingError, lambda: r.news.anchor.as_url) self.assertRaises(UrlBuildingError, r.build_url, 'news.hash', uid=3) self.assertRaises(UrlBuildingError, lambda: r.news.anchor(uid=3).as_url) self.assertRaises(UrlBuildingError, r.build_url, 'news.hash', id=3, uid=3)
def test_chain_with_list(self): 'Chain with cases' @F def h(env, data, nh): self.assertEqual(env.count, 0) env.count = env.count + 1 return nh(env, data) @F def h1(env, data, nh): self.assertEqual(env.count, 1) env.count = env.count + 1 return nh(env, data) @F def h2(env, data, nh): self.assertEqual(env.count, 2) env.count = env.count + 1 return nh(env, data) chain = h | web.cases(h1, h1 | h2) env = VS(count=0) self.assert_(chain(env, VS()) is None) self.assertEqual(env.count, 1)
def test_prefix_leaf(self): '''Simple prefix''' def handler(env, data): self.assertEqual(env._route_state.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_namespace_and_cases(self): 'Locations namespace with web.cases' chain = web.namespace('news') | web.cases( web.match('/', 'index'), web.match('/docs', 'docs')) self.assertEqual(location_keys(chain), ['news']) self.assertEqual(set(locations(chain)['news'][1].keys()), {'index', 'docs'})
def test_prefix_state(self): '''Prefix state correctness''' def handler(env, data): return Response() app = web.cases( web.match('/', 'index'), web.prefix('/docs') | web.namespace('doc') | web.cases( web.match('/item', '') | handler, web.prefix('/list') | web.cases(web.match('/item', 'list') | handler), web.match('/other-thing', 'something') | handler), web.match('/something', 'something') | handler) self.assertEqual(web.ask(app, '/docs/something'), None) self.assertEqual(web.ask(app, '/docs/list/something'), None) self.assertEqual(web.ask(app, '/docs/list/other-thing'), None)
def app(self): # Be careful, put prepare handler only after match! Otherwise it brakes # file upload (yes, its not obvious!) prepare = self.PrepareItemHandler(self) return self.app_prefix | web.cases( web.match('', '') | prepare | self, web.match('/autosave', 'autosave') | \ web.method('POST', strict=True) | prepare | self.autosave )
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: 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.as_url, encoded) self.assertEqual( web.Reverse.from_handler(app).percent.as_url.get_readable(), u'/հայերեն/%25') 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 app(self): return web.cases( web.match('/doc-link-block/<int:id>', 'doc_link_block') | self.doc_link_item, web.match('/files-block/<int:id>', 'files_block') | self.files_item, web.match('/media-link-block/<int:id>', 'media_block') | self.media_item, )
def app(self): prefix = web.prefix('/<noneint:item>/' + self.action + '/<field_name>', name=self.action, convs={'noneint': NoneIntConv}) prepare = PrepareItemHandler(self) return prefix | web.cases( web.match() | prepare | self, web.match('/crop', 'crop') | prepare | self.crop, )
def setUp(self): auth = self.auth = CookieAuth(get_user_identity, identify_user) def anonymouse(env, data): self.assertTrue(hasattr(env, 'user')) self.assertEqual(env.user, None) return web.Response('ok') def no_anonymouse(env, data): self.assertTrue(hasattr(env, 'user')) self.assertEqual(env.user, MockUser(name='user name')) return web.Response('ok') self.app = web.cases( auth.login(), auth.logout(redirect_to=None), auth | web.cases( web.match('/a', 'a') | anonymouse, web.match('/b', 'b') | auth_required | no_anonymouse, ), )
def app(self): def exc(env, data): raise HTTPMethodNotAllowed return web.cases( web.match('/', 'index') | (lambda e,d: Response(body='index')), web.match('/500', 'err500') | (lambda e,d: 1+''), web.match('/403', 'err403') | exc, web.match('/broken_response', 'broken_response') | \ (lambda e,d: 0), )
def create_handler(app): h_caching = Caching(app.cache, duration=app.cfg.CACHE_TIME) h_sections_ru = app.sections['ru'].handler() h_sections_en = app.sections['en'].handler() return cases( static_files(app.cfg.STATIC_DIR, app.cfg.STATIC_URL), static_files(app.cfg.FRONT_MEDIA_DIR, app.cfg.MEDIA_URL), h_caching | cache(enable=app.cfg.CACHE_ENABLED) | cases( prefix('/en') | app.i18n.handler('en') | cases( match('/', name='index') | h_index, h_sections_en, ), app.i18n.handler('ru') | cases( match('/', name='index') | h_index, h_sections_ru, ), ), HTTPNotFound, )
def test_repr(self): # for coverage '%r' % web.WebHandler() '%r' % web.cases() '%r' % web.request_filter(lambda e, d, n: None) '%r' % web.match('/', 'index') '%r' % web.method('GET') '%r' % web.static_files('/prefix') '%r' % web.prefix('/prefix') '%r' % web.subdomain('name') '%r' % web.namespace('name')
def test_int_converter(self): '''Check int converter''' def handler(env, data): 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_prefix_root(self): '''Prefix root''' def handler(env, data): self.assertEqual(env._route_state.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 h_section(self, section=None): parent_id = section and section.id or None subsections = self.get_sections(parent_id=parent_id) handlers = [] for subsection in subsections: resource = self.resources.get_resource(subsection.type) handler = resource.handler(self, subsection) handler = prefix('/' + (subsection.slug or ''), name=subsection.slug) | handler handlers.append(handler) return cases(*handlers)
def app(self): if isinstance(self.stream, Loner): prefix = web.prefix('/log', name=self.action) else: prefix = web.prefix('/<idconv:item>/log', name=self.action, convs={'idconv': self.stream.id_converter}) return prefix | self.PrepareItemHandler(self) | web.cases( web.match('') | self, web.match('/<int:log_id>', 'entry') | self.log_entry, )
def test_not_found(self): '''Check int converter with handler which accepts params''' def handler(env, data): 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)