test_regression.py
This file
Path
tests/test_regression.py
98 lines · 2.3 KiB · Python
Last changed by
Merge pull request #3183 from singingwolfboy/pre-commitDavid Baumgoldbefore adoption1dda032
History of this fileTake it
1# -*- coding: utf-8 -*-2"""3 tests.regression4 ~~~~~~~~~~~~~~~~~~~~~~~~~~56 Tests regressions.78 :copyright: © 2010 by the Pallets team.9 :license: BSD, see LICENSE for more details.10"""1112import gc13import sys14import threading1516import pytest17from werkzeug.exceptions import NotFound1819import flask2021_gc_lock = threading.Lock()222324class assert_no_leak(object):25 def __enter__(self):26 gc.disable()27 _gc_lock.acquire()28 loc = flask._request_ctx_stack._local2930 # Force Python to track this dictionary at all times.31 # This is necessary since Python only starts tracking32 # dicts if they contain mutable objects. It's a horrible,33 # horrible hack but makes this kinda testable.34 loc.__storage__["FOOO"] = [1, 2, 3]3536 gc.collect()37 self.old_objects = len(gc.get_objects())3839 def __exit__(self, exc_type, exc_value, tb):40 gc.collect()41 new_objects = len(gc.get_objects())42 if new_objects > self.old_objects:43 pytest.fail("Example code leaked")44 _gc_lock.release()45 gc.enable()464748def test_memory_consumption():49 app = flask.Flask(__name__)5051 @app.route("/")52 def index():53 return flask.render_template("simple_template.html", whiskey=42)5455 def fire():56 with app.test_client() as c:57 rv = c.get("/")58 assert rv.status_code == 20059 assert rv.data == b"<h1>42</h1>"6061 # Trigger caches62 fire()6364 # This test only works on CPython 2.7.65 if sys.version_info >= (2, 7) and not hasattr(sys, "pypy_translation_info"):66 with assert_no_leak():67 for x in range(10):68 fire()697071def test_safe_join_toplevel_pardir():72 from flask.helpers import safe_join7374 with pytest.raises(NotFound):75 safe_join("/foo", "..")767778def test_aborting(app):79 class Foo(Exception):80 whatever = 428182 @app.errorhandler(Foo)83 def handle_foo(e):84 return str(e.whatever)8586 @app.route("/")87 def index():88 raise flask.abort(flask.redirect(flask.url_for("test")))8990 @app.route("/test")91 def test():92 raise Foo()9394 with app.test_client() as c:95 rv = c.get("/")96 assert rv.headers["Location"] == "http://localhost/test"97 rv = c.get("/test")98 assert rv.data == b"42"