forked from orbit-oss/flask
Fix blueprint nested url_prefix
This ensures that the url_prefix is correctly applied, no matter if set during the registration override or when constructing the blueprint.
This commit is contained in:
parent
a541c2ac8b
commit
99afbb277d
2 changed files with 28 additions and 42 deletions
|
|
@ -354,7 +354,9 @@ class Blueprint(Scaffold):
|
||||||
bp_options["url_prefix"] = (
|
bp_options["url_prefix"] = (
|
||||||
state.url_prefix.rstrip("/") + "/" + bp_url_prefix.lstrip("/")
|
state.url_prefix.rstrip("/") + "/" + bp_url_prefix.lstrip("/")
|
||||||
)
|
)
|
||||||
else:
|
elif bp_url_prefix is not None:
|
||||||
|
bp_options["url_prefix"] = bp_url_prefix
|
||||||
|
elif state.url_prefix is not None:
|
||||||
bp_options["url_prefix"] = state.url_prefix
|
bp_options["url_prefix"] = state.url_prefix
|
||||||
|
|
||||||
bp_options["name_prefix"] = options.get("name_prefix", "") + self.name + "."
|
bp_options["name_prefix"] = options.get("name_prefix", "") + self.name + "."
|
||||||
|
|
|
||||||
|
|
@ -837,48 +837,32 @@ def test_nested_blueprint(app, client):
|
||||||
assert client.get("/parent/child/grandchild/no").data == b"Grandchild no"
|
assert client.get("/parent/child/grandchild/no").data == b"Grandchild no"
|
||||||
|
|
||||||
|
|
||||||
def test_nested_blueprint_url_prefix(app, client):
|
@pytest.mark.parametrize(
|
||||||
parent = flask.Blueprint("parent", __name__, url_prefix="/parent")
|
"parent_init, child_init, parent_registration, child_registration",
|
||||||
child = flask.Blueprint("child", __name__, url_prefix="/child")
|
[
|
||||||
grandchild = flask.Blueprint("grandchild", __name__, url_prefix="/grandchild")
|
("/parent", "/child", None, None),
|
||||||
apple = flask.Blueprint("apple", __name__, url_prefix="/apple")
|
("/parent", None, None, "/child"),
|
||||||
|
(None, None, "/parent", "/child"),
|
||||||
@parent.route("/")
|
("/other", "/something", "/parent", "/child"),
|
||||||
def parent_index():
|
],
|
||||||
return "Parent"
|
)
|
||||||
|
def test_nesting_url_prefixes(
|
||||||
|
parent_init,
|
||||||
|
child_init,
|
||||||
|
parent_registration,
|
||||||
|
child_registration,
|
||||||
|
app,
|
||||||
|
client,
|
||||||
|
) -> None:
|
||||||
|
parent = flask.Blueprint("parent", __name__, url_prefix=parent_init)
|
||||||
|
child = flask.Blueprint("child", __name__, url_prefix=child_init)
|
||||||
|
|
||||||
@child.route("/")
|
@child.route("/")
|
||||||
def child_index():
|
def index():
|
||||||
return "Child"
|
return "index"
|
||||||
|
|
||||||
@grandchild.route("/")
|
parent.register_blueprint(child, url_prefix=child_registration)
|
||||||
def grandchild_index():
|
app.register_blueprint(parent, url_prefix=parent_registration)
|
||||||
return "Grandchild"
|
|
||||||
|
|
||||||
@apple.route("/")
|
response = client.get("/parent/child/")
|
||||||
def apple_index():
|
assert response.status_code == 200
|
||||||
return "Apple"
|
|
||||||
|
|
||||||
child.register_blueprint(grandchild)
|
|
||||||
child.register_blueprint(apple, url_prefix="/orange") # test overwrite
|
|
||||||
parent.register_blueprint(child)
|
|
||||||
app.register_blueprint(parent)
|
|
||||||
|
|
||||||
assert client.get("/parent/").data == b"Parent"
|
|
||||||
assert client.get("/parent/child/").data == b"Child"
|
|
||||||
assert client.get("/parent/child/grandchild/").data == b"Grandchild"
|
|
||||||
assert client.get("/parent/child/orange/").data == b"Apple"
|
|
||||||
|
|
||||||
|
|
||||||
def test_nested_blueprint_url_prefix_only_parent_prefix(app, client):
|
|
||||||
parent = flask.Blueprint("parent", __name__)
|
|
||||||
child = flask.Blueprint("child", __name__)
|
|
||||||
|
|
||||||
@child.route("/child-endpoint")
|
|
||||||
def child_index():
|
|
||||||
return "Child"
|
|
||||||
|
|
||||||
parent.register_blueprint(child)
|
|
||||||
app.register_blueprint(parent, url_prefix="/parent")
|
|
||||||
|
|
||||||
assert client.get("/parent/child-endpoint").data == b"Child"
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue