本文整理汇总了Python中tipfy.Tipfy类的典型用法代码示例。如果您正苦于以下问题:Python Tipfy类的具体用法?Python Tipfy怎么用?Python Tipfy使用的例子?那么恭喜您, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了Tipfy类的20个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于我们的系统推荐出更棒的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')
开发者ID:ksachdeva,项目名称:tipfy,代码行数:34,代码来源:test_i18n.py
示例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')
开发者ID:adilhz,项目名称:tipfy,代码行数:28,代码来源:app_test.py
示例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)
开发者ID:adilhz,项目名称:tipfy,代码行数:25,代码来源:app_test.py
示例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)
开发者ID:adilhz,项目名称:tipfy,代码行数:30,代码来源:app_test.py
示例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')
开发者ID:strogo,项目名称:tipfy,代码行数:28,代码来源:test_application.py
示例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')
开发者ID:aswadrangnekar,项目名称:tipfy,代码行数:28,代码来源:test_routing.py
示例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)
开发者ID:strogo,项目名称:tipfy,代码行数:29,代码来源:test_handler.py
示例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')
开发者ID:adilhz,项目名称:tipfy,代码行数:10,代码来源:app_test.py
示例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,
})
开发者ID:strogo,项目名称:tipfy,代码行数:10,代码来源:test_config.py
示例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,
})
开发者ID:aswadrangnekar,项目名称:tipfy,代码行数:12,代码来源:test_config.py
示例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
开发者ID:peper,项目名称:tipfy,代码行数:53,代码来源:scripts.py
示例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)
开发者ID:adilhz,项目名称:tipfy,代码行数:13,代码来源:app_test.py
示例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, '/')
开发者ID:adilhz,项目名称:tipfy,代码行数:13,代码来源:app_test.py
示例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, '')
开发者ID:adilhz,项目名称:tipfy,代码行数:13,代码来源:app_test.py
示例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)
开发者ID:strogo,项目名称:tipfy,代码行数:13,代码来源:test_handler.py
示例16: test_json
def test_json(self):
class JsonHandler(RequestHandler):
def get(self, **kwargs):
return Response(self.request.json['foo'])
app = Tipfy(rules=[
Rule('/', name='home', handler=JsonHandler),
], debug=True)
data = json_encode({'foo': 'bar'})
client = app.get_test_client()
response = client.get('/', content_type='application/json', data=data)
self.assertEqual(response.data, 'bar')
开发者ID:adilhz,项目名称:tipfy,代码行数:13,代码来源:app_test.py
示例17: test_404
def test_404(self):
"""No URL rules defined."""
app = Tipfy()
client = app.get_test_client()
# Normal mode.
response = client.get('/')
self.assertEqual(response.status_code, 404)
# Debug mode.
app.debug = True
response = client.get('/')
self.assertEqual(response.status_code, 404)
开发者ID:adilhz,项目名称:tipfy,代码行数:13,代码来源:app_test.py
示例18: test_handle_exception
def test_handle_exception(self):
app = Tipfy([
Rule('/', handler=AllMethodsHandler, name='home'),
Rule('/broken', handler=BrokenHandler, name='broken'),
Rule('/broken-but-fixed', handler=BrokenButFixedHandler, name='broken-but-fixed'),
], debug=False)
client = app.get_test_client()
response = client.get('/broken')
self.assertEqual(response.status_code, 500)
response = client.get('/broken-but-fixed')
self.assertEqual(response.status_code, 200)
self.assertEqual(response.data, 'That was close!')
开发者ID:adilhz,项目名称:tipfy,代码行数:14,代码来源:app_test.py
示例19: test_dev_run
def test_dev_run(self):
self._set_dev_server_flag(True)
os.environ['APPLICATION_ID'] = 'my-app'
os.environ['SERVER_SOFTWARE'] = 'Development'
os.environ['SERVER_NAME'] = 'localhost'
os.environ['SERVER_PORT'] = '8080'
os.environ['REQUEST_METHOD'] = 'GET'
app = Tipfy(rules=[
Rule('/', name='home', handler=AllMethodsHandler),
], debug=True)
app.run()
self.assertEqual(sys.stdout.getvalue(), 'Status: 200 OK\r\nContent-Type: text/html; charset=utf-8\r\nContent-Length: 11\r\n\r\nMethod: GET')
开发者ID:peper,项目名称:tipfy,代码行数:15,代码来源:test_app.py
示例20: test_handler_prefix
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')
开发者ID:aswadrangnekar,项目名称:tipfy,代码行数:16,代码来源:test_routing.py
注:本文中的tipfy.Tipfy类示例由纯净天空整理自Github/MSDocs等源码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。 |
请发表评论