Debian Codegraph @

pecan/tests/test_hooks.py

raw
1 import inspect
2 import operator
3 from io import StringIO
4
5 from webtest import TestApp
6
7 from pecan import make_app, expose, redirect, abort, rest, Request, Response
8 from pecan.hooks import (
9 PecanHook, TransactionHook, HookController, RequestViewerHook
10 )
11 from pecan.configuration import Config
12 from pecan.decorators import transactional, after_commit, after_rollback
13 from pecan.tests import PecanTestCase
14
15 # The `inspect.Arguments` namedtuple is different between PY2/3
16 kwargs = operator.attrgetter('varkw')
17
18
19 class TestHooks(PecanTestCase):
20
21 def test_basic_single_hook(self):
22 run_hook = []
23
24 class RootController(object):
25 @expose()
26 def index(self):
27 run_hook.append('inside')
28 return 'Hello, World!'
29
30 class SimpleHook(PecanHook):
31 def on_route(self, state):
32 run_hook.append('on_route')
33
34 def before(self, state):
35 run_hook.append('before')
36
37 def after(self, state):
38 run_hook.append('after')
39
40 def on_error(self, state, e):
41 run_hook.append('error')
42
43 app = TestApp(make_app(RootController(), hooks=[SimpleHook()]))
44 response = app.get('/')
45 assert response.status_int == 200
46 assert response.body == b'Hello, World!'
47
48 assert len(run_hook) == 4
49 assert run_hook[0] == 'on_route'
50 assert run_hook[1] == 'before'
51 assert run_hook[2] == 'inside'
52 assert run_hook[3] == 'after'
53
54 def test_basic_multi_hook(self):
55 run_hook = []
56
57 class RootController(object):
58 @expose()
59 def index(self):
60 run_hook.append('inside')
61 return 'Hello, World!'
62
63 class SimpleHook(PecanHook):
64 def __init__(self, id):
65 self.id = str(id)
66
67 def on_route(self, state):
68 run_hook.append('on_route' + self.id)
69
70 def before(self, state):
71 run_hook.append('before' + self.id)
72
73 def after(self, state):
74 run_hook.append('after' + self.id)
75
76 def on_error(self, state, e):
77 run_hook.append('error' + self.id)
78
79 app = TestApp(make_app(RootController(), hooks=[
80 SimpleHook(1), SimpleHook(2), SimpleHook(3)
81 ]))
82 response = app.get('/')
83 assert response.status_int == 200
84 assert response.body == b'Hello, World!'
85
86 assert len(run_hook) == 10
87 assert run_hook[0] == 'on_route1'
88 assert run_hook[1] == 'on_route2'
89 assert run_hook[2] == 'on_route3'
90 assert run_hook[3] == 'before1'
91 assert run_hook[4] == 'before2'
92 assert run_hook[5] == 'before3'
93 assert run_hook[6] == 'inside'
94 assert run_hook[7] == 'after3'
95 assert run_hook[8] == 'after2'
96 assert run_hook[9] == 'after1'
97
98 def test_partial_hooks(self):
99 run_hook = []
100
101 class RootController(object):
102 @expose()
103 def index(self):
104 run_hook.append('inside')
105 return 'Hello World!'
106
107 @expose()
108 def causeerror(self):
109 return [][1]
110
111 class ErrorHook(PecanHook):
112 def on_error(self, state, e):
113 run_hook.append('error')
114
115 class OnRouteHook(PecanHook):
116 def on_route(self, state):
117 run_hook.append('on_route')
118
119 app = TestApp(make_app(RootController(), hooks=[
120 ErrorHook(), OnRouteHook()
121 ]))
122
123 response = app.get('/')
124 assert response.status_int == 200
125 assert response.body == b'Hello World!'
126
127 assert len(run_hook) == 2
128 assert run_hook[0] == 'on_route'
129 assert run_hook[1] == 'inside'
130
131 run_hook = []
132 try:
133 response = app.get('/causeerror')
134 except Exception as e:
135 assert isinstance(e, IndexError)
136
137 assert len(run_hook) == 2
138 assert run_hook[0] == 'on_route'
139 assert run_hook[1] == 'error'
140
141 def test_on_error_response_hook(self):
142 run_hook = []
143
144 class RootController(object):
145 @expose()
146 def causeerror(self):
147 return [][1]
148
149 class ErrorHook(PecanHook):
150 def on_error(self, state, e):
151 run_hook.append('error')
152
153 r = Response()
154 r.text = 'on_error'
155
156 return r
157
158 app = TestApp(make_app(RootController(), hooks=[
159 ErrorHook()
160 ]))
161
162 response = app.get('/causeerror')
163
164 assert len(run_hook) == 1
165 assert run_hook[0] == 'error'
166 assert response.text == 'on_error'
167
168 def test_prioritized_hooks(self):
169 run_hook = []
170
171 class RootController(object):
172 @expose()
173 def index(self):
174 run_hook.append('inside')
175 return 'Hello, World!'
176
177 class SimpleHook(PecanHook):
178 def __init__(self, id, priority=None):
179 self.id = str(id)
180 if priority:
181 self.priority = priority
182
183 def on_route(self, state):
184 run_hook.append('on_route' + self.id)
185
186 def before(self, state):
187 run_hook.append('before' + self.id)
188
189 def after(self, state):
190 run_hook.append('after' + self.id)
191
192 def on_error(self, state, e):
193 run_hook.append('error' + self.id)
194
195 papp = make_app(RootController(), hooks=[
196 SimpleHook(1, 3), SimpleHook(2, 2), SimpleHook(3, 1)
197 ])
198 app = TestApp(papp)
199 response = app.get('/')
200 assert response.status_int == 200
201 assert response.body == b'Hello, World!'
202
203 assert len(run_hook) == 10
204 assert run_hook[0] == 'on_route3'
205 assert run_hook[1] == 'on_route2'
206 assert run_hook[2] == 'on_route1'
207 assert run_hook[3] == 'before3'
208 assert run_hook[4] == 'before2'
209 assert run_hook[5] == 'before1'
210 assert run_hook[6] == 'inside'
211 assert run_hook[7] == 'after1'
212 assert run_hook[8] == 'after2'
213 assert run_hook[9] == 'after3'
214
215 def test_basic_isolated_hook(self):
216 run_hook = []
217
218 class SimpleHook(PecanHook):
219 def on_route(self, state):
220 run_hook.append('on_route')
221
222 def before(self, state):
223 run_hook.append('before')
224
225 def after(self, state):
226 run_hook.append('after')
227
228 def on_error(self, state, e):
229 run_hook.append('error')
230
231 class SubSubController(object):
232 @expose()
233 def index(self):
234 run_hook.append('inside_sub_sub')
235 return 'Deep inside here!'
236
237 class SubController(HookController):
238 __hooks__ = [SimpleHook()]
239
240 @expose()
241 def index(self):
242 run_hook.append('inside_sub')
243 return 'Inside here!'
244
245 sub = SubSubController()
246
247 class RootController(object):
248 @expose()
249 def index(self):
250 run_hook.append('inside')
251 return 'Hello, World!'
252
253 sub = SubController()
254
255 app = TestApp(make_app(RootController()))
256 response = app.get('/')
257 assert response.status_int == 200
258 assert response.body == b'Hello, World!'
259
260 assert len(run_hook) == 1
261 assert run_hook[0] == 'inside'
262
263 run_hook = []
264
265 response = app.get('/sub/')
266 assert response.status_int == 200
267 assert response.body == b'Inside here!'
268
269 assert len(run_hook) == 3
270 assert run_hook[0] == 'before'
271 assert run_hook[1] == 'inside_sub'
272 assert run_hook[2] == 'after'
273
274 run_hook = []
275 response = app.get('/sub/sub/')
276 assert response.status_int == 200
277 assert response.body == b'Deep inside here!'
278
279 assert len(run_hook) == 3
280 assert run_hook[0] == 'before'
281 assert run_hook[1] == 'inside_sub_sub'
282 assert run_hook[2] == 'after'
283
284 def test_isolated_hook_with_global_hook(self):
285 run_hook = []
286
287 class SimpleHook(PecanHook):
288 def __init__(self, id):
289 self.id = str(id)
290
291 def on_route(self, state):
292 run_hook.append('on_route' + self.id)
293
294 def before(self, state):
295 run_hook.append('before' + self.id)
296
297 def after(self, state):
298 run_hook.append('after' + self.id)
299
300 def on_error(self, state, e):
301 run_hook.append('error' + self.id)
302
303 class SubController(HookController):
304 __hooks__ = [SimpleHook(2)]
305
306 @expose()
307 def index(self):
308 run_hook.append('inside_sub')
309 return 'Inside here!'
310
311 class RootController(object):
312 @expose()
313 def index(self):
314 run_hook.append('inside')
315 return 'Hello, World!'
316
317 sub = SubController()
318
319 app = TestApp(make_app(RootController(), hooks=[SimpleHook(1)]))
320 response = app.get('/')
321 assert response.status_int == 200
322 assert response.body == b'Hello, World!'
323
324 assert len(run_hook) == 4
325 assert run_hook[0] == 'on_route1'
326 assert run_hook[1] == 'before1'
327 assert run_hook[2] == 'inside'
328 assert run_hook[3] == 'after1'
329
330 run_hook = []
331
332 response = app.get('/sub/')
333 assert response.status_int == 200
334 assert response.body == b'Inside here!'
335
336 assert len(run_hook) == 6
337 assert run_hook[0] == 'on_route1'
338 assert run_hook[1] == 'before2'
339 assert run_hook[2] == 'before1'
340 assert run_hook[3] == 'inside_sub'
341 assert run_hook[4] == 'after1'
342 assert run_hook[5] == 'after2'
343
344 def test_mixin_hooks(self):
345 run_hook = []
346
347 class HelperHook(PecanHook):
348 priority = 2
349
350 def before(self, state):
351 run_hook.append('helper - before hook')
352
353 # we'll use the same hook instance to avoid duplicate calls
354 helper_hook = HelperHook()
355
356 class LastHook(PecanHook):
357 priority = 200
358
359 def before(self, state):
360 run_hook.append('last - before hook')
361
362 class SimpleHook(PecanHook):
363 priority = 1
364
365 def before(self, state):
366 run_hook.append('simple - before hook')
367
368 class HelperMixin(object):
369 __hooks__ = [helper_hook]
370
371 class LastMixin(object):
372 __hooks__ = [LastHook()]
373
374 class SubController(HookController, HelperMixin):
375 __hooks__ = [LastHook()]
376
377 @expose()
378 def index(self):
379 return "This is sub controller!"
380
381 class RootController(HookController, LastMixin):
382 __hooks__ = [SimpleHook(), helper_hook]
383
384 @expose()
385 def index(self):
386 run_hook.append('inside')
387 return 'Hello, World!'
388
389 sub = SubController()
390
391 papp = make_app(RootController())
392 app = TestApp(papp)
393 response = app.get('/')
394 assert response.status_int == 200
395 assert response.body == b'Hello, World!'
396
397 assert len(run_hook) == 4
398 assert run_hook[0] == 'simple - before hook', run_hook[0]
399 assert run_hook[1] == 'helper - before hook', run_hook[1]
400 assert run_hook[2] == 'last - before hook', run_hook[2]
401 assert run_hook[3] == 'inside', run_hook[3]
402
403 run_hook = []
404 response = app.get('/sub/')
405 assert response.status_int == 200
406 assert response.body == b'This is sub controller!'
407
408 assert len(run_hook) == 4, run_hook
409 assert run_hook[0] == 'simple - before hook', run_hook[0]
410 assert run_hook[1] == 'helper - before hook', run_hook[1]
411 assert run_hook[2] == 'last - before hook', run_hook[2]
412 # LastHook is invoked once again -
413 # for each different instance of the Hook in the two Controllers
414 assert run_hook[3] == 'last - before hook', run_hook[3]
415
416 def test_internal_redirect_with_after_hook(self):
417 run_hook = []
418
419 class RootController(object):
420 @expose()
421 def internal(self):
422 redirect('/testing', internal=True)
423
424 @expose()
425 def testing(self):
426 return 'it worked!'
427
428 class SimpleHook(PecanHook):
429 def after(self, state):
430 run_hook.append('after')
431
432 app = TestApp(make_app(RootController(), hooks=[SimpleHook()]))
433 response = app.get('/internal')
434 assert response.body == b'it worked!'
435
436 assert len(run_hook) == 1
437
438
439 class TestStateAccess(PecanTestCase):
440
441 def setUp(self):
442 super(TestStateAccess, self).setUp()
443 self.args = None
444
445 class RootController(object):
446 @expose()
447 def index(self):
448 return 'Hello, World!'
449
450 @expose()
451 def greet(self, name):
452 return 'Hello, %s!' % name
453
454 @expose()
455 def greetmore(self, *args):
456 return 'Hello, %s!' % args[0]
457
458 @expose()
459 def kwargs(self, **kw):
460 return 'Hello, %s!' % kw['name']
461
462 @expose()
463 def mixed(self, first, second, *args):
464 return 'Mixed'
465
466 class SimpleHook(PecanHook):
467 def before(inself, state):
468 self.args = (state.controller, state.arguments)
469
470 self.root = RootController()
471 self.app = TestApp(make_app(self.root, hooks=[SimpleHook()]))
472
473 def test_no_args(self):
474 self.app.get('/')
475 assert self.args[0] == self.root.index
476 assert isinstance(self.args[1], inspect.Arguments)
477 assert self.args[1].args == []
478 assert self.args[1].varargs == []
479 assert kwargs(self.args[1]) == {}
480
481 def test_single_arg(self):
482 self.app.get('/greet/joe')
483 assert self.args[0] == self.root.greet
484 assert isinstance(self.args[1], inspect.Arguments)
485 assert self.args[1].args == ['joe']
486 assert self.args[1].varargs == []
487 assert kwargs(self.args[1]) == {}
488
489 def test_single_vararg(self):
490 self.app.get('/greetmore/joe')
491 assert self.args[0] == self.root.greetmore
492 assert isinstance(self.args[1], inspect.Arguments)
493 assert self.args[1].args == []
494 assert self.args[1].varargs == ['joe']
495 assert kwargs(self.args[1]) == {}
496
497 def test_single_kw(self):
498 self.app.get('/kwargs/?name=joe')
499 assert self.args[0] == self.root.kwargs
500 assert isinstance(self.args[1], inspect.Arguments)
501 assert self.args[1].args == []
502 assert self.args[1].varargs == []
503 assert kwargs(self.args[1]) == {'name': 'joe'}
504
505 def test_single_kw_post(self):
506 self.app.post('/kwargs/', params={'name': 'joe'})
507 assert self.args[0] == self.root.kwargs
508 assert isinstance(self.args[1], inspect.Arguments)
509 assert self.args[1].args == []
510 assert self.args[1].varargs == []
511 assert kwargs(self.args[1]) == {'name': 'joe'}
512
513 def test_mixed_args(self):
514 self.app.get('/mixed/foo/bar/spam/eggs')
515 assert self.args[0] == self.root.mixed
516 assert isinstance(self.args[1], inspect.Arguments)
517 assert self.args[1].args == ['foo', 'bar']
518 assert self.args[1].varargs == ['spam', 'eggs']
519
520
521 class TestStateAccessWithoutThreadLocals(PecanTestCase):
522
523 def setUp(self):
524 super(TestStateAccessWithoutThreadLocals, self).setUp()
525 self.args = None
526
527 class RootController(object):
528 @expose()
529 def index(self, req, resp):
530 return 'Hello, World!'
531
532 @expose()
533 def greet(self, req, resp, name):
534 return 'Hello, %s!' % name
535
536 @expose()
537 def greetmore(self, req, resp, *args):
538 return 'Hello, %s!' % args[0]
539
540 @expose()
541 def kwargs(self, req, resp, **kw):
542 return 'Hello, %s!' % kw['name']
543
544 @expose()
545 def mixed(self, req, resp, first, second, *args):
546 return 'Mixed'
547
548 class SimpleHook(PecanHook):
549 def before(inself, state):
550 self.args = (state.controller, state.arguments)
551
552 self.root = RootController()
553 self.app = TestApp(make_app(
554 self.root,
555 hooks=[SimpleHook()],
556 use_context_locals=False
557 ))
558
559 def test_no_args(self):
560 self.app.get('/')
561 assert self.args[0] == self.root.index
562 assert isinstance(self.args[1], inspect.Arguments)
563 assert len(self.args[1].args) == 2
564 assert isinstance(self.args[1].args[0], Request)
565 assert isinstance(self.args[1].args[1], Response)
566 assert self.args[1].varargs == []
567 assert kwargs(self.args[1]) == {}
568
569 def test_single_arg(self):
570 self.app.get('/greet/joe')
571 assert self.args[0] == self.root.greet
572 assert isinstance(self.args[1], inspect.Arguments)
573 assert len(self.args[1].args) == 3
574 assert isinstance(self.args[1].args[0], Request)
575 assert isinstance(self.args[1].args[1], Response)
576 assert self.args[1].args[2] == 'joe'
577 assert self.args[1].varargs == []
578 assert kwargs(self.args[1]) == {}
579
580 def test_single_vararg(self):
581 self.app.get('/greetmore/joe')
582 assert self.args[0] == self.root.greetmore
583 assert isinstance(self.args[1], inspect.Arguments)
584 assert len(self.args[1].args) == 2
585 assert isinstance(self.args[1].args[0], Request)
586 assert isinstance(self.args[1].args[1], Response)
587 assert self.args[1].varargs == ['joe']
588 assert kwargs(self.args[1]) == {}
589
590 def test_single_kw(self):
591 self.app.get('/kwargs/?name=joe')
592 assert self.args[0] == self.root.kwargs
593 assert isinstance(self.args[1], inspect.Arguments)
594 assert len(self.args[1].args) == 2
595 assert isinstance(self.args[1].args[0], Request)
596 assert isinstance(self.args[1].args[1], Response)
597 assert self.args[1].varargs == []
598 assert kwargs(self.args[1]) == {'name': 'joe'}
599
600 def test_single_kw_post(self):
601 self.app.post('/kwargs/', params={'name': 'joe'})
602 assert self.args[0] == self.root.kwargs
603 assert isinstance(self.args[1], inspect.Arguments)
604 assert len(self.args[1].args) == 2
605 assert isinstance(self.args[1].args[0], Request)
606 assert isinstance(self.args[1].args[1], Response)
607 assert self.args[1].varargs == []
608 assert kwargs(self.args[1]) == {'name': 'joe'}
609
610 def test_mixed_args(self):
611 self.app.get('/mixed/foo/bar/spam/eggs')
612 assert self.args[0] == self.root.mixed
613 assert isinstance(self.args[1], inspect.Arguments)
614 assert len(self.args[1].args) == 4
615 assert isinstance(self.args[1].args[0], Request)
616 assert isinstance(self.args[1].args[1], Response)
617 assert self.args[1].args[2:] == ['foo', 'bar']
618 assert self.args[1].varargs == ['spam', 'eggs']
619
620
621 class TestRestControllerStateAccess(PecanTestCase):
622
623 def setUp(self):
624 super(TestRestControllerStateAccess, self).setUp()
625 self.args = None
626
627 class RootController(rest.RestController):
628
629 @expose()
630 def _default(self, _id, *args, **kw):
631 return 'Default'
632
633 @expose()
634 def get_all(self, **kw):
635 return 'All'
636
637 @expose()
638 def get_one(self, _id, *args, **kw):
639 return 'One'
640
641 @expose()
642 def post(self, *args, **kw):
643 return 'POST'
644
645 @expose()
646 def put(self, _id, *args, **kw):
647 return 'PUT'
648
649 @expose()
650 def delete(self, _id, *args, **kw):
651 return 'DELETE'
652
653 class SimpleHook(PecanHook):
654 def before(inself, state):
655 self.args = (state.controller, state.arguments)
656
657 self.root = RootController()
658 self.app = TestApp(make_app(self.root, hooks=[SimpleHook()]))
659
660 def test_get_all(self):
661 self.app.get('/')
662 assert self.args[0] == self.root.get_all
663 assert isinstance(self.args[1], inspect.Arguments)
664 assert self.args[1].args == []
665 assert self.args[1].varargs == []
666 assert kwargs(self.args[1]) == {}
667
668 def test_get_all_with_kwargs(self):
669 self.app.get('/?foo=bar')
670 assert self.args[0] == self.root.get_all
671 assert isinstance(self.args[1], inspect.Arguments)
672 assert self.args[1].args == []
673 assert self.args[1].varargs == []
674 assert kwargs(self.args[1]) == {'foo': 'bar'}
675
676 def test_get_one(self):
677 self.app.get('/1')
678 assert self.args[0] == self.root.get_one
679 assert isinstance(self.args[1], inspect.Arguments)
680 assert self.args[1].args == ['1']
681 assert self.args[1].varargs == []
682 assert kwargs(self.args[1]) == {}
683
684 def test_get_one_with_varargs(self):
685 self.app.get('/1/2/3')
686 assert self.args[0] == self.root.get_one
687 assert isinstance(self.args[1], inspect.Arguments)
688 assert self.args[1].args == ['1']
689 assert self.args[1].varargs == ['2', '3']
690 assert kwargs(self.args[1]) == {}
691
692 def test_get_one_with_kwargs(self):
693 self.app.get('/1?foo=bar')
694 assert self.args[0] == self.root.get_one
695 assert isinstance(self.args[1], inspect.Arguments)
696 assert self.args[1].args == ['1']
697 assert self.args[1].varargs == []
698 assert kwargs(self.args[1]) == {'foo': 'bar'}
699
700 def test_post(self):
701 self.app.post('/')
702 assert self.args[0] == self.root.post
703 assert isinstance(self.args[1], inspect.Arguments)
704 assert self.args[1].args == []
705 assert self.args[1].varargs == []
706 assert kwargs(self.args[1]) == {}
707
708 def test_post_with_varargs(self):
709 self.app.post('/foo/bar')
710 assert self.args[0] == self.root.post
711 assert isinstance(self.args[1], inspect.Arguments)
712 assert self.args[1].args == []
713 assert self.args[1].varargs == ['foo', 'bar']
714 assert kwargs(self.args[1]) == {}
715
716 def test_post_with_kwargs(self):
717 self.app.post('/', params={'foo': 'bar'})
718 assert self.args[0] == self.root.post
719 assert isinstance(self.args[1], inspect.Arguments)
720 assert self.args[1].args == []
721 assert self.args[1].varargs == []
722 assert kwargs(self.args[1]) == {'foo': 'bar'}
723
724 def test_put(self):
725 self.app.put('/1')
726 assert self.args[0] == self.root.put
727 assert isinstance(self.args[1], inspect.Arguments)
728 assert self.args[1].args == ['1']
729 assert self.args[1].varargs == []
730 assert kwargs(self.args[1]) == {}
731
732 def test_put_with_method_argument(self):
733 self.app.post('/1?_method=put')
734 assert self.args[0] == self.root.put
735 assert isinstance(self.args[1], inspect.Arguments)
736 assert self.args[1].args == ['1']
737 assert self.args[1].varargs == []
738 assert kwargs(self.args[1]) == {'_method': 'put'}
739
740 def test_put_with_varargs(self):
741 self.app.put('/1/2/3')
742 assert self.args[0] == self.root.put
743 assert isinstance(self.args[1], inspect.Arguments)
744 assert self.args[1].args == ['1']
745 assert self.args[1].varargs == ['2', '3']
746 assert kwargs(self.args[1]) == {}
747
748 def test_put_with_kwargs(self):
749 self.app.put('/1?foo=bar')
750 assert self.args[0] == self.root.put
751 assert isinstance(self.args[1], inspect.Arguments)
752 assert self.args[1].args == ['1']
753 assert self.args[1].varargs == []
754 assert kwargs(self.args[1]) == {'foo': 'bar'}
755
756 def test_delete(self):
757 self.app.delete('/1')
758 assert self.args[0] == self.root.delete
759 assert isinstance(self.args[1], inspect.Arguments)
760 assert self.args[1].args == ['1']
761 assert self.args[1].varargs == []
762 assert kwargs(self.args[1]) == {}
763
764 def test_delete_with_method_argument(self):
765 self.app.post('/1?_method=delete')
766 assert self.args[0] == self.root.delete
767 assert isinstance(self.args[1], inspect.Arguments)
768 assert self.args[1].args == ['1']
769 assert self.args[1].varargs == []
770 assert kwargs(self.args[1]) == {'_method': 'delete'}
771
772 def test_delete_with_varargs(self):
773 self.app.delete('/1/2/3')
774 assert self.args[0] == self.root.delete
775 assert isinstance(self.args[1], inspect.Arguments)
776 assert self.args[1].args == ['1']
777 assert self.args[1].varargs == ['2', '3']
778 assert kwargs(self.args[1]) == {}
779
780 def test_delete_with_kwargs(self):
781 self.app.delete('/1?foo=bar')
782 assert self.args[0] == self.root.delete
783 assert isinstance(self.args[1], inspect.Arguments)
784 assert self.args[1].args == ['1']
785 assert self.args[1].varargs == []
786 assert kwargs(self.args[1]) == {'foo': 'bar'}
787
788 def test_post_with_invalid_method_kwarg(self):
789 self.app.post('/1?_method=invalid')
790 assert self.args[0] == self.root._default
791 assert isinstance(self.args[1], inspect.Arguments)
792 assert self.args[1].args == ['1']
793 assert self.args[1].varargs == []
794 assert kwargs(self.args[1]) == {'_method': 'invalid'}
795
796
797 class TestTransactionHook(PecanTestCase):
798 def test_transaction_hook(self):
799 run_hook = []
800
801 class RootController(object):
802 @expose()
803 def index(self):
804 run_hook.append('inside')
805 return 'Hello, World!'
806
807 @expose()
808 def redirect(self):
809 redirect('/')
810
811 @expose()
812 def error(self):
813 return [][1]
814
815 def gen(event):
816 return lambda: run_hook.append(event)
817
818 app = TestApp(make_app(RootController(), hooks=[
819 TransactionHook(
820 start=gen('start'),
821 start_ro=gen('start_ro'),
822 commit=gen('commit'),
823 rollback=gen('rollback'),
824 clear=gen('clear')
825 )
826 ]))
827
828 response = app.get('/')
829 assert response.status_int == 200
830 assert response.body == b'Hello, World!'
831
832 assert len(run_hook) == 3
833 assert run_hook[0] == 'start_ro'
834 assert run_hook[1] == 'inside'
835 assert run_hook[2] == 'clear'
836
837 run_hook = []
838
839 response = app.post('/')
840 assert response.status_int == 200
841 assert response.body == b'Hello, World!'
842
843 assert len(run_hook) == 4
844 assert run_hook[0] == 'start'
845 assert run_hook[1] == 'inside'
846 assert run_hook[2] == 'commit'
847 assert run_hook[3] == 'clear'
848
849 #
850 # test hooks for GET /redirect
851 # This controller should always be non-transactional
852 #
853
854 run_hook = []
855
856 response = app.get('/redirect')
857 assert response.status_int == 302
858 assert len(run_hook) == 2
859 assert run_hook[0] == 'start_ro'
860 assert run_hook[1] == 'clear'
861
862 #
863 # test hooks for POST /redirect
864 # This controller should always be transactional,
865 # even in the case of redirects
866 #
867
868 run_hook = []
869
870 response = app.post('/redirect')
871 assert response.status_int == 302
872 assert len(run_hook) == 3
873 assert run_hook[0] == 'start'
874 assert run_hook[1] == 'commit'
875 assert run_hook[2] == 'clear'
876
877 run_hook = []
878 try:
879 response = app.post('/error')
880 except IndexError:
881 pass
882
883 assert len(run_hook) == 3
884 assert run_hook[0] == 'start'
885 assert run_hook[1] == 'rollback'
886 assert run_hook[2] == 'clear'
887
888 def test_transaction_hook_with_after_actions(self):
889 run_hook = []
890
891 def action(name):
892 def action_impl():
893 run_hook.append(name)
894 return action_impl
895
896 class RootController(object):
897 @expose()
898 @after_commit(action('action-one'))
899 def index(self):
900 run_hook.append('inside')
901 return 'Index Method!'
902
903 @expose()
904 @transactional()
905 @after_commit(action('action-two'))
906 def decorated(self):
907 run_hook.append('inside')
908 return 'Decorated Method!'
909
910 @expose()
911 @after_rollback(action('action-three'))
912 def rollback(self):
913 abort(500)
914
915 @expose()
916 @transactional()
917 @after_rollback(action('action-four'))
918 def rollback_decorated(self):
919 abort(500)
920
921 def gen(event):
922 return lambda: run_hook.append(event)
923
924 app = TestApp(make_app(RootController(), hooks=[
925 TransactionHook(
926 start=gen('start'),
927 start_ro=gen('start_ro'),
928 commit=gen('commit'),
929 rollback=gen('rollback'),
930 clear=gen('clear')
931 )
932 ]))
933
934 response = app.get('/')
935 assert response.status_int == 200
936 assert response.body == b'Index Method!'
937
938 assert len(run_hook) == 3
939 assert run_hook[0] == 'start_ro'
940 assert run_hook[1] == 'inside'
941 assert run_hook[2] == 'clear'
942
943 run_hook = []
944
945 response = app.post('/')
946 assert response.status_int == 200
947 assert response.body == b'Index Method!'
948
949 assert len(run_hook) == 5
950 assert run_hook[0] == 'start'
951 assert run_hook[1] == 'inside'
952 assert run_hook[2] == 'commit'
953 assert run_hook[3] == 'action-one'
954 assert run_hook[4] == 'clear'
955
956 run_hook = []
957
958 response = app.get('/decorated')
959 assert response.status_int == 200
960 assert response.body == b'Decorated Method!'
961
962 assert len(run_hook) == 7
963 assert run_hook[0] == 'start_ro'
964 assert run_hook[1] == 'clear'
965 assert run_hook[2] == 'start'
966 assert run_hook[3] == 'inside'
967 assert run_hook[4] == 'commit'
968 assert run_hook[5] == 'action-two'
969 assert run_hook[6] == 'clear'
970
971 run_hook = []
972
973 response = app.get('/rollback', expect_errors=True)
974 assert response.status_int == 500
975
976 assert len(run_hook) == 2
977 assert run_hook[0] == 'start_ro'
978 assert run_hook[1] == 'clear'
979
980 run_hook = []
981
982 response = app.post('/rollback', expect_errors=True)
983 assert response.status_int == 500
984
985 assert len(run_hook) == 4
986 assert run_hook[0] == 'start'
987 assert run_hook[1] == 'rollback'
988 assert run_hook[2] == 'action-three'
989 assert run_hook[3] == 'clear'
990
991 run_hook = []
992
993 response = app.get('/rollback_decorated', expect_errors=True)
994 assert response.status_int == 500
995
996 assert len(run_hook) == 6
997 assert run_hook[0] == 'start_ro'
998 assert run_hook[1] == 'clear'
999 assert run_hook[2] == 'start'
1000 assert run_hook[3] == 'rollback'
1001 assert run_hook[4] == 'action-four'
1002 assert run_hook[5] == 'clear'
1003
1004 run_hook = []
1005
1006 response = app.get('/fourohfour', status=404)
1007 assert response.status_int == 404
1008
1009 assert len(run_hook) == 2
1010 assert run_hook[0] == 'start_ro'
1011 assert run_hook[1] == 'clear'
1012
1013 def test_transaction_hook_with_transactional_decorator(self):
1014 run_hook = []
1015
1016 class RootController(object):
1017 @expose()
1018 def index(self):
1019 run_hook.append('inside')
1020 return 'Hello, World!'
1021
1022 @expose()
1023 def redirect(self):
1024 redirect('/')
1025
1026 @expose()
1027 @transactional()
1028 def redirect_transactional(self):
1029 redirect('/')
1030
1031 @expose()
1032 @transactional(False)
1033 def redirect_rollback(self):
1034 redirect('/')
1035
1036 @expose()
1037 def error(self):
1038 return [][1]
1039
1040 @expose()
1041 @transactional(False)
1042 def error_rollback(self):
1043 return [][1]
1044
1045 @expose()
1046 @transactional()
1047 def error_transactional(self):
1048 return [][1]
1049
1050 def gen(event):
1051 return lambda: run_hook.append(event)
1052
1053 app = TestApp(make_app(RootController(), hooks=[
1054 TransactionHook(
1055 start=gen('start'),
1056 start_ro=gen('start_ro'),
1057 commit=gen('commit'),
1058 rollback=gen('rollback'),
1059 clear=gen('clear')
1060 )
1061 ]))
1062
1063 response = app.get('/')
1064 assert response.status_int == 200
1065 assert response.body == b'Hello, World!'
1066
1067 assert len(run_hook) == 3
1068 assert run_hook[0] == 'start_ro'
1069 assert run_hook[1] == 'inside'
1070 assert run_hook[2] == 'clear'
1071
1072 run_hook = []
1073
1074 # test hooks for /
1075
1076 response = app.post('/')
1077 assert response.status_int == 200
1078 assert response.body == b'Hello, World!'
1079
1080 assert len(run_hook) == 4
1081 assert run_hook[0] == 'start'
1082 assert run_hook[1] == 'inside'
1083 assert run_hook[2] == 'commit'
1084 assert run_hook[3] == 'clear'
1085
1086 #
1087 # test hooks for GET /redirect
1088 # This controller should always be non-transactional
1089 #
1090
1091 run_hook = []
1092
1093 response = app.get('/redirect')
1094 assert response.status_int == 302
1095 assert len(run_hook) == 2
1096 assert run_hook[0] == 'start_ro'
1097 assert run_hook[1] == 'clear'
1098
1099 #
1100 # test hooks for POST /redirect
1101 # This controller should always be transactional,
1102 # even in the case of redirects
1103 #
1104
1105 run_hook = []
1106
1107 response = app.post('/redirect')
1108 assert response.status_int == 302
1109 assert len(run_hook) == 3
1110 assert run_hook[0] == 'start'
1111 assert run_hook[1] == 'commit'
1112 assert run_hook[2] == 'clear'
1113
1114 #
1115 # test hooks for GET /redirect_transactional
1116 # This controller should always be transactional,
1117 # even in the case of redirects
1118 #
1119
1120 run_hook = []
1121
1122 response = app.get('/redirect_transactional')
1123 assert response.status_int == 302
1124 assert len(run_hook) == 5
1125 assert run_hook[0] == 'start_ro'
1126 assert run_hook[1] == 'clear'
1127 assert run_hook[2] == 'start'
1128 assert run_hook[3] == 'commit'
1129 assert run_hook[4] == 'clear'
1130
1131 #
1132 # test hooks for POST /redirect_transactional
1133 # This controller should always be transactional,
1134 # even in the case of redirects
1135 #
1136
1137 run_hook = []
1138
1139 response = app.post('/redirect_transactional')
1140 assert response.status_int == 302
1141 assert len(run_hook) == 3
1142 assert run_hook[0] == 'start'
1143 assert run_hook[1] == 'commit'
1144 assert run_hook[2] == 'clear'
1145
1146 #
1147 # test hooks for GET /redirect_rollback
1148 # This controller should always be transactional,
1149 # *except* in the case of redirects
1150 #
1151 run_hook = []
1152
1153 response = app.get('/redirect_rollback')
1154 assert response.status_int == 302
1155 assert len(run_hook) == 5
1156 assert run_hook[0] == 'start_ro'
1157 assert run_hook[1] == 'clear'
1158 assert run_hook[2] == 'start'
1159 assert run_hook[3] == 'rollback'
1160 assert run_hook[4] == 'clear'
1161
1162 #
1163 # test hooks for POST /redirect_rollback
1164 # This controller should always be transactional,
1165 # *except* in the case of redirects
1166 #
1167
1168 run_hook = []
1169
1170 response = app.post('/redirect_rollback')
1171 assert response.status_int == 302
1172 assert len(run_hook) == 3
1173 assert run_hook[0] == 'start'
1174 assert run_hook[1] == 'rollback'
1175 assert run_hook[2] == 'clear'
1176
1177 #
1178 # Exceptions (other than HTTPFound) should *always*
1179 # rollback no matter what
1180 #
1181 run_hook = []
1182
1183 try:
1184 response = app.post('/error')
1185 except IndexError:
1186 pass
1187
1188 assert len(run_hook) == 3
1189 assert run_hook[0] == 'start'
1190 assert run_hook[1] == 'rollback'
1191 assert run_hook[2] == 'clear'
1192
1193 run_hook = []
1194
1195 try:
1196 response = app.get('/error')
1197 except IndexError:
1198 pass
1199
1200 assert len(run_hook) == 2
1201 assert run_hook[0] == 'start_ro'
1202 assert run_hook[1] == 'clear'
1203
1204 run_hook = []
1205
1206 try:
1207 response = app.post('/error_transactional')
1208 except IndexError:
1209 pass
1210
1211 assert len(run_hook) == 3
1212 assert run_hook[0] == 'start'
1213 assert run_hook[1] == 'rollback'
1214 assert run_hook[2] == 'clear'
1215
1216 run_hook = []
1217
1218 try:
1219 response = app.get('/error_transactional')
1220 except IndexError:
1221 pass
1222
1223 assert len(run_hook) == 5
1224 assert run_hook[0] == 'start_ro'
1225 assert run_hook[1] == 'clear'
1226 assert run_hook[2] == 'start'
1227 assert run_hook[3] == 'rollback'
1228 assert run_hook[4] == 'clear'
1229
1230 run_hook = []
1231
1232 try:
1233 response = app.post('/error_rollback')
1234 except IndexError:
1235 pass
1236
1237 assert len(run_hook) == 3
1238 assert run_hook[0] == 'start'
1239 assert run_hook[1] == 'rollback'
1240 assert run_hook[2] == 'clear'
1241
1242 run_hook = []
1243
1244 try:
1245 response = app.get('/error_rollback')
1246 except IndexError:
1247 pass
1248
1249 assert len(run_hook) == 5
1250 assert run_hook[0] == 'start_ro'
1251 assert run_hook[1] == 'clear'
1252 assert run_hook[2] == 'start'
1253 assert run_hook[3] == 'rollback'
1254 assert run_hook[4] == 'clear'
1255
1256 def test_transaction_hook_with_transactional_class_decorator(self):
1257 run_hook = []
1258
1259 @transactional()
1260 class RootController(object):
1261 @expose()
1262 def index(self):
1263 run_hook.append('inside')
1264 return 'Hello, World!'
1265
1266 @expose()
1267 def redirect(self):
1268 redirect('/')
1269
1270 @expose()
1271 @transactional(False)
1272 def redirect_rollback(self):
1273 redirect('/')
1274
1275 @expose()
1276 def error(self):
1277 return [][1]
1278
1279 @expose(generic=True)
1280 def generic(self):
1281 pass
1282
1283 @generic.when(method='GET')
1284 def generic_get(self):
1285 run_hook.append('inside')
1286 return 'generic get'
1287
1288 @generic.when(method='POST')
1289 def generic_post(self):
1290 run_hook.append('inside')
1291 return 'generic post'
1292
1293 def gen(event):
1294 return lambda: run_hook.append(event)
1295
1296 app = TestApp(make_app(RootController(), hooks=[
1297 TransactionHook(
1298 start=gen('start'),
1299 start_ro=gen('start_ro'),
1300 commit=gen('commit'),
1301 rollback=gen('rollback'),
1302 clear=gen('clear')
1303 )
1304 ]))
1305
1306 response = app.get('/')
1307 assert response.status_int == 200
1308 assert response.body == b'Hello, World!'
1309
1310 assert len(run_hook) == 6
1311 assert run_hook[0] == 'start_ro'
1312 assert run_hook[1] == 'clear'
1313 assert run_hook[2] == 'start'
1314 assert run_hook[3] == 'inside'
1315 assert run_hook[4] == 'commit'
1316 assert run_hook[5] == 'clear'
1317
1318 run_hook = []
1319
1320 # test hooks for /
1321
1322 response = app.post('/')
1323 assert response.status_int == 200
1324 assert response.body == b'Hello, World!'
1325
1326 assert len(run_hook) == 4
1327 assert run_hook[0] == 'start'
1328 assert run_hook[1] == 'inside'
1329 assert run_hook[2] == 'commit'
1330 assert run_hook[3] == 'clear'
1331
1332 #
1333 # test hooks for GET /redirect
1334 # This controller should always be transactional,
1335 # even in the case of redirects
1336 #
1337
1338 run_hook = []
1339 response = app.get('/redirect')
1340 assert response.status_int == 302
1341 assert len(run_hook) == 5
1342 assert run_hook[0] == 'start_ro'
1343 assert run_hook[1] == 'clear'
1344 assert run_hook[2] == 'start'
1345 assert run_hook[3] == 'commit'
1346 assert run_hook[4] == 'clear'
1347
1348 #
1349 # test hooks for POST /redirect
1350 # This controller should always be transactional,
1351 # even in the case of redirects
1352 #
1353
1354 run_hook = []
1355
1356 response = app.post('/redirect')
1357 assert response.status_int == 302
1358 assert len(run_hook) == 3
1359 assert run_hook[0] == 'start'
1360 assert run_hook[1] == 'commit'
1361 assert run_hook[2] == 'clear'
1362
1363 #
1364 # test hooks for GET /redirect_rollback
1365 # This controller should always be transactional,
1366 # *except* in the case of redirects
1367 #
1368 run_hook = []
1369
1370 response = app.get('/redirect_rollback')
1371 assert response.status_int == 302
1372 assert len(run_hook) == 5
1373 assert run_hook[0] == 'start_ro'
1374 assert run_hook[1] == 'clear'
1375 assert run_hook[2] == 'start'
1376 assert run_hook[3] == 'rollback'
1377 assert run_hook[4] == 'clear'
1378
1379 #
1380 # test hooks for POST /redirect_rollback
1381 # This controller should always be transactional,
1382 # *except* in the case of redirects
1383 #
1384
1385 run_hook = []
1386
1387 response = app.post('/redirect_rollback')
1388 assert response.status_int == 302
1389 assert len(run_hook) == 3
1390 assert run_hook[0] == 'start'
1391 assert run_hook[1] == 'rollback'
1392 assert run_hook[2] == 'clear'
1393
1394 #
1395 # Exceptions (other than HTTPFound) should *always*
1396 # rollback no matter what
1397 #
1398 run_hook = []
1399
1400 try:
1401 response = app.post('/error')
1402 except IndexError:
1403 pass
1404
1405 assert len(run_hook) == 3
1406 assert run_hook[0] == 'start'
1407 assert run_hook[1] == 'rollback'
1408 assert run_hook[2] == 'clear'
1409
1410 run_hook = []
1411
1412 try:
1413 response = app.get('/error')
1414 except IndexError:
1415 pass
1416
1417 assert len(run_hook) == 5
1418 assert run_hook[0] == 'start_ro'
1419 assert run_hook[1] == 'clear'
1420 assert run_hook[2] == 'start'
1421 assert run_hook[3] == 'rollback'
1422 assert run_hook[4] == 'clear'
1423
1424 #
1425 # test hooks for GET /generic
1426 # This controller should always be transactional,
1427 #
1428
1429 run_hook = []
1430
1431 response = app.get('/generic')
1432 assert response.status_int == 200
1433 assert response.body == b'generic get'
1434 assert len(run_hook) == 6
1435 assert run_hook[0] == 'start_ro'
1436 assert run_hook[1] == 'clear'
1437 assert run_hook[2] == 'start'
1438 assert run_hook[3] == 'inside'
1439 assert run_hook[4] == 'commit'
1440 assert run_hook[5] == 'clear'
1441
1442 #
1443 # test hooks for POST /generic
1444 # This controller should always be transactional,
1445 #
1446
1447 run_hook = []
1448
1449 response = app.post('/generic')
1450 assert response.status_int == 200
1451 assert response.body == b'generic post'
1452 assert len(run_hook) == 4
1453 assert run_hook[0] == 'start'
1454 assert run_hook[1] == 'inside'
1455 assert run_hook[2] == 'commit'
1456 assert run_hook[3] == 'clear'
1457
1458 def test_transaction_hook_with_broken_hook(self):
1459 """
1460 In a scenario where a preceding hook throws an exception,
1461 ensure that TransactionHook still rolls back properly.
1462 """
1463 run_hook = []
1464
1465 class RootController(object):
1466 @expose()
1467 def index(self):
1468 return 'Hello, World!'
1469
1470 def gen(event):
1471 return lambda: run_hook.append(event)
1472
1473 class MyCustomException(Exception):
1474 pass
1475
1476 class MyHook(PecanHook):
1477
1478 def on_route(self, state):
1479 raise MyCustomException('BROKEN!')
1480
1481 app = TestApp(make_app(RootController(), hooks=[
1482 MyHook(),
1483 TransactionHook(
1484 start=gen('start'),
1485 start_ro=gen('start_ro'),
1486 commit=gen('commit'),
1487 rollback=gen('rollback'),
1488 clear=gen('clear')
1489 )
1490 ]))
1491
1492 self.assertRaises(
1493 MyCustomException,
1494 app.get,
1495 '/'
1496 )
1497
1498 assert len(run_hook) == 1
1499 assert run_hook[0] == 'clear'
1500
1501
1502 class TestRequestViewerHook(PecanTestCase):
1503
1504 def test_basic_single_default_hook(self):
1505
1506 _stdout = StringIO()
1507
1508 class RootController(object):
1509 @expose()
1510 def index(self):
1511 return 'Hello, World!'
1512
1513 app = TestApp(
1514 make_app(
1515 RootController(), hooks=lambda: [
1516 RequestViewerHook(writer=_stdout)
1517 ]
1518 )
1519 )
1520 response = app.get('/')
1521
1522 out = _stdout.getvalue()
1523
1524 assert response.status_int == 200
1525 assert response.body == b'Hello, World!'
1526 assert 'path' in out
1527 assert 'method' in out
1528 assert 'status' in out
1529 assert 'method' in out
1530 assert 'params' in out
1531 assert 'hooks' in out
1532 assert '200 OK' in out
1533 assert "['RequestViewerHook']" in out
1534 assert '/' in out
1535
1536 def test_bad_response_from_app(self):
1537 """When exceptions are raised the hook deals with them properly"""
1538
1539 _stdout = StringIO()
1540
1541 class RootController(object):
1542 @expose()
1543 def index(self):
1544 return 'Hello, World!'
1545
1546 app = TestApp(
1547 make_app(
1548 RootController(), hooks=lambda: [
1549 RequestViewerHook(writer=_stdout)
1550 ]
1551 )
1552 )
1553 response = app.get('/404', expect_errors=True)
1554
1555 out = _stdout.getvalue()
1556
1557 assert response.status_int == 404
1558 assert 'path' in out
1559 assert 'method' in out
1560 assert 'status' in out
1561 assert 'method' in out
1562 assert 'params' in out
1563 assert 'hooks' in out
1564 assert '404 Not Found' in out
1565 assert "['RequestViewerHook']" in out
1566 assert '/' in out
1567
1568 def test_single_item(self):
1569
1570 _stdout = StringIO()
1571
1572 class RootController(object):
1573 @expose()
1574 def index(self):
1575 return 'Hello, World!'
1576
1577 app = TestApp(
1578 make_app(
1579 RootController(),
1580 hooks=lambda: [
1581 RequestViewerHook(
1582 config={'items': ['path']}, writer=_stdout
1583 )
1584 ]
1585 )
1586 )
1587 response = app.get('/')
1588
1589 out = _stdout.getvalue()
1590
1591 assert response.status_int == 200
1592 assert response.body == b'Hello, World!'
1593 assert '/' in out
1594 assert 'path' in out
1595 assert 'method' not in out
1596 assert 'status' not in out
1597 assert 'method' not in out
1598 assert 'params' not in out
1599 assert 'hooks' not in out
1600 assert '200 OK' not in out
1601 assert "['RequestViewerHook']" not in out
1602
1603 def test_single_blacklist_item(self):
1604
1605 _stdout = StringIO()
1606
1607 class RootController(object):
1608 @expose()
1609 def index(self):
1610 return 'Hello, World!'
1611
1612 app = TestApp(
1613 make_app(
1614 RootController(),
1615 hooks=lambda: [
1616 RequestViewerHook(
1617 config={'blacklist': ['/']}, writer=_stdout
1618 )
1619 ]
1620 )
1621 )
1622 response = app.get('/')
1623
1624 out = _stdout.getvalue()
1625
1626 assert response.status_int == 200
1627 assert response.body == b'Hello, World!'
1628 assert out == ''
1629
1630 def test_item_not_in_defaults(self):
1631
1632 _stdout = StringIO()
1633
1634 class RootController(object):
1635 @expose()
1636 def index(self):
1637 return 'Hello, World!'
1638
1639 app = TestApp(
1640 make_app(
1641 RootController(),
1642 hooks=lambda: [
1643 RequestViewerHook(
1644 config={'items': ['date']}, writer=_stdout
1645 )
1646 ]
1647 )
1648 )
1649 response = app.get('/')
1650
1651 out = _stdout.getvalue()
1652
1653 assert response.status_int == 200
1654 assert response.body == b'Hello, World!'
1655 assert 'date' in out
1656 assert 'method' not in out
1657 assert 'status' not in out
1658 assert 'method' not in out
1659 assert 'params' not in out
1660 assert 'hooks' not in out
1661 assert '200 OK' not in out
1662 assert "['RequestViewerHook']" not in out
1663 assert '/' not in out
1664
1665 def test_hook_formatting(self):
1666 hooks = ['<pecan.hooks.RequestViewerHook object at 0x103a5f910>']
1667 viewer = RequestViewerHook()
1668 formatted = viewer.format_hooks(hooks)
1669
1670 assert formatted == ['RequestViewerHook']
1671
1672 def test_deal_with_pecan_configs(self):
1673 """If config comes from pecan.conf convert it to dict"""
1674 conf = Config(conf_dict={'items': ['url']})
1675 viewer = RequestViewerHook(conf)
1676
1677 assert viewer.items == ['url']
1678
1679
1680 class TestRestControllerWithHooks(PecanTestCase):
1681
1682 def test_restcontroller_with_hooks(self):
1683
1684 class SomeHook(PecanHook):
1685
1686 def before(self, state):
1687 state.response.headers['X-Testing'] = 'XYZ'
1688
1689 class BaseController(rest.RestController):
1690
1691 @expose()
1692 def delete(self, _id):
1693 return 'Deleting %s' % _id
1694
1695 class RootController(BaseController, HookController):
1696
1697 __hooks__ = [SomeHook()]
1698
1699 @expose()
1700 def get_all(self):
1701 return 'Hello, World!'
1702
1703 @staticmethod
1704 def static(cls):
1705 return 'static'
1706
1707 @property
1708 def foo(self):
1709 return 'bar'
1710
1711 def testing123(self):
1712 return 'bar'
1713
1714 unhashable = [1, 'two', 3]
1715
1716 app = TestApp(
1717 make_app(
1718 RootController()
1719 )
1720 )
1721
1722 response = app.get('/')
1723 assert response.status_int == 200
1724 assert response.body == b'Hello, World!'
1725 assert response.headers['X-Testing'] == 'XYZ'
1726
1727 response = app.delete('/100/')
1728 assert response.status_int == 200
1729 assert response.body == b'Deleting 100'
1730 assert response.headers['X-Testing'] == 'XYZ'