Skip to content

Commit e044b00

Browse files
committed
avoid triggering setupmethod late in tests
1 parent a406c29 commit e044b00

File tree

1 file changed

+42
-52
lines changed

1 file changed

+42
-52
lines changed

tests/test_basic.py

Lines changed: 42 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -329,6 +329,11 @@ def index():
329329
flask.session["testing"] = 42
330330
return "Hello World"
331331

332+
@app.route("/clear")
333+
def clear():
334+
flask.session.pop("testing", None)
335+
return "Goodbye World"
336+
332337
rv = client.get("/", "http://www.example.com:8080/test/")
333338
cookie = rv.headers["set-cookie"].lower()
334339
assert "domain=.example.com" in cookie
@@ -337,11 +342,6 @@ def index():
337342
assert "httponly" not in cookie
338343
assert "samesite" in cookie
339344

340-
@app.route("/clear")
341-
def clear():
342-
flask.session.pop("testing", None)
343-
return "Goodbye World"
344-
345345
rv = client.get("/clear", "http://www.example.com:8080/test/")
346346
cookie = rv.headers["set-cookie"].lower()
347347
assert "session=;" in cookie
@@ -1031,7 +1031,14 @@ def raise_e3():
10311031
assert rv.data == b"E2"
10321032

10331033

1034-
def test_trapping_of_bad_request_key_errors(app, client):
1034+
@pytest.mark.parametrize(
1035+
("debug", "trap", "expect_key", "expect_abort"),
1036+
[(False, None, True, True), (True, None, False, True), (False, True, False, False)],
1037+
)
1038+
def test_trap_bad_request_key_error(app, client, debug, trap, expect_key, expect_abort):
1039+
app.config["DEBUG"] = debug
1040+
app.config["TRAP_BAD_REQUEST_ERRORS"] = trap
1041+
10351042
@app.route("/key")
10361043
def fail():
10371044
flask.request.form["missing_key"]
@@ -1040,26 +1047,23 @@ def fail():
10401047
def allow_abort():
10411048
flask.abort(400)
10421049

1043-
rv = client.get("/key")
1044-
assert rv.status_code == 400
1045-
assert b"missing_key" not in rv.data
1046-
rv = client.get("/abort")
1047-
assert rv.status_code == 400
1050+
if expect_key:
1051+
rv = client.get("/key")
1052+
assert rv.status_code == 400
1053+
assert b"missing_key" not in rv.data
1054+
else:
1055+
with pytest.raises(KeyError) as exc_info:
1056+
client.get("/key")
10481057

1049-
app.debug = True
1050-
with pytest.raises(KeyError) as e:
1051-
client.get("/key")
1052-
assert e.errisinstance(BadRequest)
1053-
assert "missing_key" in e.value.get_description()
1054-
rv = client.get("/abort")
1055-
assert rv.status_code == 400
1058+
assert exc_info.errisinstance(BadRequest)
1059+
assert "missing_key" in exc_info.value.get_description()
10561060

1057-
app.debug = False
1058-
app.config["TRAP_BAD_REQUEST_ERRORS"] = True
1059-
with pytest.raises(KeyError):
1060-
client.get("/key")
1061-
with pytest.raises(BadRequest):
1062-
client.get("/abort")
1061+
if expect_abort:
1062+
rv = client.get("/abort")
1063+
assert rv.status_code == 400
1064+
else:
1065+
with pytest.raises(BadRequest):
1066+
client.get("/abort")
10631067

10641068

10651069
def test_trapping_of_all_http_exceptions(app, client):
@@ -1661,7 +1665,7 @@ def index():
16611665
assert rv.data == b"Hello World!"
16621666

16631667

1664-
def test_debug_mode_complains_after_first_request(app, client):
1668+
def test_no_setup_after_first_request(app, client):
16651669
app.debug = True
16661670

16671671
@app.route("/")
@@ -1671,19 +1675,10 @@ def index():
16711675
assert not app.got_first_request
16721676
assert client.get("/").data == b"Awesome"
16731677

1674-
with pytest.raises(AssertionError) as e:
1678+
with pytest.raises(AssertionError) as exc_info:
16751679
app.add_url_rule("/foo", endpoint="late")
16761680

1677-
assert "A setup function was called" in str(e.value)
1678-
1679-
app.debug = False
1680-
1681-
@app.route("/foo")
1682-
def working():
1683-
return "Meh"
1684-
1685-
assert client.get("/foo").data == b"Meh"
1686-
assert app.got_first_request
1681+
assert "setup method 'add_url_rule'" in str(exc_info.value)
16871682

16881683

16891684
def test_before_first_request_functions(app, client):
@@ -1720,28 +1715,23 @@ def get_and_assert():
17201715

17211716

17221717
def test_routing_redirect_debugging(monkeypatch, app, client):
1723-
@app.route("/foo/", methods=["GET", "POST"])
1724-
def foo():
1725-
return "success"
1718+
app.config["DEBUG"] = True
17261719

1727-
app.debug = False
1728-
rv = client.post("/foo", data={}, follow_redirects=True)
1729-
assert rv.data == b"success"
1720+
@app.route("/user/", methods=["GET", "POST"])
1721+
def user():
1722+
return flask.request.form["status"]
17301723

1731-
app.debug = True
1732-
1733-
with client:
1734-
rv = client.post("/foo", data={}, follow_redirects=True)
1735-
assert rv.data == b"success"
1736-
rv = client.get("/foo", data={}, follow_redirects=True)
1737-
assert rv.data == b"success"
1724+
# default redirect code preserves form data
1725+
rv = client.post("/user", data={"status": "success"}, follow_redirects=True)
1726+
assert rv.data == b"success"
17381727

1728+
# 301 and 302 raise error
17391729
monkeypatch.setattr(RequestRedirect, "code", 301)
17401730

1741-
with client, pytest.raises(AssertionError) as e:
1742-
client.post("/foo", data={})
1731+
with client, pytest.raises(AssertionError) as exc_info:
1732+
client.post("/user", data={"status": "error"}, follow_redirects=True)
17431733

1744-
assert "canonical URL 'http://localhost/foo/'" in str(e.value)
1734+
assert "canonical URL 'http://localhost/user/'" in str(exc_info.value)
17451735

17461736

17471737
def test_route_decorator_custom_endpoint(app, client):

0 commit comments

Comments
 (0)