def test_preserve_remembers_exception(): app = Flak(__name__) app.debug = True errors = [] @app.route('/fail') def fail_func(cx): 1 // 0 @app.route('/success') def success_func(cx): return 'Okay' @app.teardown def teardown_handler(cx, exc): errors.append(exc) c = app.test_client() # After this failure we did not yet call the teardown handler with pytest.raises(ZeroDivisionError): c.get('/fail') #assert errors == [] assert len(errors) == 1 # But this request triggers it, and it's an error c.get('/success') assert len(errors) == 2 # At this point another request does nothing. c.get('/success') assert len(errors) == 3 assert errors[1] is None
def test_route_decorator_custom_endpoint(): app = Flak(__name__) app.debug = True @app.route('/foo/') def foo(cx): return cx.request.endpoint @app.route('/bar/', endpoint='bar') def for_bar(cx): return cx.request.endpoint @app.route('/bar/123', endpoint='123') def for_bar_foo(cx): return cx.request.endpoint with app.test_context() as cx: assert cx.url_for('foo') == '/foo/' assert cx.url_for('bar') == '/bar/' assert cx.url_for('123') == '/bar/123' c = app.test_client() assert c.get('/foo/').data == b'foo' assert c.get('/bar/').data == b'bar' assert c.get('/bar/123').data == b'123'
def test_preserve_only_once(): app = Flak(__name__) app.debug = True @app.route('/fail') def fail_func(cx): 1 // 0 with app.test_client() as c: for x in range(3): with pytest.raises(ZeroDivisionError): c.get('/fail') assert c.captured_context is not None
def test_routing_redirect_debugging(): app = Flak(__name__) app.debug = True @app.route('/foo/', methods=['GET', 'POST']) def foo(cx): return 'success' with app.test_client() as c: try: c.post('/foo', data={}) except AssertionError as e: assert 'http://localhost/foo/' in str(e) assert ('Make sure to directly send ' 'your POST-request to this URL') in str(e) else: assert False, 'Expected exception' rv = c.get('/foo', data={}, follow_redirects=True) assert rv.data == b'success' app.debug = False with app.test_client() as c: rv = c.post('/foo', data={}, follow_redirects=True) assert rv.data == b'success'
def test_endpoint_override(): app = Flak(__name__) app.debug = True class Index(views.View): methods = ['GET', 'POST'] def dispatch_request(self, cx): return cx.request.method app.add_url_rule('/', Index.as_view('index')) with pytest.raises(AssertionError): app.add_url_rule('/', Index.as_view('index')) # But these tests should still pass. We just log a warning. common_asserts(app)
def test_enctype_debug_helper(): from flak.debughelpers import DebugFilesKeyError app = Flak(__name__) app.debug = True @app.route('/fail', methods=['POST']) def index(cx): return cx.request.files['foo'].filename with app.test_client() as c: try: c.post('/fail', data={'foo': 'index.txt'}) except DebugFilesKeyError as e: assert 'no file contents were transmitted' in str(e) assert 'This was submitted: "index.txt"' in str(e) else: assert False, 'Expected exception'
def test_debug_log(self, capsys): app = Flak(__name__) app.debug = True @app.route("/") def index(cx): app.logger.warning("the standard library is dead") app.logger.debug("this is a debug statement") return "" @app.route("/exc") def exc(cx): 1 // 0 with app.test_client() as c: c.get("/") out, err = capsys.readouterr() assert "WARNING in test_helpers [" in err assert os.path.basename(__file__.rsplit(".", 1)[0] + ".py") in err assert "the standard library is dead" in err assert "this is a debug statement" in err with pytest.raises(ZeroDivisionError): c.get("/exc")
def test_debug_log_override(self): app = Flak(__name__) app.debug = True app.logger_name = "flak_tests/test_debug_log_override" app.logger.level = 10 assert app.logger.level == 10