diff --git a/src/idom/backend/_common.py b/src/idom/backend/_common.py index 90e2dea5b..dd7916353 100644 --- a/src/idom/backend/_common.py +++ b/src/idom/backend/_common.py @@ -128,3 +128,7 @@ class CommonOptions: url_prefix: str = "" """The URL prefix where IDOM resources will be served from""" + + def __post_init__(self) -> None: + if self.url_prefix and not self.url_prefix.startswith("/"): + raise ValueError("Expected 'url_prefix' to start with '/'") diff --git a/tests/test_backend/test__common.py b/tests/test_backend/test__common.py index e575625a2..3a3b648d5 100644 --- a/tests/test_backend/test__common.py +++ b/tests/test_backend/test__common.py @@ -1,7 +1,19 @@ import pytest from idom import html -from idom.backend._common import traversal_safe_path, vdom_head_elements_to_html +from idom.backend._common import ( + CommonOptions, + traversal_safe_path, + vdom_head_elements_to_html, +) + + +def test_common_options_url_prefix_starts_with_slash(): + # no prefix specified + CommonOptions(url_prefix="") + + with pytest.raises(ValueError, match="start with '/'"): + CommonOptions(url_prefix="not-start-withslash") @pytest.mark.parametrize(