Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def basic_map() -> Map:
map_ = Map()
map_.add(Rule("/", {"POST"}, "index"))
map_.add(Rule("/", {"DELETE"}, "delete_index"))
map_.add(Rule("/leaf", {"GET"}, "leaf"))
map_.add(Rule("/branch/", {"GET"}, "branch"))
return map_
def test_path_converter() -> None:
map_ = Map()
map_.add(Rule("/", {"GET"}, "index"))
map_.add(Rule("/constant", {"GET"}, "constant"))
map_.add(Rule("/", {"GET"}, "integer"))
map_.add(Rule("/", {"GET"}, "page"))
map_.add(Rule("//constant", {"GET"}, "page_constant"))
map_.add(Rule("//middle/", {"GET"}, "double_page"))
map_.add(Rule("//middle//constant", {"GET"}, "double_page_constant"))
map_.add(Rule("/Colon:", {"GET"}, "colon_path"))
map_.add(Rule("/Colon:", {"GET"}, "colon_base"))
_test_match(map_, "/", "GET", (map_.endpoints["index"][0], {}))
_test_match(map_, "/constant", "GET", (map_.endpoints["constant"][0], {}))
_test_match(map_, "/20", "GET", (map_.endpoints["integer"][0], {"integer": 20}))
_test_match(map_, "/branch/leaf", "GET", (map_.endpoints["page"][0], {"page": "branch/leaf"}))
_test_match(
map_, "/branch/constant", "GET", (map_.endpoints["page_constant"][0], {"page": "branch"})
)
_test_match(
map_,
"/branch/middle/leaf",
"GET",
(map_.endpoints["double_page"][0], {"left": "branch", "right": "leaf"}),
)
_test_match(
def test_root_path_build() -> None:
map_ = Map()
map_.add(Rule("/", {"GET"}, "http"))
adapter = map_.bind_to_request(False, "", "GET", "/", b"", False, "/rooti")
assert adapter.build("http", method="GET") == "/rooti/"
# Relative root_path case
adapter = map_.bind_to_request(False, "", "GET", "/", b"", False, "rooti")
assert adapter.build("http", method="GET") == "/rooti/"
def test_build_external() -> None:
map_ = Map()
map_.add(Rule("/ws/", {"GET"}, "websocket", is_websocket=True))
map_.add(Rule("/", {"GET"}, "index"))
adapter = map_.bind(True, "localhost")
adapter.build("websocket") == "wss://localhost/ws/"
adapter.build("index") == "https://localhost/"
def test_defaults() -> None:
map_ = Map()
map_.add(Rule("/book/", {"GET"}, "book", defaults={"page": 1}))
map_.add(Rule("/book//", {"GET"}, "book"))
_test_match(map_, "/book/", "GET", (map_.endpoints["book"][0], {"page": 1}))
_test_match_redirect(map_, "/book/1/", "GET", "/book/")
_test_match(map_, "/book/2/", "GET", (map_.endpoints["book"][1], {"page": 2}))
adapter = map_.bind(False, "")
assert adapter.build("book", method="GET") == "/book/"
assert adapter.build("book", method="GET", values={"page": 1}) == "/book/"
assert adapter.build("book", method="GET", values={"page": 2}) == "/book/2/"
def basic_map() -> Map:
map_ = Map()
map_.add(Rule("/", {"POST"}, "index"))
map_.add(Rule("/", {"DELETE"}, "delete_index"))
map_.add(Rule("/leaf", {"GET"}, "leaf"))
map_.add(Rule("/branch/", {"GET"}, "branch"))
return map_
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")
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_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_ordering() -> None:
map_ = Map()
map_.add(Rule("/fixed", {"GET"}, "fixed"))
map_.add(Rule("/", {"GET"}, "path"))
map_.add(Rule("//", {"GET"}, "path"))
_test_match(map_, "/fixed", "GET", (map_.endpoints["fixed"][0], {}))
_test_match(map_, "/path", "GET", (map_.endpoints["path"][1], {"path": "path"}))
_test_match(
map_, "/left/right", "GET", (map_.endpoints["path"][0], {"left": "left", "right": "right"})
)