diff --git a/api/app/routers/images.py b/api/app/routers/images.py index ed5d2a5..b8fe795 100644 --- a/api/app/routers/images.py +++ b/api/app/routers/images.py @@ -30,7 +30,7 @@ def _error(detail: str, code: str, status: int): def _image_to_dict( image: Image, *, cdn_base: str | None = None, duplicate: bool | None = None ) -> dict[str, Any]: - _base = cdn_base.rstrip("/") if cdn_base else None + _base = cdn_base.strip().rstrip("/") if cdn_base else None file_url = f"{_base}/{image.storage_key}" if _base else f"/api/v1/images/{image.id}/file" thumbnail_url = ( (f"{_base}/{image.thumbnail_key}" if _base else f"/api/v1/images/{image.id}/thumbnail") diff --git a/api/tests/unit/test_url_construction.py b/api/tests/unit/test_url_construction.py index 917e3f6..54bed6b 100644 --- a/api/tests/unit/test_url_construction.py +++ b/api/tests/unit/test_url_construction.py @@ -56,3 +56,10 @@ def test_cdn_trailing_slash_normalised(): assert result["file_url"] == "https://cdn.example.com/abc123storagekey" assert result["thumbnail_url"] == "https://cdn.example.com/abc123storagekey-thumb" assert "//" not in result["file_url"].replace("https://", "") + + +def test_cdn_trailing_whitespace_normalised(): + img = _make_image(thumbnail_key="abc123storagekey-thumb") + result = _image_to_dict(img, cdn_base="https://cdn.example.com ") + assert result["file_url"] == "https://cdn.example.com/abc123storagekey" + assert result["thumbnail_url"] == "https://cdn.example.com/abc123storagekey-thumb"