本文整理汇总了Python中tipfy.Tipfy类的典型用法代码示例。如果您正苦于以下问题:Python Tipfy类的具体用法?Python Tipfy怎么用?Python Tipfy使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Tipfy类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_middleware_multiple_changes
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 = Tipfy(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')
示例2: test_redirect_relative_uris
def test_redirect_relative_uris(self):
class MyHandler(RequestHandler):
def get(self):
return self.redirect(self.request.args.get('redirect'))
app = Tipfy(rules=[
Rule('/foo/bar', name='test1', handler=MyHandler),
Rule('/foo/bar/', name='test2', handler=MyHandler),
])
client = app.get_test_client()
response = client.get('/foo/bar/', query_string={'redirect': '/baz'})
self.assertEqual(response.headers['Location'], 'http://localhost/baz')
response = client.get('/foo/bar/', query_string={'redirect': './baz'})
self.assertEqual(response.headers['Location'], 'http://localhost/foo/bar/baz')
response = client.get('/foo/bar/', query_string={'redirect': '../baz'})
self.assertEqual(response.headers['Location'], 'http://localhost/foo/baz')
response = client.get('/foo/bar', query_string={'redirect': '/baz'})
self.assertEqual(response.headers['Location'], 'http://localhost/baz')
response = client.get('/foo/bar', query_string={'redirect': './baz'})
self.assertEqual(response.headers['Location'], 'http://localhost/foo/baz')
response = client.get('/foo/bar', query_string={'redirect': '../baz'})
self.assertEqual(response.headers['Location'], 'http://localhost/baz')
示例3: test_handle_exception_2
def test_handle_exception_2(self):
res = 'I fixed it!'
class MyMiddleware(object):
def handle_exception(self, handler, exception):
raise ValueError()
class MyHandler(RequestHandler):
middleware = [MyMiddleware()]
def get(self, **kwargs):
raise ValueError()
class ErrorHandler(RequestHandler):
def handle_exception(self, exception):
return Response(res)
app = Tipfy(rules=[
Rule('/', name='home', handler=MyHandler),
], debug=False)
app.error_handlers[500] = ErrorHandler
client = app.get_test_client()
response = client.get('/')
self.assertEqual(response.data, res)
示例4: test_make_response
def test_make_response(self):
app = Tipfy()
request = Request.from_values()
# Empty.
response = app.make_response(request)
self.assertEqual(isinstance(response, app.response_class), True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, '')
# From Response.
response = app.make_response(request, Response('Hello, World!'))
self.assertEqual(isinstance(response, app.response_class), True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, 'Hello, World!')
# From string.
response = app.make_response(request, 'Hello, World!')
self.assertEqual(isinstance(response, app.response_class), True)
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, 'Hello, World!')
# From tuple.
response = app.make_response(request, 'Hello, World!', 404)
self.assertEqual(isinstance(response, app.response_class), True)
self.assertEqual(response.status_code, 404)
self.assertEqual(response.data, 'Hello, World!')
# From None.
self.assertRaises(ValueError, app.make_response, request, None)
示例5: test_custom_error_handlers
def test_custom_error_handlers(self):
class HomeHandler(RequestHandler):
def get(self):
return Response('')
app = Tipfy([
Rule('/', handler=HomeHandler, name='home'),
Rule('/broken', handler=BrokenHandler, name='broken'),
], debug=False)
app.error_handlers = {
404: Handle404,
405: Handle405,
500: Handle500,
}
client = app.get_test_client()
res = client.get('/nowhere')
self.assertEqual(res.status_code, 404)
self.assertEqual(res.data, '404 custom handler')
res = client.put('/')
self.assertEqual(res.status_code, 405)
self.assertEqual(res.data, '405 custom handler')
self.assertEqual(res.headers.get('Allow'), 'GET')
res = client.get('/broken')
self.assertEqual(res.status_code, 500)
self.assertEqual(res.data, '500 custom handler')
示例6: test_name_prefix
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')
示例7: test_abort
def test_abort(self):
class HandlerWith400(RequestHandler):
def get(self, **kwargs):
self.abort(400)
class HandlerWith403(RequestHandler):
def post(self, **kwargs):
self.abort(403)
class HandlerWith404(RequestHandler):
def put(self, **kwargs):
self.abort(404)
app = Tipfy(rules=[
Rule('/400', endpoint='400', handler=HandlerWith400),
Rule('/403', endpoint='403', handler=HandlerWith403),
Rule('/404', endpoint='404', handler=HandlerWith404),
], debug=True)
client = app.get_test_client()
response = client.get('/400')
self.assertEqual(response.status_code, 400)
response = client.post('/403')
self.assertEqual(response.status_code, 403)
response = client.put('/404')
self.assertEqual(response.status_code, 404)
示例8: test_get_config
def test_get_config(self):
app = Tipfy(rules=[
Rule('/', name='home', handler=AllMethodsHandler),
], config = {
'foo': {
'bar': 'baz',
}
})
with app.get_test_handler('/') as handler:
self.assertEqual(handler.get_config('foo', 'bar'), 'baz')
示例9: test_app_get_config
def test_app_get_config(self):
app = Tipfy()
self.assertEqual(app.get_config('resources.i18n', 'locale'), 'en_US')
self.assertEqual(app.get_config('resources.i18n', 'locale', 'foo'), 'en_US')
self.assertEqual(app.get_config('resources.i18n'), {
'locale': 'en_US',
'timezone': 'America/Chicago',
'required': REQUIRED_VALUE,
})
示例10: test_request_handler_get_config
def test_request_handler_get_config(self):
app = Tipfy()
with app.get_test_context() as request:
handler = RequestHandler(request)
self.assertEqual(handler.get_config('resources.i18n', 'locale'), 'en_US')
self.assertEqual(handler.get_config('resources.i18n', 'locale', 'foo'), 'en_US')
self.assertEqual(handler.get_config('resources.i18n'), {
'locale': 'en_US',
'timezone': 'America/Chicago',
'required': REQUIRED_VALUE,
})
示例11: compile_templates
def compile_templates(argv=None):
"""Compiles templates for better performance. This is a command line
script. From the buildout directory, run:
bin/jinja2_compile
It will compile templates from the directory configured for 'templates_dir'
to the one configured for 'templates_compiled_target'.
At this time it doesn't accept any arguments.
"""
if argv is None:
argv = sys.argv
set_gae_sys_path()
from config import config
app = Tipfy(config=config)
template_path = app.get_config('tipfyext.jinja2', 'templates_dir')
compiled_path = app.get_config('tipfyext.jinja2',
'templates_compiled_target')
if compiled_path is None:
raise ValueError('Missing configuration key to compile templates.')
if isinstance(template_path, basestring):
# A single path.
source = os.path.join(app_path, template_path)
else:
# A list of paths.
source = [os.path.join(app_path, p) for p in template_path]
target = os.path.join(app_path, compiled_path)
# Set templates dir and deactivate compiled dir to use normal loader to
# find the templates to be compiled.
app.config['tipfyext.jinja2']['templates_dir'] = source
app.config['tipfyext.jinja2']['templates_compiled_target'] = None
if target.endswith('.zip'):
zip_cfg = 'deflated'
else:
zip_cfg = None
old_list_templates = FileSystemLoader.list_templates
FileSystemLoader.list_templates = list_templates
env = Jinja2.factory(app, 'jinja2').environment
env.compile_templates(target, extensions=None,
filter_func=filter_templates, zip=zip_cfg, log_function=logger,
ignore_errors=False, py_compile=False)
FileSystemLoader.list_templates = old_list_templates
示例12: test_501
def test_501(self):
"""Method is not in app.allowed_methods."""
app = Tipfy()
client = app.get_test_client()
# Normal mode.
response = client.open('/', method='CONNECT')
self.assertEqual(response.status_code, 501)
# Debug mode.
app.debug = True
response = client.open('/', method='CONNECT')
self.assertEqual(response.status_code, 501)
示例13: test_500
def test_500(self):
"""Handler import will fail."""
app = Tipfy(rules=[Rule('/', name='home', handler='non.existent.handler')])
client = app.get_test_client()
# Normal mode.
response = client.get('/')
self.assertEqual(response.status_code, 500)
# Debug mode.
app.debug = True
app.config['tipfy']['enable_debugger'] = False
self.assertRaises(ImportError, client.get, '/')
示例14: test_redirect_to_empty
def test_redirect_to_empty(self):
class HandlerWithRedirectTo(RequestHandler):
def get(self, **kwargs):
return self.redirect_to('home', _empty=True)
app = Tipfy(rules=[
Rule('/', name='home', handler=AllMethodsHandler),
Rule('/redirect-me', name='redirect', handler=HandlerWithRedirectTo),
])
client = app.get_test_client()
response = client.get('/redirect-me', follow_redirects=False)
self.assertEqual(response.data, '')
示例15: test_405_debug
def test_405_debug(self):
class HomeHandler(RequestHandler):
pass
app = Tipfy(rules=[
Rule('/', endpoint='home', handler=HomeHandler),
], debug=True)
client = app.get_test_client()
for method in ALLOWED_METHODS:
response = client.open('/', method=method)
self.assertEqual(response.status_code, 405)