|
| 1 | +"""Test cases for nested blueprint URL prefix dynamic calculation.""" |
| 2 | +import pytest |
| 3 | +import flask |
| 4 | + |
| 5 | + |
| 6 | +def test_blueprint_url_prefix_original(): |
| 7 | + """Test that url_prefix returns the original value set during initialization.""" |
| 8 | + bp = flask.Blueprint("test", __name__, url_prefix="/test") |
| 9 | + |
| 10 | + assert bp.url_prefix == "/test" |
| 11 | + assert bp._url_prefix == "/test" |
| 12 | + |
| 13 | + |
| 14 | +def test_blueprint_full_url_prefix_before_registration(): |
| 15 | + """Test that full_url_prefix returns None before registration.""" |
| 16 | + bp = flask.Blueprint("test", __name__, url_prefix="/test") |
| 17 | + |
| 18 | + assert bp.full_url_prefix is None |
| 19 | + |
| 20 | + |
| 21 | +def test_blueprint_full_url_prefix_after_single_registration(): |
| 22 | + """Test that full_url_prefix returns the full path after single registration.""" |
| 23 | + app = flask.Flask(__name__) |
| 24 | + bp = flask.Blueprint("test", __name__, url_prefix="/test") |
| 25 | + |
| 26 | + @bp.route("/") |
| 27 | + def index(): |
| 28 | + return "test" |
| 29 | + |
| 30 | + app.register_blueprint(bp, url_prefix="/api") |
| 31 | + |
| 32 | + assert bp.url_prefix == "/test" |
| 33 | + assert bp.full_url_prefix == "/api/test" |
| 34 | + assert bp.get_registered_url_prefix() == "/api/test" |
| 35 | + |
| 36 | + |
| 37 | +def test_nested_blueprint_full_url_prefix(): |
| 38 | + """Test that nested blueprints get the full URL prefix including parent prefixes.""" |
| 39 | + app = flask.Flask(__name__) |
| 40 | + |
| 41 | + parent = flask.Blueprint("parent", __name__, url_prefix="/parent") |
| 42 | + child = flask.Blueprint("child", __name__, url_prefix="/child") |
| 43 | + grandchild = flask.Blueprint("grandchild", __name__, url_prefix="/grandchild") |
| 44 | + |
| 45 | + @parent.route("/") |
| 46 | + def parent_index(): |
| 47 | + return "parent" |
| 48 | + |
| 49 | + @child.route("/") |
| 50 | + def child_index(): |
| 51 | + return "child" |
| 52 | + |
| 53 | + @grandchild.route("/") |
| 54 | + def grandchild_index(): |
| 55 | + return "grandchild" |
| 56 | + |
| 57 | + child.register_blueprint(grandchild) |
| 58 | + parent.register_blueprint(child) |
| 59 | + app.register_blueprint(parent, url_prefix="/api") |
| 60 | + |
| 61 | + assert parent.url_prefix == "/parent" |
| 62 | + assert parent.full_url_prefix == "/api/parent" |
| 63 | + |
| 64 | + assert child.url_prefix == "/child" |
| 65 | + assert child.full_url_prefix == "/api/parent/child" |
| 66 | + |
| 67 | + assert grandchild.url_prefix == "/grandchild" |
| 68 | + assert grandchild.full_url_prefix == "/api/parent/child/grandchild" |
| 69 | + |
| 70 | + |
| 71 | +def test_nested_blueprint_url_rules(): |
| 72 | + """Test that nested blueprints have correct URL rules.""" |
| 73 | + app = flask.Flask(__name__) |
| 74 | + |
| 75 | + parent = flask.Blueprint("parent", __name__, url_prefix="/parent") |
| 76 | + child = flask.Blueprint("child", __name__, url_prefix="/child") |
| 77 | + |
| 78 | + @parent.route("/home") |
| 79 | + def parent_home(): |
| 80 | + return "parent home" |
| 81 | + |
| 82 | + @child.route("/home") |
| 83 | + def child_home(): |
| 84 | + return "child home" |
| 85 | + |
| 86 | + parent.register_blueprint(child) |
| 87 | + app.register_blueprint(parent, url_prefix="/api") |
| 88 | + |
| 89 | + client = app.test_client() |
| 90 | + |
| 91 | + assert client.get("/api/parent/home").data == b"parent home" |
| 92 | + assert client.get("/api/parent/child/home").data == b"child home" |
| 93 | + |
| 94 | + |
| 95 | +def test_multiple_registrations(): |
| 96 | + """Test behavior when blueprint is registered multiple times.""" |
| 97 | + app = flask.Flask(__name__) |
| 98 | + bp = flask.Blueprint("test", __name__, url_prefix="/test") |
| 99 | + |
| 100 | + @bp.route("/") |
| 101 | + def index(): |
| 102 | + return flask.request.endpoint |
| 103 | + |
| 104 | + app.register_blueprint(bp, url_prefix="/api1") |
| 105 | + app.register_blueprint(bp, name="test2", url_prefix="/api2") |
| 106 | + |
| 107 | + assert bp.url_prefix == "/test" |
| 108 | + assert bp.full_url_prefix is None |
| 109 | + assert bp.get_registered_url_prefix() is None |
| 110 | + assert bp.get_registered_url_prefix("test") == "/api1/test" |
| 111 | + assert bp.get_registered_url_prefix("test2") == "/api2/test" |
| 112 | + |
| 113 | + client = app.test_client() |
| 114 | + assert client.get("/api1/test/").data == b"test.index" |
| 115 | + assert client.get("/api2/test/").data == b"test2.index" |
| 116 | + |
| 117 | + |
| 118 | +def test_get_registered_url_prefix_with_name(): |
| 119 | + """Test get_registered_url_prefix with specific name.""" |
| 120 | + app = flask.Flask(__name__) |
| 121 | + bp = flask.Blueprint("test", __name__, url_prefix="/test") |
| 122 | + |
| 123 | + app.register_blueprint(bp, url_prefix="/api") |
| 124 | + |
| 125 | + assert bp.get_registered_url_prefix("test") == "/api/test" |
| 126 | + assert bp.get_registered_url_prefix("nonexistent") is None |
| 127 | + |
| 128 | + |
| 129 | +def test_blueprint_without_url_prefix(): |
| 130 | + """Test blueprint without initial url_prefix.""" |
| 131 | + app = flask.Flask(__name__) |
| 132 | + bp = flask.Blueprint("test", __name__) |
| 133 | + |
| 134 | + @bp.route("/") |
| 135 | + def index(): |
| 136 | + return "test" |
| 137 | + |
| 138 | + app.register_blueprint(bp, url_prefix="/api") |
| 139 | + |
| 140 | + assert bp.url_prefix is None |
| 141 | + assert bp.full_url_prefix == "/api" |
| 142 | + assert bp.get_registered_url_prefix() == "/api" |
| 143 | + |
| 144 | + |
| 145 | +def test_nested_with_partial_prefixes(): |
| 146 | + """Test nested blueprints with some missing prefixes.""" |
| 147 | + app = flask.Flask(__name__) |
| 148 | + |
| 149 | + parent = flask.Blueprint("parent", __name__) |
| 150 | + child = flask.Blueprint("child", __name__, url_prefix="/child") |
| 151 | + grandchild = flask.Blueprint("grandchild", __name__) |
| 152 | + |
| 153 | + @parent.route("/") |
| 154 | + def parent_index(): |
| 155 | + return "parent" |
| 156 | + |
| 157 | + @child.route("/") |
| 158 | + def child_index(): |
| 159 | + return "child" |
| 160 | + |
| 161 | + @grandchild.route("/") |
| 162 | + def grandchild_index(): |
| 163 | + return "grandchild" |
| 164 | + |
| 165 | + child.register_blueprint(grandchild, url_prefix="/gc") |
| 166 | + parent.register_blueprint(child) |
| 167 | + app.register_blueprint(parent, url_prefix="/api") |
| 168 | + |
| 169 | + assert parent.full_url_prefix == "/api" |
| 170 | + assert child.full_url_prefix == "/api/child" |
| 171 | + assert grandchild.full_url_prefix == "/api/child/gc" |
| 172 | + |
| 173 | + client = app.test_client() |
| 174 | + assert client.get("/api/").data == b"parent" |
| 175 | + assert client.get("/api/child/").data == b"child" |
| 176 | + assert client.get("/api/child/gc/").data == b"grandchild" |
| 177 | + |
| 178 | + |
| 179 | +if __name__ == "__main__": |
| 180 | + pytest.main([__file__, "-v"]) |
0 commit comments