本文整理汇总了Python中pecan.hooks.PecanHook方法的典型用法代码示例。如果您正苦于以下问题:Python hooks.PecanHook方法的具体用法?Python hooks.PecanHook怎么用?Python hooks.PecanHook使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类pecan.hooks
的用法示例。
在下文中一共展示了hooks.PecanHook方法的5个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: test_basic_single_hook
# 需要导入模块: from pecan import hooks [as 别名]
# 或者: from pecan.hooks import PecanHook [as 别名]
def test_basic_single_hook(self):
run_hook = []
class RootController(object):
@expose()
def index(self, req, resp):
run_hook.append('inside')
return 'Hello, World!'
class SimpleHook(PecanHook):
def on_route(self, state):
run_hook.append('on_route')
def before(self, state):
run_hook.append('before')
def after(self, state):
run_hook.append('after')
def on_error(self, state, e):
run_hook.append('error')
app = TestApp(Pecan(
RootController(),
hooks=[SimpleHook()],
use_context_locals=False
))
response = app.get('/')
assert response.status_int == 200
assert response.body == b_('Hello, World!')
assert len(run_hook) == 4
assert run_hook[0] == 'on_route'
assert run_hook[1] == 'before'
assert run_hook[2] == 'inside'
assert run_hook[3] == 'after'
示例2: test_on_error_response_hook
# 需要导入模块: from pecan import hooks [as 别名]
# 或者: from pecan.hooks import PecanHook [as 别名]
def test_on_error_response_hook(self):
run_hook = []
class RootController(object):
@expose()
def causeerror(self, req, resp):
return [][1]
class ErrorHook(PecanHook):
def on_error(self, state, e):
run_hook.append('error')
r = webob.Response()
r.text = u_('on_error')
return r
app = TestApp(Pecan(RootController(), hooks=[
ErrorHook()
], use_context_locals=False))
response = app.get('/causeerror')
assert len(run_hook) == 1
assert run_hook[0] == 'error'
assert response.text == 'on_error'
示例3: test_basic_multi_hook
# 需要导入模块: from pecan import hooks [as 别名]
# 或者: from pecan.hooks import PecanHook [as 别名]
def test_basic_multi_hook(self):
run_hook = []
class RootController(object):
@expose()
def index(self, req, resp):
run_hook.append('inside')
return 'Hello, World!'
class SimpleHook(PecanHook):
def __init__(self, id):
self.id = str(id)
def on_route(self, state):
run_hook.append('on_route' + self.id)
def before(self, state):
run_hook.append('before' + self.id)
def after(self, state):
run_hook.append('after' + self.id)
def on_error(self, state, e):
run_hook.append('error' + self.id)
app = TestApp(Pecan(RootController(), hooks=[
SimpleHook(1), SimpleHook(2), SimpleHook(3)
], use_context_locals=False))
response = app.get('/')
assert response.status_int == 200
assert response.body == b_('Hello, World!')
assert len(run_hook) == 10
assert run_hook[0] == 'on_route1'
assert run_hook[1] == 'on_route2'
assert run_hook[2] == 'on_route3'
assert run_hook[3] == 'before1'
assert run_hook[4] == 'before2'
assert run_hook[5] == 'before3'
assert run_hook[6] == 'inside'
assert run_hook[7] == 'after3'
assert run_hook[8] == 'after2'
assert run_hook[9] == 'after1'
示例4: test_partial_hooks
# 需要导入模块: from pecan import hooks [as 别名]
# 或者: from pecan.hooks import PecanHook [as 别名]
def test_partial_hooks(self):
run_hook = []
class RootController(object):
@expose()
def index(self, req, resp):
run_hook.append('inside')
return 'Hello World!'
@expose()
def causeerror(self, req, resp):
return [][1]
class ErrorHook(PecanHook):
def on_error(self, state, e):
run_hook.append('error')
class OnRouteHook(PecanHook):
def on_route(self, state):
run_hook.append('on_route')
app = TestApp(Pecan(RootController(), hooks=[
ErrorHook(), OnRouteHook()
], use_context_locals=False))
response = app.get('/')
assert response.status_int == 200
assert response.body == b_('Hello World!')
assert len(run_hook) == 2
assert run_hook[0] == 'on_route'
assert run_hook[1] == 'inside'
run_hook = []
try:
response = app.get('/causeerror')
except Exception as e:
assert isinstance(e, IndexError)
assert len(run_hook) == 2
assert run_hook[0] == 'on_route'
assert run_hook[1] == 'error'
示例5: test_prioritized_hooks
# 需要导入模块: from pecan import hooks [as 别名]
# 或者: from pecan.hooks import PecanHook [as 别名]
def test_prioritized_hooks(self):
run_hook = []
class RootController(object):
@expose()
def index(self, req, resp):
run_hook.append('inside')
return 'Hello, World!'
class SimpleHook(PecanHook):
def __init__(self, id, priority=None):
self.id = str(id)
if priority:
self.priority = priority
def on_route(self, state):
run_hook.append('on_route' + self.id)
def before(self, state):
run_hook.append('before' + self.id)
def after(self, state):
run_hook.append('after' + self.id)
def on_error(self, state, e):
run_hook.append('error' + self.id)
papp = Pecan(RootController(), hooks=[
SimpleHook(1, 3), SimpleHook(2, 2), SimpleHook(3, 1)
], use_context_locals=False)
app = TestApp(papp)
response = app.get('/')
assert response.status_int == 200
assert response.body == b_('Hello, World!')
assert len(run_hook) == 10
assert run_hook[0] == 'on_route3'
assert run_hook[1] == 'on_route2'
assert run_hook[2] == 'on_route1'
assert run_hook[3] == 'before3'
assert run_hook[4] == 'before2'
assert run_hook[5] == 'before1'
assert run_hook[6] == 'inside'
assert run_hook[7] == 'after1'
assert run_hook[8] == 'after2'
assert run_hook[9] == 'after3'