Island: Enforce "no trailing slash" rule for URLs

This commit is contained in:
Kekoa Kaaikala 2022-08-15 17:34:16 +00:00
parent 5d36b7a981
commit 19df4d9755
2 changed files with 14 additions and 8 deletions

View File

@ -128,7 +128,14 @@ class FlaskDIWrapper:
raise ValueError(f"Resource {resource.__name__} has no defined URLs")
self._reserve_urls(resource.urls)
resource.urls = map(lambda url: url.rstrip("/"), resource.urls)
# enforce our rule that URLs should not contain a trailing slash
for url in resource.urls:
if url.endswith("/"):
raise ValueError(
f"Resource {resource.__name__} has an invalid URL: A URL "
"should not have a trailing slash."
)
dependencies = self._container.resolve_dependencies(resource)
self._api.add_resource(resource, *resource.urls, resource_class_args=dependencies)

View File

@ -75,7 +75,7 @@ def test_url_check_slash_stripping__trailing_slash(resource_manager):
def test_url_check_slash_stripping__path_separation(resource_manager):
resource3 = get_mock_resource("res3", ["/beef/face/"])
resource3 = get_mock_resource("res3", ["/beef/face"])
resource4 = get_mock_resource("res4", ["/beefface"])
# Following shouldn't raise and exception
@ -83,9 +83,8 @@ def test_url_check_slash_stripping__path_separation(resource_manager):
resource_manager.add_resource(resource4)
def test_trailing_slash_removal(resource_manager):
bogus_endpoint = "/beef/face"
resource3 = get_mock_resource("res3", [f"{bogus_endpoint}/"])
resource_manager.add_resource(resource3)
registered_rules = resource_manager._api.app.url_map._rules
assert any([rule.rule == bogus_endpoint for rule in registered_rules])
def test_trailing_slash_enforcement(resource_manager):
bad_endpoint = "/beef/face/"
with pytest.raises(ValueError):
resource3 = get_mock_resource("res3", [f"{bad_endpoint}"])
resource_manager.add_resource(resource3)