1from webtest import TestApp, lint 2from weberror.errormiddleware import ErrorMiddleware 3from paste.util.quoting import strip_html 4 5def do_request(app, expect_status=500): 6 app = lint.middleware(app) 7 app = ErrorMiddleware(app, {}, debug=True) 8 app = clear_middleware(app) 9 testapp = TestApp(app) 10 res = testapp.get('', status=expect_status, 11 expect_errors=True) 12 return res 13 14def clear_middleware(app): 15 """ 16 The fixture sets paste.throw_errors, which suppresses exactly what 17 we want to test in this case. This wrapper also strips exc_info 18 on the *first* call to start_response (but not the second, or 19 subsequent calls. 20 """ 21 def clear_throw_errors(environ, start_response): 22 headers_sent = [] 23 def replacement(status, headers, exc_info=None): 24 if headers_sent: 25 return start_response(status, headers, exc_info) 26 headers_sent.append(True) 27 return start_response(status, headers) 28 if 'paste.throw_errors' in environ: 29 del environ['paste.throw_errors'] 30 return app(environ, replacement) 31 return clear_throw_errors 32 33 34############################################################ 35## Applications that raise exceptions 36############################################################ 37 38def bad_app(): 39 "No argument list!" 40 return None 41 42def start_response_app(environ, start_response): 43 "raise error before start_response" 44 raise ValueError("hi") 45 46def after_start_response_app(environ, start_response): 47 start_response("200 OK", [('Content-type', 'text/plain')]) 48 raise ValueError('error2') 49 50def iter_app(environ, start_response): 51 start_response("200 OK", [('Content-type', 'text/plain')]) 52 return yielder(['this', ' is ', ' a', None]) 53 54def yielder(args): 55 for arg in args: 56 if arg is None: 57 raise ValueError("None raises error") 58 yield arg 59 60############################################################ 61## Tests 62############################################################ 63 64def test_makes_exception(): 65 res = do_request(bad_app) 66 assert '<html' in res 67 res = strip_html(str(res.body)) 68 assert 'bad_app() takes no arguments (2 given' in res 69 assert 'application(environ, sr_checker)' in res 70 assert 'webtest.lint' in res 71 assert 'weberror.errormiddleware' in res 72 73def test_start_res(): 74 res = do_request(start_response_app) 75 res = strip_html(str(res)) 76 #print res 77 assert 'ValueError: hi' in res 78 assert 'test_error_middleware' in res 79 # This assertion assumes that the start_response_app returns on line 45 80 # of this file. 81 assert ':44 in start_response_app' in res 82 83def test_after_start(): 84 res = do_request(after_start_response_app, 200) 85 res = strip_html(str(res)) 86 assert 'ValueError: error2' in res 87 # This assertion assumes that the start_response_app returns on line 45 88 # of this file. 89 assert ':48' in res 90 91def test_iter_app(): 92 res = do_request(lint.middleware(iter_app), 200) 93 #print res 94 assert 'None raises error' in res 95 assert 'yielder' in res 96 97 98 99 100