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