from wsgiref.validate import validator from paste import catch_exceptions from ..common_wsgi import call_app def test_no_exception(): @validator @catch_exceptions @validator def app(_, start_response): start_response("200 OK", [("Content-Type", "text/plain")]) return [b"Hello, world!"] response = call_app(app) assert response.data == b"Hello, world!" assert response.status == "200 OK" assert ("Content-Type", "text/plain") in response.headers def test_exception(capfd): @validator @catch_exceptions @validator def app(_, start_response): start_response("200 OK", [("Content-Type", "text/plain")]) raise Exception("Something went wrong") response = call_app(app) assert response.data == b"500 Internal Server Error\n" assert response.status == "500 Internal Server Error" assert ("Content-Type", "text/plain") in response.headers out, _ = capfd.readouterr() assert "Exception: Something went wrong\n" in out assert response.exc_info is not None assert isinstance(response.exc_info[1], Exception)