Skip to content
Myr

test_request.py

This file

Path

tests/test_request.py

70 lines · 1.9 KiB · Python

Last changed by

configure and check `request.trusted_hosts` (#5637)David Lordbefore adoption7b21d43

History of this file

Take it

1from __future__ import annotations23from flask import Flask4from flask import Request5from flask import request6from flask.testing import FlaskClient789def test_max_content_length(app: Flask, client: FlaskClient) -> None:10    app.config["MAX_CONTENT_LENGTH"] = 501112    @app.post("/")13    def index():14        request.form["myfile"]15        AssertionError()1617    @app.errorhandler(413)18    def catcher(error):19        return "42"2021    rv = client.post("/", data={"myfile": "foo" * 50})22    assert rv.data == b"42"232425def test_limit_config(app: Flask):26    app.config["MAX_CONTENT_LENGTH"] = 10027    app.config["MAX_FORM_MEMORY_SIZE"] = 5028    app.config["MAX_FORM_PARTS"] = 329    r = Request({})3031    # no app context, use Werkzeug defaults32    assert r.max_content_length is None33    assert r.max_form_memory_size == 500_00034    assert r.max_form_parts == 1_0003536    # in app context, use config37    with app.app_context():38        assert r.max_content_length == 10039        assert r.max_form_memory_size == 5040        assert r.max_form_parts == 34142    # regardless of app context, use override43    r.max_content_length = 9044    r.max_form_memory_size = 3045    r.max_form_parts = 44647    assert r.max_content_length == 9048    assert r.max_form_memory_size == 3049    assert r.max_form_parts == 45051    with app.app_context():52        assert r.max_content_length == 9053        assert r.max_form_memory_size == 3054        assert r.max_form_parts == 4555657def test_trusted_hosts_config(app: Flask) -> None:58    app.config["TRUSTED_HOSTS"] = ["example.test", ".other.test"]5960    @app.get("/")61    def index() -> str:62        return ""6364    client = app.test_client()65    r = client.get(base_url="http://example.test")66    assert r.status_code == 20067    r = client.get(base_url="http://a.other.test")68    assert r.status_code == 20069    r = client.get(base_url="http://bad.test")70    assert r.status_code == 400