def test_handler(self): rules = [ HandlerPrefix('resources.alternative_routing.', [ Rule('/', name='home', handler='HomeHandler'), Rule('/foo', name='home/foo', handler='HomeHandler:foo'), Rule('/bar', name='home/bar', handler='HomeHandler:bar'), Rule('/other/foo', name='other/foo', handler='OtherHandler:foo'), Rule('/other/bar', name='other/bar', handler='OtherHandler:bar'), ]) ] app = Tipfy(rules) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'home-get') response = client.get('/foo') self.assertEqual(response.data, 'home-foo') response = client.get('/bar') self.assertEqual(response.data, 'home-bar') response = client.get('/other/foo') self.assertEqual(response.data, 'other-foo') response = client.get('/other/bar') self.assertEqual(response.data, 'other-bar')
def test_name_prefix(self): class DummyHandler(RequestHandler): def get(self, **kwargs): return '' rules = [ NamePrefix('company-', [ Rule('/', name='home', handler=DummyHandler), Rule('/about', name='about', handler=DummyHandler), Rule('/contact', name='contact', handler=DummyHandler), ]), ] app = Tipfy(rules) with app.get_test_handler('/') as handler: self.assertEqual(handler.url_for('company-home'), '/') self.assertEqual(handler.url_for('company-about'), '/about') self.assertEqual(handler.url_for('company-contact'), '/contact') with app.get_test_handler('/') as handler: self.assertEqual(handler.request.rule.name, 'company-home') with app.get_test_handler('/about') as handler: self.assertEqual(handler.request.rule.name, 'company-about') with app.get_test_handler('/contact') as handler: self.assertEqual(handler.request.rule.name, 'company-contact')
def test_add(self): app = Tipfy() router = Router(app) self.assertEqual(len(list(router.map.iter_rules())), 0) router.add(Rule('/', name='home', handler='HomeHandler')) self.assertEqual(len(list(router.map.iter_rules())), 1) router.add([ Rule('/about', name='about', handler='AboutHandler'), Rule('/contact', name='contact', handler='ContactHandler'), ]) self.assertEqual(len(list(router.map.iter_rules())), 3)
def test_set_get_flashes(self): class MyHandler(BaseHandler): def get(self): res = [msg for msg, level in self.session.get_flashes()] if not res: res = [{'body': 'undefined'}] self.session.flash({'body': 'a flash value'}) return Response(res[0]['body']) rules = [Rule('/', name='test', handler=MyHandler)] app = self._get_app('/') app.router.add(rules) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'undefined') response = client.get('/', headers={ 'Cookie': '\n'.join( response.headers.getlist('Set-Cookie')), }) self.assertEqual(response.data, 'a flash value')
def test_set_get_secure_cookie(self): class MyHandler(BaseHandler): def get(self): response = Response() cookie = self.session_store.get_secure_cookie('test') or {} res = cookie.get('test') if not res: res = 'undefined' self.session_store.set_secure_cookie( response, 'test', {'test': 'a secure cookie value'}) response.data = res return response rules = [Rule('/', name='test', handler=MyHandler)] app = self._get_app('/') app.router.add(rules) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'undefined') response = client.get('/', headers={ 'Cookie': '\n'.join( response.headers.getlist('Set-Cookie')), }) self.assertEqual(response.data, 'a secure cookie value')
def test_set_unset_cookie(self): class MyHandler(BaseHandler): def get(self): res = self.request.cookies.get('test') if not res: res = 'undefined' self.session_store.set_cookie('test', 'a cookie value') self.session_store.unset_cookie('test') return Response(res) rules = [Rule('/', name='test', handler=MyHandler)] app = self._get_app('/') app.router.add(rules) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'undefined') response = client.get('/', headers={ 'Cookie': '\n'.join( response.headers.getlist('Set-Cookie')), }) self.assertEqual(response.data, 'undefined')
def test_get_datastore_session(self): class MyHandler(BaseHandler): def get(self): session = self.session_store.get_session(backend='datastore') res = session.get('test') if not res: res = 'undefined' session['test'] = 'a datastore session value' return Response(res) rules = [Rule('/', name='test', handler=MyHandler)] app = self._get_app('/') app.router.add(rules) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'undefined') response = client.get('/', headers={ 'Cookie': '\n'.join( response.headers.getlist('Set-Cookie')), }) self.assertEqual(response.data, 'a datastore session value')
def test_set_session(self): class MyHandler(BaseHandler): def get(self): res = self.session.get('key') if not res: res = 'undefined' session = SecureCookieSession() session['key'] = 'a session value' self.session_store.set_session( self.session_store.config['cookie_name'], session) return Response(res) rules = [Rule('/', name='test', handler=MyHandler)] app = self._get_app('/') app.router.add(rules) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'undefined') response = client.get('/', headers={ 'Cookie': '\n'.join( response.headers.getlist('Set-Cookie')), }) self.assertEqual(response.data, 'a session value')
def test_middleware_multiple_changes(self): class MyHandler(RequestHandler): middleware = [SessionMiddleware(), i18n.I18nMiddleware()] def get(self, **kwargs): locale = self.i18n.locale return Response(locale) app = App(rules=[Rule('/', name='home', handler=MyHandler)], config={ 'tipfy.sessions': { 'secret_key': 'secret', }, 'tipfy.i18n': { 'locale_request_lookup': [('args', 'lang'), ('session', '_locale')], } }) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'en_US') response = client.get('/?lang=pt_BR') self.assertEqual(response.data, 'pt_BR') response = client.get('/') self.assertEqual(response.data, 'pt_BR') response = client.get('/?lang=en_US') self.assertEqual(response.data, 'en_US') response = client.get('/') self.assertEqual(response.data, 'en_US')
def test_function_handler(self): rules = [ HandlerPrefix('resources.alternative_routing.', [ Rule('/', name='home', handler='home'), Rule('/foo', name='home/foo', handler='foo'), Rule('/bar', name='home/bar', handler='bar'), ]) ] app = Tipfy(rules) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'home') response = client.get('/foo') self.assertEqual(response.data, 'foo') response = client.get('/bar') self.assertEqual(response.data, 'bar')
def get_app(self): return App(rules=[Rule('/', name='home', handler=RequestHandler)], config={ 'tipfy.sessions': { 'secret_key': 'secret', }, 'tipfy.i18n': { 'timezone': 'UTC' }, })
def test_handler_prefix(self): rules = [ HandlerPrefix('resources.handlers.', [ Rule('/', name='home', handler='HomeHandler'), Rule('/defaults', name='defaults', handler='HandlerWithRuleDefaults', defaults={'foo': 'bar'}), ]) ] app = Tipfy(rules) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'Hello, World!') response = client.get('/defaults') self.assertEqual(response.data, 'bar')
def setUp(self): app = App(rules=[Rule('/', name='home', handler=RequestHandler)], config={ 'tipfy.sessions': { 'secret_key': 'secret', }, 'tipfy.i18n': { 'timezone': 'UTC' }, }) local.request = request = Request.from_values('/') request.app = app test_utils.BaseTestCase.setUp(self)
def test_regex_converter(self): class TestHandler(RequestHandler): def get(self, **kwargs): return Response(kwargs.get('path')) app = Tipfy([ Rule('/<regex(".*"):path>', name='home', handler=TestHandler), ]) client = app.get_test_client() response = client.get('/foo') self.assertEqual(response.data, 'foo') response = client.get('/foo/bar') self.assertEqual(response.data, 'foo/bar') response = client.get('/foo/bar/baz') self.assertEqual(response.data, 'foo/bar/baz')
# -*- coding: utf-8 -*- """URL definitions.""" from tipfy.routing import Rule rules = [ Rule('/', name='home', handler='views.handlers.HomeHandler'), Rule('/news/<string:name>', name='noticias', handler='views.handlers.NoticiasHandler'), Rule('/hello', name='hello-world', handler='hello_world.handlers.HelloWorldHandler'), Rule('/pretty', name='hello-world-pretty', handler='hello_world.handlers.PrettyHelloWorldHandler'), ]
def test_set_get_messages(self): class MyHandler(BaseHandler): @cached_property def messages(self): """A list of status messages to be displayed to the user.""" messages = [] flashes = self.session.get_flashes(key='_messages') for msg, level in flashes: msg['level'] = level messages.append(msg) return messages def set_message(self, level, body, title=None, life=None, flash=False): """Adds a status message. :param level: Message level. Common values are "success", "error", "info" or "alert". :param body: Message contents. :param title: Optional message title. :param life: Message life time in seconds. User interface can implement a mechanism to make the message disappear after the elapsed time. If not set, the message is permanent. :returns: None. """ message = {'title': title, 'body': body, 'life': life} if flash is True: self.session.flash(message, level, '_messages') else: self.messages.append(message) def get(self): self.set_message('success', 'a normal message value') self.set_message('success', 'a flash message value', flash=True) return Response('|'.join(msg['body'] for msg in self.messages)) rules = [Rule('/', name='test', handler=MyHandler)] app = self._get_app('/') app.router.add(rules) client = app.get_test_client() response = client.get('/') self.assertEqual(response.data, 'a normal message value') response = client.get('/', headers={ 'Cookie': '\n'.join( response.headers.getlist('Set-Cookie')), }) self.assertEqual(response.data, 'a flash message value|a normal message value')
# -*- coding: utf-8 -*- """URL definitions.""" from tipfy.routing import Rule rules = [ Rule('/', name='hello-world', handler='hello_world.handlers.HelloWorldHandler'), Rule('/pretty', name='hello-world-pretty', handler='hello_world.handlers.PrettyHelloWorldHandler'), Rule('/auth', name='auth', handler='multi_auth.handlers.AuthHandler'), Rule('/auth/logout', name='auth-logout', handler='multi_auth.handlers.LogoutHandler'), Rule('/auth/google', name='auth-google', handler='multi_auth.handlers.GoogleAuthHandler'), Rule('/auth/twitter/', name='auth-twitter', handler='multi_auth.handlers.TwitterAuthHandler'), Rule('/checkins/add', name='checkins-add', handler='checkins.handlers.AddHandler'), ]
# -*- coding: utf-8 -*- """URL definitions.""" from tipfy.routing import Rule rules = [ Rule('/', name='hello-world', handler='hello_world.handlers.HelloWorldHandler'), Rule('/pretty', name='hello-world-pretty', handler='hello_world.handlers.PrettyHelloWorldHandler'), ]
# -*- coding: utf-8 -*- """URL definitions.""" from tipfy.routing import Rule rules = [ Rule('/', name='hello-world', handler='dev_time_sample.handlers.HelloWorldHandler'), Rule('/pretty', name='hello-world-pretty', handler='dev_time_sample.handlers.PrettyHelloWorldHandler'), Rule('/add', name='add-sample-data', handler='dev_time_sample.handlers.AddSampleDataHandler'), Rule('/view', name='view-sample-data', handler='dev_time_sample.handlers.ViewSampleDataHandler'), ]
# -*- coding: utf-8 -*- """ URL definitions. """ from tipfy.routing import Rule from tipfy.routing import HandlerPrefix rules = [ HandlerPrefix('agendatrends.handlers.', [ Rule('/', name='landing', handler='main.LandingHandler'), Rule('/topics', name='topics', handler='site.topics.Landing'), Rule('/people', name='people', handler='site.people.Landing'), Rule('/trends', name='trends', handler='site.trends.Landing'), Rule('/sources', name='sources', handler='site.sources.Landing'), Rule('/topic/<path:filters>', name='topic', handler='main.TopicHandler'), Rule('/api/topic/<path:filters>', name='api', handler='api.ApiTopicHandler'), Rule('/api/legislators', name='api', handler='api.ApiLegislatorHandler'), Rule('/dev/data', name='dev-data', handler='dev.DefaultData'), Rule('/dev/fill', name='fill-data', handler='dev.FillDatastore'), Rule('/api/rep_mentions', name='api', handler='api.ApiRepMentionsHandler'), Rule('/dev/data', name='dev-data', handler='dev.DefaultData') ]) ]
# -*- coding: utf-8 -*- """URL definitions.""" from tipfy.routing import Rule rules = [ Rule('/', name='home', handler='home.HomeHandler'), Rule('/applicant/home', handler='applicant.HomeHandler'), Rule('/case/create', handler='applicant.CreateCaseHandler'), Rule('/case/details/<int:id>', handler='applicant.CaseDetailsHandler'), Rule('/case/emails/<int:id>', handler='applicant.CaseEmailsHandler'), Rule('/case/emailschange/<int:id>', handler='applicant.EmailsChangeHandler'), Rule('/case/submit/<int:id>', handler='applicant.CaseSubmitHandler'), Rule('/document/serve/<string:key>/<string:filename>', handler='document.DownloadHandler'), Rule('/document/upload/<int:id>', handler='document.UploadHandler'), Rule('/approver/home', handler='approver.HomeHandler'), Rule('/approver/case/<int:id>', handler='approver.CaseReviewHandler'), Rule('/case/approve/<int:id>', handler='approver.CaseApproveHandler'), Rule('/case/comment/<int:id>', handler='approver.CaseCommentHandler'), ]
# -*- coding: utf-8 -*- """URL definitions.""" from tipfy.routing import Rule rules = [ Rule( '/', name='claslite', handler='claslite.handlers.ClasliteHandler' ), Rule( '/auth/login', name='auth/login', handler='claslite.handlers.LoginHandler'), Rule( '/auth/logout', name='auth/logout', handler='claslite.handlers.LogoutHandler'), Rule( '/auth/signup', name='auth/signup', handler='claslite.handlers.SignupHandler'), Rule( '/ee/<path:path>', name='proxy', handler='proxy.handlers.ProxyHandler' ), Rule( '/fusionkml/<int:tableId>/<int:rowId>', name='fusionkml', handler='fusionkml.handlers.FusionKmlHandler' ), Rule( '/rpc', name='rpc', handler='rpc.handlers.JsonHandler'), Rule( '/shape/upload', name='shape', handler='shape.handlers.ShapeUploadHandler'), ]
# -*- coding: utf-8 -*- """URL definitions.""" from tipfy.routing import Rule rules = [ Rule('/', name='home', handler='home.HomeHandler'), Rule('/action', handler='action.AddActionHandler'), ]