From e79d6174fb838f193dd072d43009fc28f2d41acc Mon Sep 17 00:00:00 2001 From: rmorshea Date: Tue, 24 Jan 2023 10:35:27 -0800 Subject: [PATCH] check that url prefix starts with / --- src/idom/backend/_common.py | 4 ++++ tests/test_backend/test__common.py | 14 +++++++++++++- 2 files changed, 17 insertions(+), 1 deletion(-) 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(