• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1from paste.fixture import *
2from paste.cgitb_catcher import CgitbMiddleware
3from paste import lint
4from .test_exceptions.test_error_middleware import clear_middleware
5
6def do_request(app, expect_status=500):
7    app = lint.middleware(app)
8    app = CgitbMiddleware(app, {}, display=True)
9    app = clear_middleware(app)
10    testapp = TestApp(app)
11    res = testapp.get('', status=expect_status,
12                      expect_errors=True)
13    return res
14
15
16############################################################
17## Applications that raise exceptions
18############################################################
19
20def bad_app():
21    "No argument list!"
22    return None
23
24def start_response_app(environ, start_response):
25    "raise error before start_response"
26    raise ValueError("hi")
27
28def after_start_response_app(environ, start_response):
29    start_response("200 OK", [('Content-type', 'text/plain')])
30    raise ValueError('error2')
31
32def iter_app(environ, start_response):
33    start_response("200 OK", [('Content-type', 'text/plain')])
34    return yielder([b'this', b' is ', b' a', None])
35
36def yielder(args):
37    for arg in args:
38        if arg is None:
39            raise ValueError("None raises error")
40        yield arg
41
42############################################################
43## Tests
44############################################################
45
46def test_makes_exception():
47    res = do_request(bad_app)
48    print(res)
49    if six.PY3:
50        assert 'bad_app() takes 0 positional arguments but 2 were given' in res
51    else:
52        assert 'bad_app() takes no arguments (2 given' in res
53    assert 'iterator = application(environ, start_response_wrapper)' in res
54    assert 'lint.py' in res
55    assert 'cgitb_catcher.py' in res
56
57def test_start_res():
58    res = do_request(start_response_app)
59    print(res)
60    assert 'ValueError: hi' in res
61    assert 'test_cgitb_catcher.py' in res
62    assert 'line 26, in start_response_app' in res
63
64def test_after_start():
65    res = do_request(after_start_response_app, 200)
66    print(res)
67    assert 'ValueError: error2' in res
68    assert 'line 30' in res
69
70def test_iter_app():
71    res = do_request(iter_app, 200)
72    print(res)
73    assert 'None raises error' in res
74    assert 'yielder' in res
75
76
77
78
79