fix subdomain_matching=False behavior

This commit is contained in:
David Lord 2024-11-10 20:13:05 -08:00
parent 07c7d5730a
commit 4995a775df
No known key found for this signature in database
GPG key ID: 43368A7AA8CC5926
5 changed files with 98 additions and 43 deletions

View file

@ -4,6 +4,7 @@ import typing as t
import uuid
import warnings
import weakref
from contextlib import nullcontext
from datetime import datetime
from datetime import timezone
from platform import python_implementation
@ -1483,6 +1484,48 @@ def test_request_locals():
assert not flask.g
@pytest.mark.parametrize(
("subdomain_matching", "host_matching", "expect_base", "expect_abc", "expect_xyz"),
[
(False, False, "default", "default", "default"),
(True, False, "default", "abc", "<invalid>"),
(False, True, "default", "abc", "default"),
],
)
def test_server_name_matching(
subdomain_matching: bool,
host_matching: bool,
expect_base: str,
expect_abc: str,
expect_xyz: str,
) -> None:
app = flask.Flask(
__name__,
subdomain_matching=subdomain_matching,
host_matching=host_matching,
static_host="example.test" if host_matching else None,
)
app.config["SERVER_NAME"] = "example.test"
@app.route("/", defaults={"name": "default"}, host="<name>")
@app.route("/", subdomain="<name>", host="<name>.example.test")
def index(name: str) -> str:
return name
client = app.test_client()
r = client.get(base_url="http://example.test")
assert r.text == expect_base
r = client.get(base_url="http://abc.example.test")
assert r.text == expect_abc
with pytest.warns() if subdomain_matching else nullcontext():
r = client.get(base_url="http://xyz.other.test")
assert r.text == expect_xyz
def test_server_name_subdomain():
app = flask.Flask(__name__, subdomain_matching=True)
client = app.test_client()