summaryrefslogtreecommitdiff
path: root/pecan/tests/middleware/test_debug.py
blob: bc9d65fa10cf6dd16bc2922f7e5196273d4b9eb1 (plain)
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
from wsgiref.util import setup_testing_defaults

from webtest import TestApp
from six import b as b_

from pecan.middleware.debug import DebugMiddleware
from pecan.tests import PecanTestCase


class StripPasteVar(object):
    def __init__(self, app):
        self.app = app

    def __call__(self, environ, start_response):
        environ.pop('paste.testing')
        return self.app(environ, start_response)


class TestDebugMiddleware(PecanTestCase):

    def setUp(self):
        super(TestDebugMiddleware, self).setUp()

        def conditional_error_app(environ, start_response):
            setup_testing_defaults(environ)
            if environ['PATH_INFO'] == '/error':
                assert 1 == 2
            start_response("200 OK", [('Content-type', 'text/plain')])
            return [b_('requested page returned')]
        self.app = TestApp(StripPasteVar(DebugMiddleware(
            conditional_error_app
        )))

    def test_middleware_passes_through_when_no_exception_raised(self):
        r = self.app.get('/')
        assert r.status_int == 200
        assert r.body == b_('requested page returned')

    def test_middleware_gives_stack_trace_on_errors(self):
        r = self.app.get('/error', expect_errors=True)
        assert r.status_int == 400
        assert b_('AssertionError') in r.body

    def test_middleware_complains_in_multi_process_environment(self):

        class MultiProcessApp(object):

            def __init__(self, app):
                self.app = app

            def __call__(self, environ, start_response):
                environ['wsgi.multiprocess'] = True
                return self.app(environ, start_response)

        def conditional_error_app(environ, start_response):
            start_response("200 OK", [('Content-type', 'text/plain')])
            return ['Hello, World!']

        app = TestApp(MultiProcessApp(DebugMiddleware(conditional_error_app)))
        self.assertRaises(
            RuntimeError,
            app.get,
            '/'
        )

    def test_middlware_allows_for_post_mortem_debugging(self):
        def patch_debugger(d):
            def _patched_debug_request():
                d.append(True)
            return _patched_debug_request

        debugger = []

        app = TestApp(StripPasteVar(DebugMiddleware(
            self.app,
            patch_debugger(debugger)
        )))

        r = app.get('/error', expect_errors=True)
        assert r.status_int == 400

        r = app.get('/__pecan_initiate_pdb__', expect_errors=True)
        assert len(debugger) > 0