forked from Pylons/pyramid
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_tweens.py
109 lines (79 loc) · 3.18 KB
/
test_tweens.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
import unittest
from pyramid import testing
class Test_excview_tween_factory(unittest.TestCase):
def setUp(self):
self.config = testing.setUp()
def tearDown(self):
testing.tearDown()
def _makeOne(self, handler, registry=None):
from pyramid.tweens import excview_tween_factory
if registry is None:
registry = self.config.registry
return excview_tween_factory(handler, registry)
def test_it_passthrough_no_exception(self):
dummy_response = DummyResponse()
def handler(request):
return dummy_response
tween = self._makeOne(handler)
request = DummyRequest()
result = tween(request)
self.assertTrue(result is dummy_response)
self.assertIsNone(request.exception)
self.assertIsNone(request.exc_info)
def test_it_catches_notfound(self):
from pyramid.httpexceptions import HTTPNotFound
from pyramid.request import Request
self.config.add_notfound_view(lambda exc, request: exc)
def handler(request):
raise HTTPNotFound
tween = self._makeOne(handler)
request = Request.blank('/')
request.registry = self.config.registry
result = tween(request)
self.assertEqual(result.status, '404 Not Found')
self.assertIsInstance(request.exception, HTTPNotFound)
self.assertEqual(request.exception, request.exc_info[1])
def test_it_catches_with_predicate(self):
from pyramid.request import Request
from pyramid.response import Response
def excview(request):
return Response('foo')
self.config.add_view(excview, context=ValueError, request_method='GET')
def handler(request):
raise ValueError
tween = self._makeOne(handler)
request = Request.blank('/')
request.registry = self.config.registry
result = tween(request)
self.assertTrue(b'foo' in result.body)
self.assertIsInstance(request.exception, ValueError)
self.assertEqual(request.exception, request.exc_info[1])
def test_it_reraises_on_mismatch(self):
from pyramid.request import Request
def excview(request): # pragma: no cover
pass
self.config.add_view(excview, context=ValueError, request_method='GET')
def handler(request):
raise ValueError
tween = self._makeOne(handler)
request = Request.blank('/')
request.registry = self.config.registry
request.method = 'POST'
self.assertRaises(ValueError, lambda: tween(request))
self.assertIsNone(request.exception)
self.assertIsNone(request.exc_info)
def test_it_reraises_on_no_match(self):
from pyramid.request import Request
def handler(request):
raise ValueError
tween = self._makeOne(handler)
request = Request.blank('/')
request.registry = self.config.registry
self.assertRaises(ValueError, lambda: tween(request))
self.assertIsNone(request.exception)
self.assertIsNone(request.exc_info)
class DummyRequest:
exception = None
exc_info = None
class DummyResponse:
pass