Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def test_redirect_url_query_string() -> None:
map_ = Map()
map_.add(Rule("/path/", {"GET"}, "branch"))
_test_match_redirect(map_, "/path", "GET", "/path/?a=b", b"a=b")
def test_root_path_match() -> None:
map_ = Map()
map_.add(Rule("/", {"GET"}, "http"))
_test_no_match(map_, "/", "GET", root_path="/rooti")
_test_match(map_, "/rooti/", "GET", (map_.endpoints["http"][0], {}), root_path="/rooti")
# Relative root_path case
_test_match(map_, "/rooti/", "GET", (map_.endpoints["http"][0], {}), root_path="rooti")
def test_host() -> None:
map_ = Map(host_matching=True)
map_.add(Rule("/", {"GET"}, "index"))
map_.add(Rule("/", {"GET"}, "subdomain", host="quart.com"))
_test_match(map_, "/", "GET", (map_.endpoints["index"][0], {}))
_test_match(map_, "/", "GET", (map_.endpoints["subdomain"][0], {}), host="quart.com")
def test_strict_slashes() -> None:
def _test_strict_slashes(map_: Map) -> None:
adapter = map_.bind_to_request(False, "", "POST", "/path/", b"", False, "")
with pytest.raises(MethodNotAllowed):
adapter.match()
_test_match_redirect(map_, "/path", "GET", "/path/")
map_ = Map()
map_.add(Rule("/path", {"POST"}, "leaf"))
map_.add(Rule("/path/", {"GET"}, "branch"))
# Ensure that the matching is invariant under reversed order of
# addition to a Map.
map_reversed = Map()
map_reversed.add(Rule("/path", {"POST"}, "leaf"))
map_reversed.add(Rule("/path/", {"GET"}, "branch"))
_test_strict_slashes(map_)
_test_strict_slashes(map_reversed)
def test_websocket_and_method_not_allowed() -> None:
map_ = Map()
map_.add(Rule("/ws/", {"GET"}, "websocket", is_websocket=True))
map_.add(Rule("/ws/", {"POST"}, "post"))
adapter = map_.bind_to_request(False, "", "PUT", "/ws/", b"", False, "")
with pytest.raises(MethodNotAllowed):
adapter.match()
def test_value_building() -> None:
map_ = Map()
map_.add(Rule("/book/", {"GET"}, "book"))
adapter = map_.bind(False, "")
assert adapter.build("book", values={"page": 1}) == "/book/1"
assert adapter.build("book", values={"page": 1, "line": 12}) == "/book/1?line=12"
assert adapter.build("book", values={"page": 1, "line": [1, 2]}) == "/book/1?line=1&line=2"
def test_websocket_bad_request(websocket: bool) -> None:
map_ = Map()
map_.add(Rule("/ws/", {"GET"}, "websocket", is_websocket=websocket))
adapter = map_.bind_to_request(False, "", "GET", "/ws/", b"", not websocket, "")
with pytest.raises(BadRequest):
adapter.match()
def test_uuid_converter_match() -> None:
map_ = Map()
map_.add(Rule("/", {"GET"}, "uuid"))
_test_match(
map_,
"/a8098c1a-f86e-11da-bd1a-00112444be1e",
"GET",
(map_.endpoints["uuid"][0], {"uuid": uuid.UUID("a8098c1a-f86e-11da-bd1a-00112444be1e")}),
)
def test_any_converter() -> None:
map_ = Map()
map_.add(Rule('/', {"GET"}, "any"))
_test_match(map_, "/about", "GET", (map_.endpoints["any"][0], {"name": "about"}))
_test_match(map_, "/left,right", "GET", (map_.endpoints["any"][0], {"name": "left,right"}))
_test_no_match(map_, "/other", "GET")
def test_redirect_url_host() -> None:
map_ = Map(host_matching=True)
map_.add(Rule("/path/", {"GET"}, "branch", host="quart.com"))
map_.add(Rule("/path/", {"GET"}, "branch", host="flask.com"))
_test_match_redirect(map_, "/path", "GET", "/path/", host="quart.com")
_test_match_redirect(map_, "/path", "GET", "/path/", host="flask.com")